【发布时间】:2018-12-28 10:57:16
【问题描述】:
例如,如果您有一个像这样带有多边形的 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呢?
其他选项可以使用带有 GeoJson 插件的 GeoTools,但我无法解析 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 文件的最后一个多边形。如果这个文件有很多多边形,我应该如何解析它?
使用 JTS2GEOJSON,例如 this
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" +
" }";
【问题讨论】:
-
到目前为止你尝试了什么?
-
我在主帖@IanTurton 中添加了更多细节