SymbolixAU / geojsonsf

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

geojson_sf with NA numeric values changes to character #79

Closed ateucher closed 4 years ago

ateucher commented 4 years ago

It seems that when a numeric field in a geojson contains null values, geojson_sf converts that field to character:

library(sf)
#> Linking to GEOS 3.8.1, GDAL 2.4.4, PROJ 7.0.0
library(geojsonsf)

gj <- '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"a":1.0},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},{"type":"Feature","properties":{"a":null},"geometry":{"type":"Point","coordinates":[1.0,1.0]}}]}'

sf <- geojson_sf(gj)

sf$a # character
#> [1] "1" NA

Created on 2020-04-08 by the reprex package (v0.3.0)

SymbolixAU commented 4 years ago

Thanks. This is odd given the underlying json parser keeps the numeric

res <- jsonify::from_json( gj )
str( res$features$properties )
# 'data.frame': 2 obs. of  1 variable:
#   $ a: num  1 NA

There must be a mis-match between my implementations (I wrote geojsonsf waaaay before jsonify, so probably not everything made it across).

dcooley commented 4 years ago

On branch issue79 I think this is working

library(sf)
#> Linking to GEOS 3.8.1, GDAL 2.4.4, PROJ 7.0.0

# remotes::install_github("SymbolixAU/geojsonsf", ref = "issue79")
library(geojsonsf)

gj <- '{"type":"FeatureCollection","features":[
  {"type":"Feature","properties":{"a":1.0,"b":true,"c":"hello"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},
  {"type":"Feature","properties":{"a":null,"b":null,"c":null},"geometry":{"type":"Point","coordinates":[1.0,1.0]}}
]}'

sf <- geojson_sf(gj)

str( sf )

# Classes ‘sf’ and 'data.frame':    2 obs. of  4 variables:
# $ a       : num  1 NA
# $ c       : chr  "hello" NA
# $ b       : logi  TRUE NA
# $ geometry:sfc_POINT of length 2; first list element:  'XY' num  0 0
# - attr(*, "sf_column")= chr "geometry"
ateucher commented 4 years ago

Brilliant! All tests pass in rmapshaper now (at least locally) with that fix (nb. I did have to install dev sfheaders to get geojsonsf to install from github).

dcooley commented 4 years ago

yeah I just saw that too, so have updated the sfheaders requirement in the DESCRIPTION.

chloedebyser commented 1 year ago

It seems that this issue isn't resolved whenever the first value in a numeric field is a null value.

For example:

library(geojsonsf)

gj <- '{"type":"FeatureCollection","features":[
  {"type":"Feature","properties":{"a":null,"b":true,"c":"hello"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},
  {"type":"Feature","properties":{"a":1.0,"b":null,"c":null},"geometry":{"type":"Point","coordinates":[1.0,1.0]}}
]}'

sf <- geojson_sf(gj)

str( sf )

# Classes ‘sf’ and 'data.frame':    2 obs. of  4 variables:
# $ a       : chr  NA "1"
# $ b       : logi  TRUE NA
# $ c       : chr  "hello" NA
# $ geometry:List of 2
dcooley commented 1 year ago

thanks - should now be fixed on master branch

chloedebyser commented 1 year ago

Fantastic, thank you!