locationtech / spatial4j

LocationTech Spatial4j: A Geospatial Library for Java
https://projects.eclipse.org/projects/locationtech.spatial4j
932 stars 167 forks source link

Unknown type: FeatureCollection #190

Closed porunov closed 4 years ago

porunov commented 4 years ago

Version: spatial4j-0.7

The code to reproduce the error:

String geoJson = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-126.456789,-2.123,0.0],[-123.456789,2.123,0.0],[-124.456789,2.123,0.0],[-125.456789,-2.123,0.0],[-126.456789,-2.123,0.0]]]},\"properties\":{\"myProp\":\"PropData1\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-123.123,55.555,0.0]},\"properties\":{\"myProp\":\"PropData2\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[123.456789,2.123,0.0],[-123.456789,2.123,0.0],[-124.456789,2.123,0.0],[-125.456789,-2.123,0.0],[123.456789,2.123,0.0]]]},\"properties\":{\"myProp\":\"PropData3\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-15.123,75.555,0.0]},\"properties\":{\"myProp\":\"PropData4\"}}]}";

JtsSpatialContextFactory factory = new JtsSpatialContextFactory();
JtsSpatialContext context = new JtsSpatialContext(factory);
GeoJSONReader geojsonReader = new GeoJSONReader(context, factory);
Shape shape = geojsonReader.read(geoJson);
Unknown type: FeatureCollection
java.text.ParseException: Unknown type: FeatureCollection
    at org.locationtech.spatial4j.io.GeoJSONReader.readShape(GeoJSONReader.java:273)
    at org.locationtech.spatial4j.io.GeoJSONReader.read(GeoJSONReader.java:48)
    at org.locationtech.spatial4j.io.GeoJSONReader.read(GeoJSONReader.java:54)

For convince here is the geoJson which I try to parse:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -126.456789,
              -2.123,
              0.0
            ],
            [
              -123.456789,
              2.123,
              0.0
            ],
            [
              -124.456789,
              2.123,
              0.0
            ],
            [
              -125.456789,
              -2.123,
              0.0
            ],
            [
              -126.456789,
              -2.123,
              0.0
            ]
          ]
        ]
      },
      "properties": {
        "myProp": "PropData1"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -123.123,
          55.555,
          0.0
        ]
      },
      "properties": {
        "myProp": "PropData2"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              123.456789,
              2.123,
              0.0
            ],
            [
              -123.456789,
              2.123,
              0.0
            ],
            [
              -124.456789,
              2.123,
              0.0
            ],
            [
              -125.456789,
              -2.123,
              0.0
            ],
            [
              123.456789,
              2.123,
              0.0
            ]
          ]
        ]
      },
      "properties": {
        "myProp": "PropData3"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -15.123,
          75.555,
          0.0
        ]
      },
      "properties": {
        "myProp": "PropData4"
      }
    }
  ]
}
dsmiley commented 4 years ago

Hi, Spatial4j works with the "geometry" portion of GeoJSON, not the FeatureCollection wrapper. Perhaps pertinent documentation needs to be more clear about that.

porunov commented 4 years ago

@dsmiley Thank you for clarification!