1、单个类型转换
一次只能转换出一个 Geometry
geojson中只有一个 Feature 的情况
import com.fasterxml.jackson.core.JsonProcessingException;import lombok.Data;import org.locationtech.jts.geom.*;import org.locationtech.jts.io.ParseException;import org.locationtech.jts.io.WKTReader;import java.util.List;import java.util.Map;/*** Created on 2021/9/10--20:08.** @author fengyuhao* @Description GeoJson 转 Geometry*/public class GeoJsonToGeometry {private static final int SRID = 4326;private static final String TYPES = "Point,LineString,Polygon,MultiPoint,MultiLineString,MultiPolygon";/*** 将 GeoJson 转换为对应的 Geometry 类型,默认 srid = 4326* @param geoJson geoJson*/public static GeoJson getGeometryByGeoJson(String geoJson,String type) throws Exception {return getGeometryByGeoJson(geoJson,type,SRID);}/*** 支持转换所有类型,将 GeoJson 转换为对应的 Geometry 类型,默认 srid = 4326* @param geoJson geoJson*/public static GeoJson getGeometryByGeoJson(String geoJson) throws Exception {return getGeometryByGeoJson(geoJson,TYPES,SRID);}/*** 支持转换所有类型,将 GeoJson 转换为对应的 Geometry 类型* @param geoJson geoJson*/public static GeoJson getGeometryByGeoJson(String geoJson,int srid) throws Exception {return getGeometryByGeoJson(geoJson,TYPES,srid);}/*** 将 GeoJson 转换为对应的 Geometry 类型* @param geoJson geoJson* @param srid SRID*/public static GeoJson getGeometryByGeoJson(String geoJson,String type,int srid) throws Exception {List<Map<String, Object>> list = null;try {list = (List<Map<String, Object>>) OtherUtil.OBJECT_MAPPER.readValue(geoJson, Map.class).get("features");} catch (JsonProcessingException e) {e.printStackTrace();}if (list == null) {throw new Exception("Json 转换异常");}// 获取属性信息Map<String, Object> properties = (Map<String, Object>)list.get(0).get("properties");GeoJson geo = getProperties(properties);// 获取 geometryMap<String, Object> geometry = (Map<String, Object>)list.get(0).get("geometry");if(!type.toUpperCase().contains(geometry.get("type").toString().toUpperCase())){throw new Exception("不支持的 Geometry 类型");}switch (geometry.get("type").toString().toUpperCase()) {case "POINT":geo.setGeom(getPoint((List<Double>) geometry.get("coordinates"),srid));break;case "LINESTRING":geo.setGeom(getLineString((List<Object>) geometry.get("coordinates"),srid));break;case "POLYGON":geo.setGeom(getPolygon((List<Object>) geometry.get("coordinates"),srid));break;case "MULTIPOINT":geo.setGeom(getMultiPoint((List<Object>) geometry.get("coordinates"),srid));break;case "MULTILINESTRING":geo.setGeom(getMultiLineString((List<Object>) geometry.get("coordinates"),srid));break;case "MULTIPOLYGON":geo.setGeom(getMultiPolygon((List<Object>) geometry.get("coordinates"),srid));break;default:throw new Exception("不支持的 Geometry 类型");}return geo;}/*** [[* [122.19794471743876,30.241048024596722],[122.19349009648414,30.235207526854044],* [122.20655698914918,30.23401962566707],[122.19794471743876,30.241048024596722]* ]]* POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,* -71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))*/private static Polygon getPolygon(List<Object> coordinates,int srid) throws ParseException {coordinates = (List<Object>)coordinates.get(0);StringBuilder str = new StringBuilder();str.append("POLYGON((");List<Double> coordinate;for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("))");Polygon polygon = (Polygon) new WKTReader().read(str.toString());polygon.setSRID(srid);return polygon;}/*** [[119.001,34.001],[120.001,35.001]]* LINESTRING(1 2, 3 4)*/private static LineString getLineString(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();List<Double> coordinate;str.append("LINESTRING(");for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append(")");LineString lineString = (LineString) new WKTReader().read(str.toString());lineString.setSRID(srid);return lineString;}/*** [119,34]* POINT(1 2)*/private static Point getPoint(List<Double> coordinates,int srid) throws ParseException {Point point = (Point) new WKTReader().read("POINT("+coordinates.get(0)+" "+coordinates.get(1)+")");point.setSRID(srid);return point;}/*** [ [122.19794471743876,30.241048024596722],[122,30] ]* MULTIPOINT(1 2, 3 4)*/private static MultiPoint getMultiPoint(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();List<Double> coordinate;str.append("MultiPoint(");for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append(")");MultiPoint multiPoint = (MultiPoint) new WKTReader().read(str.toString());multiPoint.setSRID(srid);return multiPoint;}/*** [* [[119,35],[120,35]],* [[119,34],[122,34]]* ]* MULTILINESTRING((1 2, 3 4), (4 5, 6 7))*/private static MultiLineString getMultiLineString(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();str.append("MULTILINESTRING(");List<Double> coordinate;for(Object obj : coordinates){str.append("(");for(Object data : (List<Object>)obj){coordinate = (List<Double>) data;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("),");}str.deleteCharAt(str.length() -1).append(")");MultiLineString multiLineString = (MultiLineString) new WKTReader().read(str.toString());multiLineString.setSRID(srid);return multiLineString;}/*** [* [[ [122,30],[123,30],[122,31],[122,30] ]],* [[ [122,33],[123,33],[122.5,34],[122,33] ]]* ]* MULTIPOLYGON((* (0 0 1,20 0 1,20 20 1,0 20 1,0 0 1),* (5 5 3,5 7 3,7 7 3,7 5 3,5 5 3)* ))*/private static MultiPolygon getMultiPolygon(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();str.append("MULTIPOLYGON((");List<Double> coordinate;for(Object obj1 : coordinates){for(Object obj2 :(List<Object>)obj1){str.append("(");for(Object obj3 : (List<Object>)obj2){coordinate = (List<Double>) obj3;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("),");}}str.deleteCharAt(str.length() - 1).append("))");MultiPolygon multiPolygon = (MultiPolygon) new WKTReader().read(str.toString());multiPolygon.setSRID(srid);return multiPolygon;}private static GeoJson getProperties(Map<String, Object> properties) {GeoJson geoJson = new GeoJson();if(properties == null){return geoJson;}geoJson.setName(properties.getOrDefault("name","").toString());return geoJson;}@Datapublic static class GeoJson{private Geometry geom;private String name;}public static void main(String[] args) throws Exception {String geoPoint = "{\"type\": \"FeatureCollection\"," +"\"features\": [{" +"\"type\": \"Feature\"," +"\"geometry\": {\"type\": \"Point\"," +"\"coordinates\": [119,34]}," +"\"properties\": null" +"}]" +"}";String geoLineString = "{\"type\": \"FeatureCollection\"," +"\"features\": [{" +"\"type\": \"Feature\"," +"\"geometry\": {\"type\": \"LineString\"," +"\"coordinates\": [[119.001,34.001],[120.001,35.001]]}," +"\"properties\":{\"name\":\"names\"}" +"}]" +"}";String geoPolygon = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"Polygon\"," +"\"coordinates\":[[ [122.19794471743876,30.241048024596722],[122.19349009648414,30.235207526854044]," +"[122.20655698914918,30.23401962566707],[122.19794471743876,30.241048024596722] ]]}," +"\"properties\":{\"name\":\"names\"}" +"}]" +"}";String geoMultiPoint = "{\"type\": \"FeatureCollection\"," +"\"features\": [{" +"\"type\": \"Feature\"," +"\"geometry\": { \"type\": \"MultiPoint\"," +"\"coordinates\": [[122.19794471743876,30.241048024596722],[122,30]]}," +"\"properties\": null" +"}]" +"}";String geoMultiLineString = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"MultiLineString\"," +"\"coordinates\":[[[119,35],[120,35]],[[119,34],[122,34]]]}," +"\"properties\":null" +"}]" +"}";String geoMultiPolygon = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"MultiPolygon\"," +"\"coordinates\":[" +"[[ [122,30],[123,30],[122,31],[122,30] ]]," +"[[ [122,33],[123,33],[122.5,34],[122,33] ]]" +"]" +"}," +"\"properties\":{\"id\":\"10\"}" +"}]" +"}";GeoJson geoJson = getGeometryByGeoJson(geoLineString,"Polygon,LineString",4326);System.out.println(geoJson.getGeom());}}
2、多类型转换
geojson 文件中有几个 Feature 就转换出多少 Geometry
package com.zjic.common.utils;import com.fasterxml.jackson.core.JsonProcessingException;import lombok.Data;import org.locationtech.jts.geom.*;import org.locationtech.jts.io.ParseException;import org.locationtech.jts.io.WKTReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/*** Created on 2021/9/10--20:08.** @author fengyuhao* @Description GeoJson 转 Geometry, 仅支持读取 properties 中的 "name" 属性*/public class GeoJsonToGeometry {private static final int SRID = 4326;private static final String TYPES = "Point,LineString,Polygon,MultiPoint,MultiLineString,MultiPolygon";/*** 将 GeoJson 转换为对应的 Geometry 类型,默认 srid = 4326* @param geoJson geoJson* @param type geoJson 类型*/public static List<GeoJson> getGeometryByGeoJson(String geoJson,String type) throws Exception {return getGeometryByGeoJson(geoJson,type,SRID);}/*** 支持转换所有类型,将 GeoJson 转换为对应的 Geometry 类型,默认 srid = 4326* @param geoJson geoJson*/public static List<GeoJson> getGeometryByGeoJson(String geoJson) throws Exception {return getGeometryByGeoJson(geoJson,TYPES,SRID);}/*** 支持转换所有类型,将 GeoJson 转换为对应的 Geometry 类型* @param geoJson geoJson* @param srid srid*/public static List<GeoJson> getGeometryByGeoJson(String geoJson,int srid) throws Exception {return getGeometryByGeoJson(geoJson,TYPES,srid);}/*** 将 GeoJson 转换为对应的 Geometry 类型* @param geoJson geoJson* @param srid SRID*/public static List<GeoJson> getGeometryByGeoJson(String geoJson,String type,int srid) throws Exception {List<Map<String, Object>> list;try {list = (List<Map<String, Object>>) CommonUtil.OBJECT_MAPPER.readValue(geoJson, Map.class).get("features");} catch (JsonProcessingException e) {throw new Exception("Json 转换异常");}if (list == null) {throw new Exception("Json 转换异常");}List<GeoJson> res = new ArrayList<>(list.size()+1);for(Map<String, Object> data : list){// 获取属性信息Map<String, Object> properties = (Map<String, Object>)data.getOrDefault("properties",new HashMap<>());GeoJson geo = getProperties(properties);// 获取 geometryMap<String, Object> geometry = (Map<String, Object>)data.get("geometry");if(!type.toUpperCase().contains(geometry.get("type").toString().toUpperCase())){throw new Exception("Geometry 类型错误:期望的类型为"+type);}switch (geometry.get("type").toString().toUpperCase()) {case "POINT":geo.setGeom(getPoint((List<Double>) geometry.get("coordinates"),srid));break;case "LINESTRING":geo.setGeom(getLineString((List<Object>) geometry.get("coordinates"),srid));break;case "POLYGON":geo.setGeom(getPolygon((List<Object>) geometry.get("coordinates"),srid));break;case "MULTIPOINT":geo.setGeom(getMultiPoint((List<Object>) geometry.get("coordinates"),srid));break;case "MULTILINESTRING":geo.setGeom(getMultiLineString((List<Object>) geometry.get("coordinates"),srid));break;case "MULTIPOLYGON":geo.setGeom(getMultiPolygon((List<Object>) geometry.get("coordinates"),srid));break;default:throw new Exception("不支持的 Geometry 类型");}res.add(geo);}return res;}/*** [[* [122.19794471743876,30.241048024596722],[122.19349009648414,30.235207526854044],* [122.20655698914918,30.23401962566707],[122.19794471743876,30.241048024596722]* ]]* POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,* -71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))*/private static Polygon getPolygon(List<Object> coordinates,int srid) throws ParseException {coordinates = (List<Object>)coordinates.get(0);StringBuilder str = new StringBuilder();str.append("POLYGON((");List<Double> coordinate;for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("))");Polygon polygon = (Polygon) new WKTReader().read(str.toString());polygon.setSRID(srid);return polygon;}/*** [[119.001,34.001],[120.001,35.001]]* LINESTRING(1 2, 3 4)*/private static LineString getLineString(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();List<Double> coordinate;str.append("LINESTRING(");for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append(")");LineString lineString = (LineString) new WKTReader().read(str.toString());lineString.setSRID(srid);return lineString;}/*** [119,34]* POINT(1 2)*/private static Point getPoint(List<Double> coordinates,int srid) throws ParseException {Point point = (Point) new WKTReader().read("POINT("+coordinates.get(0)+" "+coordinates.get(1)+")");point.setSRID(srid);return point;}/*** [ [122.19794471743876,30.241048024596722],[122,30] ]* MULTIPOINT(1 2, 3 4)*/private static MultiPoint getMultiPoint(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();List<Double> coordinate;str.append("MultiPoint(");for(Object obj : coordinates){coordinate = (List<Double>) obj;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append(")");MultiPoint multiPoint = (MultiPoint) new WKTReader().read(str.toString());multiPoint.setSRID(srid);return multiPoint;}/*** [* [[119,35],[120,35]],* [[119,34],[122,34]]* ]* MULTILINESTRING((1 2, 3 4), (4 5, 6 7))*/private static MultiLineString getMultiLineString(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();str.append("MULTILINESTRING(");List<Double> coordinate;for(Object obj : coordinates){str.append("(");for(Object data : (List<Object>)obj){coordinate = (List<Double>) data;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("),");}str.deleteCharAt(str.length() -1).append(")");MultiLineString multiLineString = (MultiLineString) new WKTReader().read(str.toString());multiLineString.setSRID(srid);return multiLineString;}/*** [* [[ [122,30],[123,30],[122,31],[122,30] ]],* [[ [122,33],[123,33],[122.5,34],[122,33] ]]* ]* MULTIPOLYGON((* (0 0 1,20 0 1,20 20 1,0 20 1,0 0 1),* (5 5 3,5 7 3,7 7 3,7 5 3,5 5 3)* ))*/private static MultiPolygon getMultiPolygon(List<Object> coordinates,int srid) throws ParseException {StringBuilder str = new StringBuilder();str.append("MULTIPOLYGON((");List<Double> coordinate;for(Object obj1 : coordinates){for(Object obj2 :(List<Object>)obj1){str.append("(");for(Object obj3 : (List<Object>)obj2){coordinate = (List<Double>) obj3;str.append(coordinate.get(0)).append(" ").append(coordinate.get(1)).append(",");}str.deleteCharAt(str.length() - 1).append("),");}}str.deleteCharAt(str.length() - 1).append("))");MultiPolygon multiPolygon = (MultiPolygon) new WKTReader().read(str.toString());multiPolygon.setSRID(srid);return multiPolygon;}private static GeoJson getProperties(Map<String, Object> properties) {GeoJson geoJson = new GeoJson();if(properties == null){return geoJson;}geoJson.setName(properties.getOrDefault("name","").toString());return geoJson;}@Datapublic static class GeoJson{private Geometry geom;private String name;}public static void main(String[] args) throws Exception {// String geoPoint = "{\"type\": \"FeatureCollection\"," +// "\"features\": [{" +// "\"type\": \"Feature\"," +// "\"geometry\": {\"type\": \"Point\"," +// "\"coordinates\": [119,34]}," +// "\"properties\": null" +// "}]" +// "}";String geoPoints = "{ \"type\": \"FeatureCollection\", \"features\": [ { \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 120.915783542, 30.882011484 ] }, \"properties\": { \"name\": \"魏塘站\", \"gid\": 1 } }, { \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 120.989985292, 30.952577374 ] }, \"properties\": { \"name\": \"红旗塘站\", \"gid\": 2 } }, { \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 120.835532968, 30.997386357 ] }, \"properties\": { \"name\": \"下甸庙站\", \"gid\": 3 } }, { \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [ 120.954403108, 31.019139073 ] }, \"properties\": { \"name\": \"太浦河站\", \"gid\": 4 } } ] }";String geoLineString = "{\"type\": \"FeatureCollection\"," +"\"features\": [{" +"\"type\": \"Feature\"," +"\"geometry\": {\"type\": \"LineString\"," +"\"coordinates\": [[119.001,34.001],[120.001,35.001]]}," +"\"properties\":{\"name\":\"names\"}" +"}]" +"}";String geoPolygon = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"Polygon\"," +"\"coordinates\":[[ [122.19794471743876,30.241048024596722],[122.19349009648414,30.235207526854044]," +"[122.20655698914918,30.23401962566707],[122.19794471743876,30.241048024596722] ]]}," +"\"properties\":{\"name\":\"names\"}" +"}]" +"}";String geoMultiPoint = "{\"type\": \"FeatureCollection\"," +"\"features\": [{" +"\"type\": \"Feature\"," +"\"geometry\": { \"type\": \"MultiPoint\"," +"\"coordinates\": [[122.19794471743876,30.241048024596722],[122,30]]}," +"\"properties\": null" +"}]" +"}";String geoMultiLineString = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"MultiLineString\"," +"\"coordinates\":[[[119,35],[120,35]],[[119,34],[122,34]]]}," +"\"properties\":null" +"}]" +"}";String geoMultiPolygon = "{\"type\":\"FeatureCollection\"," +"\"features\":[{" +"\"type\":\"Feature\"," +"\"geometry\":{\"type\":\"MultiPolygon\"," +"\"coordinates\":[" +"[[ [122,30],[123,30],[122,31],[122,30] ]]," +"[[ [122,33],[123,33],[122.5,34],[122,33] ]]" +"]" +"}," +"\"properties\":{\"id\":\"10\"}" +"}]" +"}";List<GeoJson> geoJson = getGeometryByGeoJson(geoPoints,"MultiPolygon",4326);System.out.println(geoJson);}}
