Java实现地图的导出

2021-12-06 10:40:11 浏览数 (1)

概述

**前文**讲了在前端实现地图的导出,本文简单的通过java实现一下后端地图的导出。

实现效果

实现代码

代码语言:javascript复制
package com.lzugis.image;

import sun.misc.BASE64Decoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class MapPrint {

    static int TILE_SIZE = 256;
    static double RES_ZERO = 156543.03392804097;
    static double[] resolutions = new double[19];
    static int ZOOM = 4;

    public static void main(String[] args) {
        long start = new Date().getTime();
        System.out.println("开始出图。。。");
        double res = RES_ZERO;
        for (int i = 0; i < 19; i  ) {
            resolutions[i] = res;
            res = res / 2;
        }
        double[] extent = {4816313.49458338, 1168266.6364252055, 19000980.555761307, 7826564.13063698};
        int[] rowCol = getTileRowCol(extent);

        int xmin = rowCol[0];
        int xmax = rowCol[1];
        int ymin = rowCol[2];
        int ymax = rowCol[3];

        int width = (xmax - xmin) * TILE_SIZE;
        int height = (ymax - ymin) * TILE_SIZE;
        try {
            BufferedImage mergeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = mergeImage.createGraphics();
            for (int i = xmin; i <= xmax ; i  ) {
                for (int j = ymin; j <= ymax ; j  ) {
                    String destUrl = "http://webrd01.is.autonavi.com/appmaptile?x=" i "&y=" j "&z=" ZOOM "&lang=zh_cn&size=1&scale=1&style=8";
                    BufferedImage tile = getBufferedImageDestUrl(destUrl);
                    int x = (i - xmin) * TILE_SIZE;
                    int y = (j -ymin) * TILE_SIZE;
                    graphics2D.drawImage(tile, x, y, TILE_SIZE, TILE_SIZE, null);
                }
            }

            // 添加wms图层
            String extentStr = extent[0] ","   extent[1] ","   extent[2] ","   extent[3];
            String wmsUrl = "http://localhost:8081/geoserver/ecodrought/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&LAYERS=ecodrought:paddy-rice&CRS=EPSG:3857&SRS=EPSG:3857&WIDTH=" width "&HEIGHT=" height "&STYLES=&BBOX=" extentStr;
            BufferedImage wms = getBufferedImageDestUrl(wmsUrl);
            System.out.println(wmsUrl);
            graphics2D.drawImage(wms, 0, 0, width, height, null);

            // 添加地图标题
            String mapTitle = "地图导出测试";
            graphics2D.setColor(new Color(255,0,0,255));

            Font font = new Font("微软雅黑", Font.BOLD, 24);
            graphics2D.setFont(font);
            int length = graphics2D.getFontMetrics(graphics2D.getFont()).charsWidth(mapTitle.toCharArray(), 0, mapTitle.length());
            int posx = 60, posy = 80, offest = 15;
            graphics2D.drawString(mapTitle, posx, posy);
            graphics2D.setStroke(new BasicStroke(2.0f));
            int x1 = posx - offest, y1 = posy - offest - 18, x2 = posx   length   offest, y2 = posy   offest   6;
            graphics2D.drawLine(x1, y1, x2, y1);
            graphics2D.drawLine(x2, y1, x2, y2);
            graphics2D.drawLine(x2, y2, x1, y2);
            graphics2D.drawLine(x1, y2, x1, y1);

            // 添加指北针
            String base64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAAXNSR0IArs4c6QAADPFJREFUeF7tnXmQXUUVh8 54zBhUSwZIgIqYBCS7vtCEi2IWiQiCEqxFAEFopQaSlYxlpS4sAoqipYgAqJGFAn7REFQ2SQiReEyDHl9esyEESKLLAa3oE4yzDtWwyMOw7x3l 6 y7x7q/JHavr8zulzvtfvLufdRqiOjs4AdvTsq8lDBUCHQ1ABUAHQ4Rno8OlXK0AFQIdnoMOnX60AFQAdngHH05dScoTkOUR0dlq3UfpElOhDnWhw2qA7yS6qQM1cpIYgSr8CIGfaogo0LrxUEETpVwCUBwATaWIIKgByLnCU 6gCTWKfCIIo/WoFiKqQ579HFaiF 9gQROlXAHgucJR8VIHa2MeCIEq/AiCqQhP XqvVptfr9WcSmrUcHlUg20vEKP0KgISVFEKcrrU L6GZLwCM7tlEdE4rBxUArio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cj/mzJmz/ejo6BHN8wVvP/BAxK8ppby O8EaAE8NH2PMvAwRryEic1lZ2MNc TDzUYhoviK6HAf6UE9Pzx79/f3/cay7Sc4aAMcNH/cy85VjY2MrVq9e/ayvSfvQ3X333bfp6uo6DBHNJeW7XPlAxMVKqatd6U3UsQJgxowZPdOmTTOXX9MsAvwbAPwgCII 3993FjEmMm3eZDJXER z3e6GmW/UWpuvGy HFQCWDR 3MvP1XV1dpvD/9jK7nEVrtdqWY2NjixDRvA/gwJThcKPRCAcHB82tbOeHLQDmhsW2CaJ6BBGXmyU j/anBHE6Hzpr1qw55iuCmc1vI3ZO4oCZz9Ban5fEJu7Y1AAIIU5ERHP2H e4HgD6ent7V0Tdmo0jVuYxzVvPhzU3wY77ppB IvLyc/rUAEgpzTPrt7QpxoPM3IeIfWlvzZa50HFiN7eemdl8RZjzhT3a2TDzwVrrn8XRTTImFQBSSvNOX/Nu34nHRgC4FhFXKKVuShJIp49tPkY3K8ORLXZJvYKIzEml0yMtAOae/9xxkZhHrebTbgrv9das09kXUMzcembml74i9hoX4t8bjcYeg4ODZhtdZ0diAIQQByOi XQ/awreaDT68ro16ywLBRUyt56DIFjUBGIbAFhKRBe5DDcxAM03fKzo7u7uK8qtWZcJKaJW89azOU84zPXPyf8H8Ry1vYSr14wAAAAASUVORK5CYII=";
            int w =50, h = 50, x = width - w - posx;
            graphics2D.drawImage(base64ToBufferedImage(base64), x , posx, w, h, null);

            // 保存图片
            generateSaveFile(mergeImage, "merge.png");

            long end = new Date().getTime();
            System.out.println(String.format("出图结束,耗时%sms", end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取切片行列号
     * @param {double[]} extent - 出图四至,格式为[xmin, ymin, xmax, ymax]
     * @return {int[]} - 切片的行列号范围
     */
    public static int[] getTileRowCol (double[] extent) {
        if(extent.length != 4) return new int[]{};
        double originY = RES_ZERO * TILE_SIZE / 2;
        double originX = -originY;
        double res = resolutions[ZOOM] * TILE_SIZE;
        int xmin = (int) Math.floor((extent[0] - originX) / res);
        int xmax = (int) Math.ceil((extent[2] - originX) / res);
        int ymax = (int) Math.ceil((originY - extent[1]) / res);
        int ymin = (int) Math.floor((originY - extent[3]) / res);
        // 往周围扩充一圈
        return new int[]{ xmin   1, xmax - 1, ymin   1, ymax - 1 };
    }

    /**
     * 远程图片转BufferedImage
     * @return {BufferedImage} - 返回图片BufferedImage
     */
    public static BufferedImage getBufferedImageDestUrl(String destUrl) {
        HttpURLConnection conn = null;
        BufferedImage image = null;
        try {
            URL url = new URL(destUrl);
            conn = (HttpURLConnection) url.openConnection();
            if (conn.getResponseCode() == 200) {
                image = ImageIO.read(conn.getInputStream());
                return image;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return image;
    }

    /**
     * base64转BufferedImage
     * @param {String} base64
     * @return {BufferedImage}
     * @throws Exception
     */
    public static BufferedImage base64ToBufferedImage(String base64) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] result = decoder.decodeBuffer(base64);
        InputStream stream = new ByteArrayInputStream(result);
        BufferedImage bufferedImage = ImageIO.read(stream);
        return bufferedImage;
    }

    /**
     * 输出图片
     * @param buffImg  图像拼接叠加之后的BufferedImage对象
     * @param savePath 图像拼接叠加之后的保存路径
     */
    public static void generateSaveFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".")   1;
        try {
            File outFile = new File(savePath);
            if(!outFile.exists()){
                outFile.createNewFile();
            }
            ImageIO.write(buffImg, savePath.substring(temp), outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实现分析

上面的实现中,实现了两类图层的叠加:底图切片和WMS图层的加载。底图切片的核心加载逻辑是通过传入的四至和地图级别计算需要叠加的切片的x和y的范围,并计算合并后的图片的大小,wms加载的逻辑是通过传入的四至和计算好的图片大小,再通过参数url和layers获取对应的图层的图片,再将两个图片合并。

0 人点赞