locationtech/spatial4j

Unable to parse GeoJSON when type is set after coordinates

mmadoo opened this issue · 1 comments

When executing following code:

GeoJSONReader geojsonReader = new GeoJSONReader(new SpatialContext(new SpatialContextFactory()), 
				new SpatialContextFactory());
System.out.println("Type first:" + geojsonReader.read("{\"type\":\"LineString\", \"coordinates\":[[2.287563,48.862743],[2.287862,48.862958],[2.288109,48.863045]]}"));
System.out.println("Type second:" + geojsonReader.read("{\"coordinates\":[[2.287563,48.862743],[2.287862,48.862958],[2.288109,48.863045]],\"type\":\"LineString\"}"));

the following exception is thrown at line 3:

java.lang.NullPointerException
	at org.locationtech.spatial4j.io.GeoJSONReader.readShapeFromCoordinates(GeoJSONReader.java:304)
	at org.locationtech.spatial4j.io.GeoJSONReader.readShape(GeoJSONReader.java:248)
	at org.locationtech.spatial4j.io.GeoJSONReader.read(GeoJSONReader.java:48)
	at org.locationtech.spatial4j.io.GeoJSONReader.read(GeoJSONReader.java:54)

This is unfortunately a common issue I've see with GeoJSON parsers that are streaming based, like the spatial4j one. GeoTools suffers from the same problem and has to jump through some hoops to make it work. My initial thought looking at the code and how the parser is built is that it would be a non-trivial amount of work to handle this case, but doable.

My first thought at an approach would be to check for this case where we don't know the type and do a "dumb" read of the coordinates property, storing them for later. Then when we hit the type property check for the stored coordinates and parse those like we normally would. Patches are always welcome :)