应用配置
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.lilei.geojson
wkt2geojson
0.0.1-SNAPSHOT
jar
wkt2geojson
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
org.geotools
gt-geojson
12.0
nexus2
internal nexus repository2
http://central.maven.org/maven2/
maven2-repository.dev.java.net
Java.net repository
http://download.java.net/maven/2
osgeo
Open Source Geospatial Foundation Repository
http://download.osgeo.org/webdav/geotools/
true
boundless
Boundless Maven Repository
http://repo.boundlessgeo.com/main
程序
package com.lilei.geojson.wkt2geojson;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.geotools.geojson.geom.GeometryJSON;
import org.json.simple.JSONObject;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKTReader;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
String wkt = "POINT(114.298456 30.553544)";
Map map = new HashMap();
map.put("name", "北清路");
map.put("author","李雷");
String json = geoToJson(wkt,map);
System.out.println(jsonToWkt(json));
}
//
public static String geoToJson(String wkt,Map map) {
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);
map.put("geometry", writer);
json = JSONObject.toJSONString(map);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
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;
}
}