Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geojson_sf with NA numeric values changes to character #79

Closed
ateucher opened this issue Apr 8, 2020 · 4 comments
Closed

geojson_sf with NA numeric values changes to character #79

ateucher opened this issue Apr 8, 2020 · 4 comments
Assignees
Labels
bug

Comments

@ateucher
Copy link
Contributor

@ateucher ateucher commented Apr 8, 2020

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)

@ateucher ateucher changed the title Round trip with NA values geojson_sf with NA numeric values changes to character Apr 8, 2020
@SymbolixAU
Copy link
Owner

@SymbolixAU SymbolixAU commented Apr 8, 2020

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).

@SymbolixAU SymbolixAU self-assigned this Apr 8, 2020
@SymbolixAU SymbolixAU added the bug label Apr 8, 2020
dcooley added a commit that referenced this issue Apr 15, 2020
@dcooley
Copy link
Collaborator

@dcooley dcooley commented Apr 15, 2020

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"
dcooley added a commit that referenced this issue Apr 15, 2020
@ateucher
Copy link
Contributor Author

@ateucher ateucher commented Apr 15, 2020

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
Copy link
Collaborator

@dcooley dcooley commented Apr 15, 2020

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

@dcooley dcooley closed this in 2b0d41a Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.