使用Java拓扑套件或GeoTools解析GeoJSON文件

时间:2018-12-28 10:57:16

标签: java geotools jts

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

col_2

要点:

{
  "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
            ]
          ]
        ]
      }
    }
  ]
}

您想在程序中加载geoJSON文件以测试该多边形中是否包含该点,如何使用JTS在Java中进行处理?


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


我尝试过的事情

使用this之类的GEOTOOLS

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

这里的问题是多边形p仅包含geojson文件的最后一个多边形。如果此文件包含许多多边形,应该如何解析?

使用this之类的JTS2GEOJSON

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);

此代码失败的是此行:

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);

此错误是由于我试图从GeoJson文件中读取FeatureCollections。如果我尝试使用以下简单字符串,它将起作用:

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)

2 个答案:

答案 0 :(得分:1)

使用GeoJsonReader类,您可以将GeoJson Geometry从JSON片段读取为Geometry:

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)

答案 1 :(得分:1)

如果您使用的是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>