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

Parse GeoJSON file with Java Topology Suite or GeoTools

例如,如果您有这样一个带有多边形的 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 文件以在该多边形中测试包含点,您如何在 Java 中使用 JTS 来完成?


其他选项可以将 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" +
                "      }";

使用 GeoJsonReader class 您可以将 JSON 片段中的 GeoJson 几何读入几何:

http://locationtech.github.io/jts/javadoc/org/locationtech/jts/io/geojson/GeoJsonReader.html

然后,测试该几何图形(geojson 多边形)是否包含参数几何图形(wkt 点):

http://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/Geometry.html#contains(org.locationtech.jts.geom.Geometry)

如果您正在使用 GeoTools then you need to start thinking in terms of DataStores and FeatureCollections. There is lots of built in smarts (much of it using JTS) to handle 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>