了解geojson:GeoJSON格式规范说明
导入jar包:
gt-geojson-9.3.jar,json-simple-1.1.1.jar,jts-1.8.jar
1.由wkt格式的geometry生成geojson:
/**
* 由wkt格式的geometry生成geojson
* @param wkt
* @return
*/
public static String geoToJson(String wkt){
String json = null;
try{
WKTReader reader = new WKTReader();
Geometry geometry = reader.read(wkt);
StringWriter writer = new StringWriter();
GeometryJSON g = new GeometryJSON();
g.write(geometry,writer);
json = writer.toString();
}catch(Exception e){
e.printStackTrace();
}
return json;
}
2.将geojson解析为geometry的wkt形式
/**
* 将geojson解析为geometry的wkt形式
* @param geoJson
* @return
*/
public static String jsonToWkt(String geoJson){
String wkt = null;
GeometryJSON gjson = new GeometryJSON();
Reader reader = new StringReader(geoJson);
try{
Geometry geometry = gjson.read(reader);
wkt = geometry.toText();
}catch(IOException e){
e.printStackTrace();
}
return wkt;
}