ropensci / geojsonio

Convert many data formats to & from GeoJSON & TopoJSON
https://docs.ropensci.org/geojsonio
Other
151 stars 59 forks source link

sf 0.7.0 (upcoming) names sfc objects, resulting in invalid geojson #142

Closed ateucher closed 5 years ago

ateucher commented 5 years ago
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(geojsonio)
#> 
#> Attaching package: 'geojsonio'
#> The following object is masked from 'package:base':
#> 
#>     pretty
library(geojsonlint)
#> 
#> Attaching package: 'geojsonlint'
#> The following object is masked from 'package:geojsonio':
#> 
#>     as.location
x <- st_sfc(st_point(0:1), st_point(1:2))

names(x) <- 1:2

x_json <- geojson_json(x)
# Geometries are 'named', which is not valid geojson
cat(x_json)
#> {"type":"GeometryCollection","geometries":{"1":{"type":"Point","coordinates":[0,1]},"2":{"type":"Point","coordinates":[1,2]}}}

geojson_hint(geojson_json(x))
#> [1] FALSE

x_json_no_names <- geojson_json(unname(x))
cat(x_json_no_names)
#> {"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[0,1]},{"type":"Point","coordinates":[1,2]}]}

geojson_hint(x_json_no_names)
#> [1] TRUE

I think the answer is to just unname() sfc objects before converting with geojson_json.sfc() and geojson_list.sfc()

Created on 2018-10-12 by the reprex package (v0.2.1)

ateucher commented 5 years ago

@sckott @edzer

sckott commented 5 years ago

thanks for pointing this out @ateucher

that sounds good to me to unname the objects. is there any complexity or edge cases here?

ateucher commented 5 years ago

Not that I can see. It works in rmapshaper which has a pretty comprehensive test suite. The only issue is that on round-trip (i.e., sfc -> geojson -> scf) the names would be lost... but I don't know a: if that's a big deal, or b: a robust way to get around it (i.e., if there is manipulation of the geojson before converting back to sfc.

sckott commented 5 years ago

we could consider keeping the names in attributes in the object, and applying them to the geojson before converting back to sfc ?

ateucher commented 5 years ago

Yep, that would work if the geojson is unchanged (though then you have to ask why would a user do a straight round trip if not to do some processing (e.g., with rmapshaper or jqr)?

sckott commented 5 years ago

right, that's not gonna work to reapply names

edzer commented 5 years ago

Having named geometries is, afair, neither mentioned by simple feature access nor by the geojson RFC. Package sf does it because OGR (GDAL) provides feature IDs -- for some data formats it makes sense to pass them on to R. I don't see an issue if geojsonio drops them.

ateucher commented 5 years ago

Thanks @edzer that's a good perspective.

github-actions[bot] commented 2 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.