Geotools无法使用自定义属性解析GeoJSON文件

时间:2017-08-09 17:12:40

标签: java geotools

我使用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]
      }
    }
  ]
}

解析失败,并显示以下错误消息/ stacktrace:

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在定制属性方面更灵活一些? (或者这是库中的错误吗?)

1 个答案:

答案 0 :(得分:0)

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