SymbolixAU / geojsonsf

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

Only geometry sf object output #38

Closed layik closed 6 years ago

layik commented 6 years ago

Hi @SymbolixAU you know I am using the package in geoplumber and last time I opened an issue I just wasted your time. Hope I am not doing the same now.

Please have a look at this and let me know if that is what you expect the outcome to be. Thank you

library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2
ls <- st_linestring(rbind(c(0,0),c(1,1),c(2,1)))
mls <- st_multilinestring(list(rbind(c(2,2),c(1,3)), rbind(c(0,0),c(1,1),c(2,1))))
sfc <- st_sfc(ls,mls)
class(sfc)
#> [1] "sfc_GEOMETRY" "sfc"
sf <- st_sf(sfc)
gjio <- geojsonio::geojson_json(sf)
#> Warning: 'geojsonlint' not installed, skipping GeoJSON linting
gjsf <- geojsonsf::sf_geojson(sf)
class(gjio)
#> [1] "geofeaturecollection" "geojson"              "geo_json"            
#> [4] "json"
print(gjio)
#> <FeatureCollection> 
#>   type:  FeatureCollection 
#>   no. features:  2 
#>   features (1st 5):  LineString, MultiLineString
class(gjsf)
#> [1] "character"
print(gjsf)
#> [1] "{\"type\":\"LineString\",\"coordinates\":[[0,0],[1,1],[2,1]]}"                     
#> [2] "{\"type\":\"MultiLineString\",\"coordinates\":[[[2,2],[1,3]],[[0,0],[1,1],[2,1]]]}"

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


TODO

SymbolixAU commented 6 years ago

HI @layik

Yes, this is by design, but I'm open for a discussion about the validity of it.

from RFC 7946

A Feature object has a member with the name "properties".

I implemented sf_geojson() so that if there weren't any properties in an sf object, it won't get interpreted as a Feature.


What's your opinion on how this should behave? Should an sf object become a FeatureCollection regardless of whether it contains properties?

layik commented 6 years ago

Hey @SymbolixAU, thank you for the confirmation and reference.

I actually don't know. Let me also say how I got to become aware of this, 'plumber' is my underlying HTTP package which in turn relies on "httr", and to serve application/json it does not like vectors > 1.

If a vector > 1 is provided, it only returns the first element. Does that mean, they too, expect a one vector "FeatureCollection" by default? I doubt they would be willing to contribute to this discussion.

This is the warning coming from httr

In (function (x)  : argument should be a character vector of length 1
all but the first element will be ignored

and here is a quick googling.

I will try to educate myself a little more on GeoJSON before I can be of help!

SymbolixAU commented 6 years ago

Maybe the solution is to have an argument inside sf_geojson() like simplify = TRUE/FALSE (much like drop = FALSE for [.data.frame). So setting simplify = FALSE will mean

  1. sf always becomes FeatureCollection
  2. sfc always becomes vectors of features

and simplify = TRUE will reduce sf objects to vectors of features if there aren't any properties.

layik commented 6 years ago

That would make my underlying code happy!

SymbolixAU commented 6 years ago

I've added it to v1.2 milestone due by the end of October.

layik commented 6 years ago

Thank you very much @SymbolixAU. Awesome response time.

layik commented 6 years ago

Close it when you feel like it.

SymbolixAU commented 6 years ago

@layik in this commit I've implemented a simplify argument.

to test you'll need to install jsonify (I've submitted it to CRAN, but it's not accepted yet).

# devtools::install_github("SymbolixAU/jsonify")
## jsonify now on CRAN
devtools::install_github("SymbolixAU/geojsonsf")