Geom与wkt互转 java
2022/6/16 1:22:50
本文主要是介绍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
------------恢复内容结束------------
这篇关于Geom与wkt互转 java的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南