Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mmadoo opened this issue Jan 15, 2018 · 1 comment
Open

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

mmadoo opened this issue Jan 15, 2018 · 1 comment

Comments

@mmadoo
Copy link

mmadoo commented Jan 15, 2018

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)
@jdeolive
Copy link
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 :)

jdeolive added a commit to voyagersearch/spatial4j that referenced this issue Feb 28, 2022
First cut at locationtech#156

(cherry picked from commit 36a02c52efdf758ea9ba0df61569bcff008cb3ba)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants