geo-tools系列(二) 是用geo-tools绘制GeoJSON地图
一、绘制MapContent到图片
参考: http://docs.geotools.org/latest/userguide/library/render/gtrenderer.html
生成图片的核心代码:
public void saveImage(final MapContent map, final String file, final int imageWidth) {
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
Rectangle imageBounds = null;
ReferencedEnvelope mapBounds = null;
try {
mapBounds = map.getMaxBounds();
double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
imageBounds = new Rectangle(
0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
} catch (Exception e) {
// failed to access map layers
throw new RuntimeException(e);
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gr = image.createGraphics();
gr.setPaint(Color.WHITE);
gr.fill(imageBounds);
try {
renderer.paint(gr, imageBounds, mapBounds);
File fileToSave = new File(file);
ImageIO.write(image, "jpeg", fileToSave);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
上述代码中涉及MapContent对象,需要文档中查看MapContent的相关内容
二、MapContent
文档地址:
http://docs.geotools.org/latest/userguide/library/render/map.html
MapContent是用来捕获和渲染地图,MapContent的相关类图如下:
由类之间的关系可以看出,Layer是要绘制地图时添加的图层信息,MapViewport是对地图视点控制相关的类,现在去查看Layer
三、Layer
查看Layer的类图
layer抽象类中的方法
综上可知需要构建一个FeatureLayer来绘制geojson中的数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tovmNrew-1571199253970)(http://docs.geotools.org/latest/userguide/_images/feature_layer.PNG)]
这里使用FeatureLayer(FeatureCollection,Style)的构造器,构造FeatureCollection
四、构建FeatureLayer
4.1 GeoJSON转换为FeatureCollection
参考:
https://blog.csdn.net/aliasone/article/details/80186301
https://blog.csdn.net/aliasone/article/details/80218540
代码如下:
/** * * 绘制有孔洞的多边形 * @param geojson * @return: void * @throws */
public static void deawMultiPolygonWithHoleFromGeoJson(FeatureCollectionDTO<MultiPolygonWithHoleDTO> geojson) {
String json = JSONObject.toJSONString(geojson);
FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(15));
try {
FeatureCollection featureCollection = featureJSON.readFeatureCollection(json);
// 获取simpleFeatureType
SimpleFeatureType simpleFeatureType = (SimpleFeatureType) featureCollection.getSchema();
try {
// 由坐标顺序引发坐标变换
String crs = CRS.lookupIdentifier(simpleFeatureType.getCoordinateReferenceSystem(), true);
try {
featureCollection = new ForceCoordinateSystemFeatureResults(featureCollection,
CRS.decode(crs, true));
} catch (SchemaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建map对象
MapContent map = new MapContent();
// 创建featurelayer
// StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
// SLDParser sldParser = new SLDParser(styleFactory, new File("d://depth.sld"));
// Style style = sldParser.readXML()[0];
Style style = SLD.createLineStyle(new Color(0x1890ff), 2);
FeatureLayer layer = new FeatureLayer(featureCollection, style);
// 创建要输出的文件
String saveFile = "D://saveImage.jpg";
// 保存geojson到图片
MapViewport viewPoint = new MapViewport();
ReferencedEnvelope bounds = layer.getBounds();
viewPoint.setBounds(bounds);
map.addLayer(layer);
map.setViewport(viewPoint);
saveMapToImage(map, saveFile, 1000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.2 绘制mapContent
public static void saveMapToImage(final MapContent map, final String file, final int imageWidth) {
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
Rectangle imageBounds = null;
ReferencedEnvelope mapBounds = null;
try {
mapBounds = map.getMaxBounds();
double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
} catch (Exception e) {
// failed to access map layers
throw new RuntimeException(e);
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gr = image.createGraphics();
gr.setPaint(Color.WHITE);
gr.fill(imageBounds);
try {
renderer.paint(gr, imageBounds, mapBounds);
File fileToSave = new File(file);
ImageIO.write(image, "jpeg", fileToSave);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
运行结果如下:
