gpt4 book ai didi

java - 使用 Java Topology Suite 或 GeoTools 解析 GeoJSON 文件

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:25 25 4
gpt4 key购买 nike

例如,如果您有一个像这样的带有多边形的 GeoJSON 文件(用于测试的简单文件)

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-4.658203125,
41.343824581185686
],
[
-5.6689453125,
39.13006024213511
],
[
-1.9335937499999998,
39.16414104768742
],
[
-1.3623046875,
41.21172151054787
],
[
-4.658203125,
41.343824581185686
]
]
]
}
}
]
}

要点:

Geometry point2 = new WKTReader().read("POINT (-3.2958984375 40.44694705960048)");

并且您想在程序中加载 geoJSON 文件以测试此多边形是否包含点,您如何使用 JTS 在 Java 中完成?


其他选项可以将 GeoTools 与 GeoJson 一起使用插件,但我无法解析 GeoJson 文件


我尝试过的

使用像 this 这样的 GEOTOOLS

String content = new String(Files.readAllBytes(Paths.get("file.geojson")), "UTF-8");
GeometryJSON gjson = new GeometryJSON();
Reader reader = new StringReader(content);
Polygon p = gjson.readPolygon(reader);
System.out.println("polygon: " + p);

这里的问题是多边形 p 只包含 geojson 文件的最后一个多边形。如果这个文件有很多多边形,我应该如何解析它?

this一样使用JTS2GEOJSON

String content = new String(Files.readAllBytes(Paths.get("file.geojson")), "UTF-8");
System.out.println("content: " + content);
GeoJSONReader reader1 = new GeoJSONReader();
Geometry geometry = reader1.read(content);

此代码失败是这一行:

Geometry geometry = reader1.read(content);

出现此错误:

Exception in thread "main" java.lang.UnsupportedOperationException
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:51)
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:21)
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:16)

此错误是由于我正在尝试从 GeoJson 文件中读取 FeatureCollections。如果我尝试使用这个简单的字符串,它会起作用:

   String content = "{\n" +
" \"type\": \"Polygon\",\n" +
" \"coordinates\": [\n" +
" [\n" +
" [\n" +
" -4.141845703125,\n" +
" 40.9218144123785\n" +
" ],\n" +
" [\n" +
" -4.603271484375,\n" +
" 40.002371935876475\n" +
" ],\n" +
" [\n" +
" -3.5595703125,\n" +
" 39.757879992021756\n" +
" ],\n" +
" [\n" +
" -2.548828125,\n" +
" 40.43858586704331\n" +
" ],\n" +
" [\n" +
" -3.2080078125,\n" +
" 41.12074559016745\n" +
" ],\n" +
" [\n" +
" -4.141845703125,\n" +
" 40.9218144123785\n" +
" ]\n" +
" ]\n" +
" ]\n" +
" }";

最佳答案

如果您使用 GeoTools那么你需要开始考虑 DataStoresFeatureCollections .有很多内置智能(大部分使用 JTS)来处理 filtering等等,如果出现潜在问题,这可以为您节省很多。

File inFile = new File("/home/ian/Data/states/states.geojson");
Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(inFile));
DataStore newDataStore = DataStoreFinder.getDataStore(params);
String pt = "POINT (-107 42)";

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(newDataStore.getTypeNames()[0]);
Filter f = ff.contains(ff.property(featureSource.getSchema().getGeometryDescriptor().getLocalName()),
ff.literal(pt));
SimpleFeatureCollection collection = featureSource.getFeatures(f);
if (collection.size() > 0) {
try (SimpleFeatureIterator itr = collection.features()) {
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}

这将需要在您的 pom.xml 中包含以下内容:

<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojsondatastore</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>

关于java - 使用 Java Topology Suite 或 GeoTools 解析 GeoJSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53957417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com