Skip to content

Unable to parse GeoJSON when type is set after coordinates #156

Open

Description

@mmadoo

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)

Activity

  1. jdeolive commented on Jan 15, 2018

    @jdeolive
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions