geotools绘制地图导出图片

第一步:

arcmap创建工程使用ArcGIS_SLD_Converter导出sld

第二步:编写代码实现,直接上代码

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.nio.charset.Charset;

import java.util.HashMap;

import java.util.Map;

import javax.imageio.ImageIO;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;

import org.geotools.data.simple.SimpleFeatureSource;

import org.geotools.factory.CommonFactoryFinder;

import org.geotools.geometry.jts.ReferencedEnvelope;

import org.geotools.map.FeatureLayer;

import org.geotools.map.Layer;

import org.geotools.map.MapContent;

import org.geotools.referencing.crs.DefaultGeographicCRS;

import org.geotools.renderer.lite.StreamingRenderer;

import org.geotools.styling.SLD;
import org.geotools.styling.SLDParser;

import org.geotools.styling.Style;

import org.geotools.styling.StyleFactory;

import org.opengis.referencing.crs.CoordinateReferenceSystem;

public class Shape2Image {
    private static MapContent map = new MapContent();

    /**

     * 添加shp文件

     * @param shpPath

     */

    public void addShapeLayer(String shpPath, String sldPath){
        try{
            File file = new File(shpPath);

            ShapefileDataStore shpDataStore = null;

            shpDataStore = new ShapefileDataStore(file.toURL());

//设置编码

            Charset charset = Charset.forName("GB18030");

            shpDataStore.setCharset(charset);

            String typeName = shpDataStore.getTypeNames()[0];

            SimpleFeatureSource featureSource = null;

            featureSource = shpDataStore.getFeatureSource (typeName);

//SLD的方式

            File sldFile = new File(sldPath);

            StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();

            SLDParser stylereader = new SLDParser(styleFactory, sldFile.toURI().toURL());

            Style[] stylearray = stylereader.readXML();

            Style style = stylearray[0];

//默认的方式

//            Style style = SLD.createSimpleStyle(featureSource.getSchema());
//
//            SLD.setPolyColour(style, Color.RED );

            Layer layer = new FeatureLayer(featureSource, style);

            map.addLayer(layer);

        }

        catch(Exception e){
            e.printStackTrace();

        }

    }

    /**

     * 添加shp文件

     * @param featureSource

     */

    public void addShapeLayer(FeatureSource featureSource, String sldPath){
        try{

//SLD的方式

            File sldFile = new File(sldPath);

            StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();

            SLDParser stylereader = new SLDParser(styleFactory, sldFile.toURI().toURL());

            Style[] stylearray = stylereader.readXML();

            //Style style = stylearray[0];

//默认的方式

            Style style = SLD.createSimpleStyle(featureSource.getSchema());

            SLD.setPolyColour(style, Color.RED );

            Layer layer = new FeatureLayer(featureSource, style);

            map.addLayer(layer);

        }

        catch(Exception e){
            e.printStackTrace();

        }

    }

    /**

     * 根据四至、长、宽获取地图内容,并生成图片

     * @param paras

     * @param imgPath

     */

    public void getMapContent(Map paras, String imgPath){
        try{
            double[] bbox = (double[]) paras.get("bbox");

            double x1 = bbox[0], y1 = bbox[1],

                    x2 = bbox[2], y2 = bbox[3];

            int width = (int) paras.get("width"),

                    height=(int) paras.get("height");

// 设置输出范围

            CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;

            ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, x2, y1, y2, crs);

// 初始化渲染器

            StreamingRenderer sr = new StreamingRenderer();

            sr.setMapContent(map);

// 初始化输出图像

            BufferedImage bi = new BufferedImage(width, height,

                    BufferedImage.TYPE_INT_ARGB);

            Graphics g = bi.getGraphics();

            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                    RenderingHints.VALUE_ANTIALIAS_ON);

            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            Rectangle rect = new Rectangle(0, 0, width, height);

// 绘制地图

            sr.paint((Graphics2D) g, rect, mapArea);

//将BufferedImage变量写入文件中。

            ImageIO.write(bi,"png",new File(imgPath));

        }

        catch(Exception e){
            e.printStackTrace();

        }

    }
}

注意:编码使用GB18030解决中文标注问题

第三步调用代码编写:

  @GetMapping("/exportMap")
    @ResponseBody
    public String exportMap()
    {
        long start = System.currentTimeMillis();
        Shape2Image shp2img = new Shape2Image();
        String shpPath = JeeThinkConfig.getRegionpath()+"sxj/"+"51.shp";
        String sldPath = JeeThinkConfig.getRegionpath()+"sxj/"+"51.sld";
        String shpPath1 = JeeThinkConfig.getRegionpath()+"sxj/"+"qx.shp";
        String sldPath1 = JeeThinkConfig.getRegionpath()+"sxj/"+"qx.sld";
        String shpPath5 = JeeThinkConfig.getRegionpath()+"sxj/"+"legendLabel.shp";
        String sldPath5 = JeeThinkConfig.getRegionpath()+"sxj/"+"legendLabel.sld";
        String sldPath2 = JeeThinkConfig.getRegionpath()+"sxj/"+"warnR.sld";
        String shpPath3 = JeeThinkConfig.getRegionpath()+"sxj/"+"title.shp";
        String sldPath3 = JeeThinkConfig.getRegionpath()+"sxj/"+"title.sld";
        String shpPath6 = JeeThinkConfig.getRegionpath()+"sxj/"+"subtitle.shp";
        String sldPath6 = JeeThinkConfig.getRegionpath()+"sxj/"+"subtitle.sld";
        String shpPath4 = JeeThinkConfig.getRegionpath()+"sxj/"+"LegendP.shp";
        String sldPath4 = JeeThinkConfig.getRegionpath()+"sxj/"+"LegendP.sld";
        String imgPath = JeeThinkConfig.getRegionpath()+"png/"+"warn3.png";
        String geots="";
        WarnGeoInfo warnGeoInfo=new WarnGeoInfo();

        warnGeoInfo.setFilename("G_202104020824024.json");
        List<WarnGeoInfo> list = warnGeoInfoService.selectWarnGeoInfoList(warnGeoInfo);
        if(list.size()>0)
        {
            geots= list.get(0).getGeojson();
        }



        FileFormat.geojson2Shape(geots,"H:\\scdz\\sxj\\warn2.shp");

        Map paras = new HashMap();

        double[] bbox = new double[]{97.347607,25.513332,108.542384,36.119336};

        paras.put("bbox", bbox);

        paras.put("width", 1034);

        paras.put("height", 862);

        shp2img.addShapeLayer(shpPath3, sldPath3);
        shp2img.addShapeLayer(shpPath6, sldPath6);
        shp2img.addShapeLayer(shpPath4, sldPath4);


//        shp2img.addShapeLayer(GeoJSONUtil.creatFeatureSourceByCollection(GeoJSONUtil.readGeoJsonByString(geots)), sldPath2);
        shp2img.addShapeLayer("H:\\scdz\\sxj\\warn2.shp", sldPath2);
        shp2img.addShapeLayer(shpPath, sldPath);


        shp2img.addShapeLayer(shpPath1, sldPath1);
        shp2img.addShapeLayer(shpPath5, sldPath5);


        shp2img.getMapContent(paras, imgPath);

        return "图片生成完成,共耗时"+(System.currentTimeMillis() - start)+"ms";

    }

第四步:查看导出效果图:

 

感谢支持技术分享,请扫码点赞支持:

技术合作交流qq:2401315930

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴诚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值