Search code examples
javageotools

Geotools fails to parse a GeoJSON file with custom properties


I am using geotools 17.2 to parse a GeoJSON file as follows:

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

The GeoJSON file that I am using is the following:

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

The parsing fails with the following error message/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)

However if I move the name property to the first point, then the parsing is successful. Is there a way to make geotools a bit more flexible with respect to custom properties? (or is this a bug in the library?)


Solution

  • You need to use the second form of the readFeatureCollectionSchema that takes a boolean to force geotools to read the whole file to work out the schema.