需求:将geojson字符串转为java对象(jts),或将java对象转为geojson字符串
1、引入maven依赖(注意,此依赖由jdk11编译,所以如果你的版本是jdk8,需要自行下载源码编译,源码地址https://github.com/codingmiao/giscat)
<dependency>
<groupId>org.wowtools</groupId>
<artifactId>giscat-vector-pojo</artifactId>
<version>1.1.1-STABLE</version>
</dependency>
2、geojson to FeatureCollection
String strGeoJson = "{\"features\":[{\"geometry\":{\"coordinates\":[30,10],\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"id\":1}},{\"geometry\":{\"coordinates\":[[30,10],[10,30],[40,40]],\"type\":\"LineString\"},\"type\":\"Feature\",\"properties\":{\"name\":\"hello\"}}],\"type\":\"FeatureCollection\"}";
GeometryFactory geometryFactory = new GeometryFactory();// jts GeometryFactory
FeatureCollection featureCollection = GeoJsonFeatureConverter.fromGeoJsonFeatureCollection(strGeoJson, geometryFactory);
for (Feature feature : featureCollection.getFeatures()) {
System.out.println(feature.getGeometry());//POINT (30 10)
System.out.println(feature.getProperties());//{name=hello ...}
}
3、FeatureCollection to geojson
GeoJsonObject.FeatureCollection geoJson = GeoJsonFeatureConverter.toGeoJson(featureCollection);
System.out.println(geoJson.toGeoJsonString());
完整示例
package org.wowtools.giscat.vector.pojo.converter;
import org.locationtech.jts.geom.GeometryFactory;
import org.wowtools.giscat.vector.pojo.Feature;
import org.wowtools.giscat.vector.pojo.FeatureCollection;
import org.wowtools.giscat.vector.pojo.GeoJsonObject;
/**
* @author liuyu
* @date 2022/4/1
*/
public class Test1 {
public static void main(String[] args) throws Exception {
String strGeoJson = "{\"features\":[{\"geometry\":{\"coordinates\":[30,10],\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"id\":1}},{\"geometry\":{\"coordinates\":[[30,10],[10,30],[40,40]],\"type\":\"LineString\"},\"type\":\"Feature\",\"properties\":{\"name\":\"hello\"}}],\"type\":\"FeatureCollection\"}";
GeometryFactory geometryFactory = new GeometryFactory();// jts GeometryFactory
FeatureCollection featureCollection = GeoJsonFeatureConverter.fromGeoJsonFeatureCollection(strGeoJson, geometryFactory);
for (Feature feature : featureCollection.getFeatures()) {
System.out.println(feature.getGeometry());//POINT (30 10)
System.out.println(feature.getProperties());//{name=hello ...}
}
GeoJsonObject.FeatureCollection geoJson = GeoJsonFeatureConverter.toGeoJson(featureCollection);
System.out.println(geoJson.toGeoJsonString());
}
}
参考