• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Geom与wkt互转 java

java 佚名 2年前 (2023-03-11) 74次浏览 已收录 0个评论

标签:gt java String reader Geom org 互转 new wkt

————恢复内容开始————

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&gt;
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>locationtech-releases</id>
<url>https://repo.locationtech.org/content/groups/releases</url&gt;
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!– geotools –>
<repository>
<id>boundlessgeo</id>
<url>https://repo.boundlessgeo.com/main</url&gt;
</repository>
<!– jai –>
<repository>
<id>osgeo</id>
<url>https://repo.osgeo.org/repository/release/</url&gt;
</repository>
<repository>
<id>geomajas</id>
<url>http://maven.geomajas.org/</url&gt;
</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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Geom与wkt互转 java

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址