微码:把 GeoJson 转成 GeometryCollection 类型
本文发布于 2024年05月08日,阅读 26 次,点赞
0 次,归类于
微码
引入 maven 依赖
该依赖在 maven 中央仓库中没有,需要另外配置下仓库
geotools库支持 jdk8 的最高版本是 28.2。
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson-core</artifactId>
<version>28.2</version>
</dependency>
<repositories>
<!-- geotools的远程库 -->
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<!-- geotools的远程库end -->
</repositories>
代码实现
package top.emanjusaka;
import org.geotools.data.geojson.GeoJSONReader;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
convertGeoJsonToGeometryCollection("{\n" +
"\t\"type\": \"FeatureCollection\",\n" +
"\t\"features\": [\n" +
"\t\t{\n" +
"\t\t\t\"type\": \"Feature\",\n" +
"\t\t\t\"geometry\": {\n" +
"\t\t\t\t\"type\": \"LineString\",\n" +
"\t\t\t\t\"coordinates\": [\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.38974542773435,\n" +
"\t\t\t\t\t\t27.479859763671875\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.52982111132812,\n" +
"\t\t\t\t\t\t27.334290916015625\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.3856255546875,\n" +
"\t\t\t\t\t\t27.485352927734375\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.51608820117188,\n" +
"\t\t\t\t\t\t27.350770408203125\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.52707452929688,\n" +
"\t\t\t\t\t\t27.33017104296875\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.38974542773435,\n" +
"\t\t\t\t\t\t27.279359275390625\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.75641412890624,\n" +
"\t\t\t\t\t\t27.31918471484375\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.44879694140624,\n" +
"\t\t\t\t\t\t27.199708396484375\n" +
"\t\t\t\t\t],\n" +
"\t\t\t\t\t[\n" +
"\t\t\t\t\t\t111.62320490039062,\n" +
"\t\t\t\t\t\t27.146150046875\n" +
"\t\t\t\t\t]\n" +
"\t\t\t\t]\n" +
"\t\t\t},\n" +
"\t\t\t\"properties\": null\n" +
"\t\t}\n" +
"\t]\n" +
"}");
}
private static GeometryCollection convertGeoJsonToGeometryCollection(String geoJsonStr) {
// 创建GeoJSONReader实例
try (GeoJSONReader geoJSONReader = new GeoJSONReader(geoJsonStr)) {
// 将GeoJSON字符串解析为FeatureCollection
SimpleFeatureCollection features = geoJSONReader.getFeatures();
SimpleFeatureIterator iterator = features.features();
List<Geometry> geometries = new ArrayList<>();
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry();
geometries.add(geometry);
}
GeometryCollection geometryCollection = new GeometryCollection(geometries.toArray(new Geometry[0]), new GeometryFactory());
return geometryCollection;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
在技术的星河中遨游,我们互为引路星辰,共同追逐成长的光芒。愿本文的洞见能触动您的思绪,若有所共鸣,请以点赞之手,轻抚赞同的弦。
原文地址: https://www.emanjusaka.top/2024/05/geojson-convert-geometryCollection
微信公众号:emanjusaka的编程栈