应用配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lilei.geojson</groupId> <artifactId>wkt2geojson</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wkt2geojson</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson --> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>12.0</version> </dependency> </dependencies> <repositories> <repository> <id>nexus2</id> <name>internal nexus repository2</name> <url>http://central.maven.org/maven2/</url> </repository> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net repository</name> <url>http://download.java.net/maven/2</url> </repository> <repository> <id>osgeo</id> <name>Open Source Geospatial Foundation Repository</name> <url>http://download.osgeo.org/webdav/geotools/</url> </repository> <repository> <snapshots> <enabled>true</enabled> </snapshots> <id>boundless</id> <name>Boundless Maven Repository</name> <url>http://repo.boundlessgeo.com/main</url> </repository> </repositories> </project>
程序
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<String,Object> map = new HashMap<String,Object>(); map.put("name", "北清路"); map.put("author","李雷"); String json = geoToJson(wkt,map); System.out.println(jsonToWkt(json)); } // public static String geoToJson(String wkt,Map<String,Object> 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; } }