SymbolixAU / geojsonsf

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

sf_geojson may crash R session if sf type is factor not character #65

Closed cywhale closed 5 years ago

cywhale commented 5 years ago

I can get correct results by forcing the factor type in sf to be as.character() But it may be still an issue to crash the R session...

library(sf)
library(geojsonsf)
## I put the test file contains a sf object (sfx) on my github ##
download.file("https://github.com/cywhale/ODB/raw/master/R/sf_obj.RData",
              destfile = "~/temp/sf_obj.RData", mode="wb")
load("~/temp/sf_obj.RData")

sf2 = sfx[c(1,3), 2:3]
str(sfx)
#Classes ‘sf’ and 'data.frame': 7 obs. of  3 variables:
#  $ id  : int  6645 6684 6703 6713 6721 6731 6736
#$ type: Factor w/ 3 levels "Polygon","LineString",..: 1 2 1 3 3 1 1
#$ geom:sfc_GEOMETRY of length 7; first list element: List of 1
#..$ : num [1:5, 1:2] 124 124 124 124 124 ...

##NOTE: if column_2 (type) is a factor, then the sf_geojson will get wrong type

geojsonsf::sf_geojson(sf2) 
## LineString in the second is wrong, should be Polygon

## {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"type":"Polygon"},"geometry":{"type":"Polygon","coordinates":[[[123.75,32.9165],[123.75,32.9534],[123.7939,32.9534],[123.7939,32.9165],[123.75,32.9165]]]}},{"type":"Feature","properties":{"type":"LineString"},"geometry":{"type":"Polygon","coordinates":[[[120.9375,27.2937],[119.6191,26.1949],[119.3555,25.006],[119.6191,24.5271],[121.2891,24.9661],[122.3437,26.4312],[120.9375,27.2937]]]}}]} 

## geojsonsf::sf_geojson(sfx) ## Warning: This line of code would crash R session!!

sf2$type <- as.character(sf2$type)
geojsonsf::sf_geojson(sf2) ## get correct result

sfx$type <- as.character(sfx$type)
geojsonsf::sf_geojson(sf2) ## get correct result 
SymbolixAU commented 5 years ago

Thanks for reporting, however, I'm not seeing the error you're seeing. Which version of geojsonsf are you using? Perhaps you can try with the latest development version

devtools::install_github("SymbolixAU/geojsonsf")
SymbolixAU commented 5 years ago

And I suspect the crash was fixed in this issue

cywhale commented 5 years ago

Yes, fixed in this develop version. Thanks.