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>
2.maven依赖
<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>
3.GeomUtil
public class GeomUtil {
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){
if(jsonObject==null){
return new String();
}
String wkt = null;
String type = jsonObject.getString("type");
GeometryJSON gJson = new GeometryJSON();
try {
if (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;
}
}
参考自:
https://www.cnblogs.com/tuxiaoer/p/15594113.html
https://blog.csdn.net/m0_61228138/article/details/122863897
------------恢复内容结束------------
标签:gt,java,String,reader,Geom,org,互转,new,wkt 来源: https://www.cnblogs.com/assistants/p/16377657.html