3

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

Krassi
  • 2,336
  • 3
  • 22
  • 30

1 Answers1

0

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.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • I think I might have oversimplified the example file that I was using. In addition to the Point geometry, the GeoJSON file also contains LineString features, so when using the approach that you suggested (while there are no parsing errors), the LineString geometries are also rendered as Points. Do you have an idea how to handle such a FeatureCollection? – Krassi Aug 10 '17 at 11:17
  • Hm, apparently it is not that straightforward as I imagined. Is there an example on how to parse a GeoJSON file with multiple geometries, with some of them having custom properties? – Krassi Aug 11 '17 at 11:57
  • That's really a new question but see https://gis.stackexchange.com/questions/240709/geoserver-layer-styling-with-multiple-geometries for an example – Ian Turton Aug 11 '17 at 12:33
  • But how to use FeatureType determined this way to read a feature collection from a geojson file? – Barbra Mar 19 '20 at 14:42
  • Use the new gt-GeoJSONDatastore module, the gt-geojson is completely unsupported now – Ian Turton Mar 19 '20 at 15:01
  • Hmm, gt-GeoJSONDatastore (22.3) has dependency on gt-geojson So what is actually the difference between these two and how to read feature collection avoiding the "No such attribute" issue with gt-GeoJSONDatastore? I noticed gt-GeoJSONDatastore calls gt-geojson methods anyway (`GeoJSONReader` class of gt-GeoJSONDatastore). – Barbra Mar 20 '20 at 12:54
  • Look at 23.x for the new version – Ian Turton Mar 20 '20 at 13:26
  • But 23 is RC :) So there's no stable version containing this functionality yet? How does RC get maintained (updated in artifacts repository, for example)? – Barbra Mar 20 '20 at 14:36
  • You are looking at an unsupported module so it gets updated when ever I (or someone else) feels like it – Ian Turton Mar 20 '20 at 15:03