Geom与wkt互转 java

------------恢复内容开始------------

1.设置maven

<!-- include central so that it is searched before our alternate repos -->
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>locationtech-releases</id>
<url>https://repo.locationtech.org/content/groups/releases</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!-- geotools -->
<repository>
<id>boundlessgeo</id>
<url>https://repo.boundlessgeo.com/main</url>
</repository>
<!-- jai -->
<repository>
<id>osgeo</id>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
<repository>
<id>geomajas</id>
<url>http://maven.geomajas.org/</url>
</repository>
View Code

 

2.maven依赖

 1 <dependency>
 2 <groupId>org.geotools</groupId>
 3 <artifactId>gt-shapefile</artifactId>
 4 <version>19.2</version>
 5 </dependency>
 6 <dependency>
 7 <groupId>org.geotools</groupId>
 8 <artifactId>gt-swing</artifactId>
 9 <version>19.2</version>
10 </dependency>
11 <dependency>
12 <groupId>org.geotools</groupId>
13 <artifactId>gt-jdbc</artifactId>
14 <version>19.2</version>
15 </dependency>
16 <dependency>
17 <groupId>org.geotools.jdbc</groupId>
18 <artifactId>gt-jdbc-postgis</artifactId>
19 <version>19.2</version>
20 </dependency>
21 <dependency>
22 <groupId>org.geotools</groupId>
23 <artifactId>gt-epsg-hsql</artifactId>
24 <version>19.2</version>
25 </dependency>
26 <dependency>
27 <groupId>org.geotools</groupId>
28 <artifactId>gt-geojson</artifactId>
29 <version>12.0</version>
30 </dependency>
31 <dependency>
32 <groupId>com.vividsolutions</groupId>
33 <artifactId>jts</artifactId>
34 <version>1.13</version> <!--2-->
35 <exclusions>
36 <exclusion>
37 <groupId>xerces</groupId>
38 <artifactId>xercesImpl</artifactId>
39 </exclusion>
40 </exclusions>
41 </dependency>
View Code

 

3.GeomUtil

 1 public class GeomUtil {
 2 
 3 private static WKTReader reader = new WKTReader();
 4 
 5 private static final String GEO_JSON_TYPE = "GeometryCollection";
 6 
 7 private static final String WKT_TYPE = "GEOMETRYCOLLECTION";
 8 
 9 public static String wktToJson(String wkt) {
10 String json = null;
11 try {
12 WKTReader reader = new WKTReader();
13 Geometry geometry = reader.read(wkt);
14 StringWriter writer = new StringWriter();
15 GeometryJSON g = new GeometryJSON(20);
16 g.write(geometry, writer);
17 json = writer.toString();
18 } catch (Exception e) {
19 e.printStackTrace();
20 }
21 return json;
22 }
23 
24 public static String jsonToWkt(JSONObject jsonObject){
25 if(jsonObject==null){
26 return new String();
27 }
28 String wkt = null;
29 String type = jsonObject.getString("type");
30 GeometryJSON gJson = new GeometryJSON();
31 try {
32 if (GEO_JSON_TYPE.equals(type)) {
33 // 由于解析上面的json语句会出现这个geometries属性没有采用以下办法
34 JSONArray geometriesArray = jsonObject.getJSONArray("geometries");
35 // 定义一个数组装图形对象
36 int size = geometriesArray.size();
37 Geometry[] geometries = new Geometry[size];
38 for (int i = 0; i < size; i++) {
39 String str = geometriesArray.get(i).toString();
40 // 使用GeoUtil去读取str
41 Reader reader = GeoJSONUtil.toReader(str);
42 Geometry geometry = gJson.read(reader);
43 geometries[i] = geometry;
44 }
45 GeometryCollection geometryCollection = new GeometryCollection(geometries, new GeometryFactory());
46 wkt = geometryCollection.toText();
47 } else {
48 Reader reader = GeoJSONUtil.toReader(jsonObject.toString());
49 Geometry read = gJson.read(reader);
50 wkt = read.toText();
51 }
52 } catch (IOException e) {
53 System.out.println("GeoJson转WKT出现异常");
54 e.printStackTrace();
55 }
56 return wkt;
57 }
58 }
View Code

 

参考自:

https://www.cnblogs.com/tuxiaoer/p/15594113.html

https://blog.csdn.net/m0_61228138/article/details/122863897

------------恢复内容结束------------

posted @ 2022-06-15 10:51  闻长歌而知雅意  阅读(655)  评论(0编辑  收藏  举报