赞
踩
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.vividsolutions.jts.geom.Geometry;
- import com.vividsolutions.jts.geom.GeometryCollection;
- import com.vividsolutions.jts.geom.GeometryFactory;
- import com.vividsolutions.jts.io.WKTReader;
- import org.geotools.geojson.GeoJSONUtil;
- import org.geotools.geojson.geom.GeometryJSON;
-
- import java.io.IOException;
- import java.io.Reader;
- import java.io.StringWriter;
-
- public class WKTutil {
- private static WKTReader reader = new WKTReader();
-
- private static final String GEO_JSON_TYPE = "GeometryCollection";
-
- private static final String WKT_TYPE = "GEOMETRYCOLLECTION";
-
- public static String wktToJson(String wkt) {
- String json = null;
- try {
- WKTReader reader = new WKTReader();
- Geometry geometry = reader.read(wkt);
- StringWriter writer = new StringWriter();
- GeometryJSON g = new GeometryJSON(20);
- g.write(geometry, writer);
- json = writer.toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return json;
- }
-
- public static String jsonToWkt(JSONObject jsonObject) {
- String wkt = null;
- String type = jsonObject.getString("type");
- GeometryJSON gJson = new GeometryJSON();
- try {
- // {"geometries":[{"coordinates":[4,6],"type":"Point"},{"coordinates":[[4,6],[7,10]],"type":"LineString"}],"type":"GeometryCollection"}
- if (WKTutil.GEO_JSON_TYPE.equals(type)) {
- // 由于解析上面的json语句会出现这个geometries属性没有采用以下办法
- JSONArray geometriesArray = jsonObject.getJSONArray("geometries");
- // 定义一个数组装图形对象
- int size = geometriesArray.size();
- Geometry[] geometries = new Geometry[size];
- for (int i = 0; i < size; i++) {
- String str = geometriesArray.get(i).toString();
- // 使用GeoUtil去读取str
- Reader reader = GeoJSONUtil.toReader(str);
- Geometry geometry = gJson.read(reader);
- geometries[i] = geometry;
- }
- GeometryCollection geometryCollection = new GeometryCollection(geometries, new GeometryFactory());
- wkt = geometryCollection.toText();
- } else {
- Reader reader = GeoJSONUtil.toReader(jsonObject.toString());
- Geometry read = gJson.read(reader);
- wkt = read.toText();
- }
-
-
- } catch (IOException e) {
- System.out.println("GeoJson转WKT出现异常");
- e.printStackTrace();
- }
- return wkt;
- }
-
- }
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-shapefile</artifactId>
- <version>19.2</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-swing</artifactId>
- <version>19.2</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-jdbc</artifactId>
- <version>19.2</version>
- </dependency>
- <dependency>
- <groupId>org.geotools.jdbc</groupId>
- <artifactId>gt-jdbc-postgis</artifactId>
- <version>19.2</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-epsg-hsql</artifactId>
- <version>19.2</version>
- </dependency>
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-geojson</artifactId>
- <version>12.0</version>
- </dependency>
- <dependency>
- <groupId>com.vividsolutions</groupId>
- <artifactId>jts</artifactId>
- <version>1.13</version> <!--2-->
- <exclusions>
- <exclusion>
- <groupId>xerces</groupId>
- <artifactId>xercesImpl</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- @RequestMapping(value = "/text", method = RequestMethod.POST)
- public String text(@RequestBody Object geom) {
- JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(geom));
- return WKTutil.jsonToWkt(jsonObject);
- }
参数
{ "geom": { "type": "MultiPolygon", "coordinates": [ [ [ [ -122.2694982, 37.79045922 ], [ -122.2693624, 37.79041125 ], [ -122.2693518, 37.79042521 ], [ -122.26899, 37.7902858 ], [ -122.2690027, 37.79027181 ], [ -122.2688602, 37.79021705 ], [ -122.2687222, 37.790445 ], [ -122.2688582, 37.79049813 ], [ -122.2689084, 37.79041634 ], [ -122.2689473, 37.79041058 ], [ -122.2691974, 37.79051029 ], [ -122.2692367, 37.7906097 ], [ -122.2692201, 37.79064271 ], [ -122.2693538, 37.79069243 ], [ -122.2694982, 37.79045922 ] ] ] ] } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。