概述:java
本文講解經過Geotools實現用shp和sld導出圖片。git
實現:github
生成後的圖片web
實現代碼:ssh
package com.lzugis.geotools; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; 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.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.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("GBK"); 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(); } } /** * 根據四至、長、寬獲取地圖內容,並生成圖片 * @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(); } } /** * 工具類測試方法 * @param args */ public static void main(String[] args){ long start = System.currentTimeMillis(); Shape2Image shp2img = new Shape2Image(); String shpPath = "D:\\data\\beijing\\China4326.shp"; String sldPath = "D:\\data\\beijing\\China4326.sld"; String shpPath1 = "D:\\data\\gdal\\university.shp"; String sldPath1 = "D:\\data\\gdal\\university.sld"; String imgPath = "D:\\data\\beijing\\China4326.png"; Map paras = new HashMap(); double[] bbox = new double[]{73.30078125,10.634765625,140.80078125,55.107421875}; paras.put("bbox", bbox); paras.put("width", 768); paras.put("height", 506); shp2img.addShapeLayer(shpPath, sldPath); shp2img.addShapeLayer(shpPath1, sldPath1); shp2img.getMapContent(paras, imgPath); System.out.println("圖片生成完成,共耗時"+(System.currentTimeMillis() - start)+"ms"); } }
一、本文未解決中文標註的問題。工具
二、出圖是須要結合地圖分辨率的。測試
---------------------------------------------------------------------------------------------------------------編碼
技術博客spa
CSDN:http://blog.csdn.NET/gisshixisheng.net
博客園:http://www.cnblogs.com/lzugis/
在線教程
http://edu.csdn.Net/course/detail/799
Github
https://github.com/lzugis/
聯繫方式
q q:1004740957
e-mail:niujp08@qq.com
公衆號:lzugis15
Q Q 羣:452117357(webgis)
337469080(Android)