第一步:
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