SymbolixAU / geojsonsf

Conversion between sf and geojson
Other
82 stars 7 forks source link

Error when converting a FeatureCollection to sf #16

Closed aoles closed 6 years ago

aoles commented 6 years ago

Hi, first of all thanks a lot for your package, very useful to have a GeoJSON to sf converter.

I've run into the following error when trying to convert a "FeatureCollection" into sf. According to geojsonlint::geojson_lint the GeoJSON string seems to be valid. It would be great to hear what your thoughts on this issue are.

Cheers, Andrzej

FeatureCollection.txt

# read sample GeoJSON string
library("jsonlite")
txt <- readLines("FeatureCollection.txt")
class(txt) <- "json"

# validate
library("geojsonlint")
geojson_lint(txt)
## TRUE

library("geojsonsf")
geojson_sf <- geojson_sf(txt)
## Error in rcpp_geojson_to_sf(geojson) : 
##   No 'coordinates' member at object index 0 - invalid GeoJSON
SymbolixAU commented 6 years ago

@aoles

From what I can see, the geojson_lint() function is using http://geojsonlint.com/ to validate the txt.

If you paste your GeoJSON directly into the 'valdiate' box, it will error. I don't know why the geojson_lint() function allows this to pass, however.

The error looks like it's because all the type values are arrays, not strings, e.g.

{"type":["FeatureCollection",]"features":[{"type":["Feature"],"geometry":{"type":["Polygon"],"coordinates":

From the specification it says

A Feature object has a "type" member with the value "Feature".

Whereas your 'value's are the arrays ["FeatureCollection"], ["Feature"], ["Polygon"], etc. I don't think this is valid.

I've edited your file to convert the arrays to strings and it now works.

## The development version can read files directly
geojson_sf("~/Downloads/FeatureCollection.txt")

# Simple feature collection with 5 features and 3 fields
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 11.5743 ymin: 50.9136 xmax: 11.5907 ymax: 50.9295
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
#   center                               geometry group_index  value
# 1 [11.5803,50.9203] POLYGON ((11.5777 50.9223, ...         [0]  [200]
# 2 [11.5803,50.9203] POLYGON ((11.5762 50.9193, ...         [0]  [400]
# 3 [11.5803,50.9203] POLYGON ((11.5762 50.9193, ...         [0]  [600]
# 4 [11.5803,50.9203] POLYGON ((11.5762 50.9193, ...         [0]  [800]
# 5 [11.5803,50.9203] POLYGON ((11.5743 50.9153, ...         [0] [1000]

FeatureCollection.txt

aoles commented 6 years ago

Awesome, thanks a lot for your prompt response and explanation of the problem!

SymbolixAU commented 6 years ago

@aoles - No problem. Hopefully I haven't mis-interpreted the specification!

You've also reminded me to consider nested property values, so I've added this issue

mxblsdl commented 1 year ago

Was recently wrestling with a similar problem of converting a list of FeatureCollection to sf object. This answer helped me find the solution of using jsonlite::toJSON(FeatureCollection, auto_unbox = TRUE) %>% geojson_sf() With the auto_unbox converting the types from arrays to strings.