【问题标题】:Geotools fails to parse a GeoJSON file with custom propertiesGeotools 无法解析具有自定义属性的 GeoJSON 文件
【发布时间】:2017-08-09 17:12:39
【问题描述】:

我正在使用 geotools 17.2 来解析 GeoJSON 文件,如下所示:

try (FileInputStream is = new FileInputStream(routeFile)) {
    FeatureJSON io = new FeatureJSON();
    return io.readFeatureCollection(is);
}

我使用的 GeoJSON 文件如下:

{
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": { },
    "geometry": {
      "type": "Point",
      "coordinates": [125.6, 10.1]
      }
    }, {
      "type": "Feature",
      "properties": { "name": "test" },
      "geometry": {
        "type": "Point",
        "coordinates": [44.912109375, 53.64463782485651]
      }
    }
  ]
}

解析失败并显示以下错误消息/堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: No such attribute:name
    at org.geotools.feature.simple.SimpleFeatureBuilder.set(SimpleFeatureBuilder.java:288)
    at org.geotools.geojson.feature.FeatureHandler.endObject(FeatureHandler.java:176)
    at org.geotools.geojson.DelegatingHandler.endObject(DelegatingHandler.java:81)
    at org.geotools.geojson.feature.FeatureCollectionHandler.endObject(FeatureCollectionHandler.java:121)
    at org.json.simple.parser.JSONParser.parse(Unknown Source)
    at org.geotools.geojson.feature.FeatureJSON$FeatureCollectionIterator.readNext(FeatureJSON.java:746)

但是,如果我将 name 属性移动到第一点,则解析成功。有没有办法让 geotools 在自定义属性方面更加灵活? (或者这是库中的错误?)

【问题讨论】:

    标签: java geotools


    【解决方案1】:

    您需要使用readFeatureCollectionSchema 的第二种形式,它采用布尔值来强制geotools 读取整个文件以计算出架构。

    【讨论】:

    • 我想我可能过度简化了我正在使用的示例文件。除了点几何,GeoJSON 文件还包含 LineString 功能,因此当使用您建议的方法时(虽然没有解析错误),LineString 几何也呈现为点。你知道如何处理这样的 FeatureCollection 吗?
    • 嗯,显然它并没有我想象的那么简单。是否有关于如何解析具有多个几何图形的 GeoJSON 文件的示例,其中一些具有自定义属性?
    • 这确实是一个新问题,但请参阅 gis.stackexchange.com/questions/240709/… 示例
    • 但是如何使用这种方式确定的FeatureType从geojson文件中读取特征集合呢?
    • 使用新的 gt-GeoJSONDatastore 模块,现在完全不支持 gt-geojson
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2023-03-04
    • 2013-06-03
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2014-02-24
    • 2012-02-17
    相关资源
    最近更新 更多