ropensci / geojsonio

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

geojson_json and geojson_write seem to unexpectedly truncate lat/lon precision #170

Closed dlebauer closed 4 years ago

dlebauer commented 4 years ago

I would like to translate my data to geojson. But I was surprised that the values of lat and lon were truncated by the geojson_json function.

Not sure if this is related to #141 or #96 that also deals with precision. Apologies if this is a duplicate (but fwiw - the 7 digits implemented in #96 should be sufficient for cm accuracy in my use case - sorry for the arbitrary precision in the example 😺 ).

I will start with the same example tibble:

library(geojsonio)
s <- dplyr::tibble(
  id = c("A", "B"),
  lat = c(10.123456789012, 10.1234567890123),
  lon = c(-20.123456789012, -20.1234567890123))

What works:

when I write out the tibble directly:

## works; values printed as specified
geojson_write(s, lat = 'lat', lon = 'lon', file = 'test_s.geojson')

produces desired precision

{"type": "FeatureCollection","name": "test_s",
"features": [{ "type": "Feature", "properties": { "id": "A", "lat": 10.123456789012, "lon": -20.123456789012 }, "geometry": { "type": "Point", "coordinates": [ -20.123456789012, 10.123456789012 ] } },{ "type": "Feature", "properties": { "id": "B", "lat": 10.1234567890123, "lon": -20.123456789012302 }, "geometry": { "type": "Point", "coordinates": [ -20.123456789012302, 10.1234567890123 ] }}]}

What doesn't work:

using geojson_json to convert the tibble to class geojson

## doesn't work; values truncated to 0.0001
t <- geojsonio::geojson_json(s, lat = "lat", lon = "lon")
summary(t)

produces values w/ only 5 digits of precision:

{"type":"FeatureCollection",
"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.12346,10.12346]},"properties":{"id":"A"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.12346,10.12346]},"properties":{"id":"B"}}]}

and if I write this out using geojson_write, the precision gets truncated by an additional digit:

geojson_write(t,file = 'test_t.geojson')
{"type":"FeatureCollection",
"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.1235,10.1235]},"properties":{"id":"A"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.1235,10.1235]},"properties":{"id":"B"}}]}
Session Info ```r - Session info ------------------------------------------------------------- setting value version R version 3.5.3 (2019-03-11) os Windows 10 x64 system x86_64, mingw32 ui RStudio language (EN) collate English_United States.1252 ctype English_United States.1252 tz America/Phoenix date 2020-07-29 - Packages ----------------------------------------------------------------- package * version date lib source assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.5.3) backports 1.1.7 2020-05-13 [1] CRAN (R 3.5.3) callr 3.4.3 2020-03-28 [1] CRAN (R 3.5.3) class 7.3-15 2019-01-01 [2] CRAN (R 3.5.3) classInt 0.4-3 2020-04-07 [1] CRAN (R 3.5.3) cli 2.0.2 2020-02-28 [1] CRAN (R 3.5.3) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.3) crul 0.9.0 2019-11-06 [1] CRAN (R 3.5.3) curl 4.3 2019-12-02 [1] CRAN (R 3.5.3) DBI 1.1.0 2019-12-15 [1] CRAN (R 3.5.3) desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.3) devtools 2.3.0 2020-04-10 [1] CRAN (R 3.5.3) digest 0.6.25 2020-02-23 [1] CRAN (R 3.5.3) dplyr 1.0.0 2020-05-29 [1] CRAN (R 3.5.3) e1071 1.7-3 2019-11-26 [1] CRAN (R 3.5.3) ellipsis 0.3.1 2020-05-15 [1] CRAN (R 3.5.3) fansi 0.4.1 2020-01-08 [1] CRAN (R 3.5.3) foreign 0.8-71 2018-07-20 [2] CRAN (R 3.5.3) fs 1.4.1 2020-04-04 [1] CRAN (R 3.5.3) generics 0.0.2 2018-11-29 [1] CRAN (R 3.5.3) geojson 0.3.4 2020-06-23 [1] CRAN (R 3.5.3) geojsonio * 0.9.2 2020-04-07 [1] CRAN (R 3.5.3) geojsonlint 0.4.0 2020-02-13 [1] CRAN (R 3.5.3) glue 1.4.1 2020-05-13 [1] CRAN (R 3.5.3) httpcode 0.3.0 2020-04-10 [1] CRAN (R 3.5.3) jqr 1.1.0 2018-10-22 [1] CRAN (R 3.5.3) jsonlite 1.7.0 2020-06-25 [1] CRAN (R 3.5.3) jsonvalidate 1.1.0 2019-06-25 [1] CRAN (R 3.5.3) KernSmooth 2.23-15 2015-06-29 [2] CRAN (R 3.5.3) lattice 0.20-38 2018-11-04 [2] CRAN (R 3.5.3) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.5.3) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.5.3) magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.3) maptools 1.0-1 2020-05-14 [1] CRAN (R 3.5.3) memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.3) pillar 1.4.6 2020-07-10 [1] CRAN (R 3.5.3) pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 3.5.3) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.5.3) pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.3) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.5.3) processx 3.4.2 2020-02-09 [1] CRAN (R 3.5.3) ps 1.3.3 2020-05-08 [1] CRAN (R 3.5.3) purrr 0.3.4 2020-04-17 [1] CRAN (R 3.5.3) R6 2.4.1 2019-11-12 [1] CRAN (R 3.5.3) Rcpp 1.0.5 2020-07-06 [1] CRAN (R 3.5.3) remotes 2.1.1 2020-02-15 [1] CRAN (R 3.5.3) RevoUtils * 11.0.3 2019-05-10 [2] local RevoUtilsMath * 11.0.0 2019-05-08 [2] local rgeos 0.5-3 2020-05-08 [1] CRAN (R 3.5.3) rlang 0.4.7 2020-07-09 [1] CRAN (R 3.5.3) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.3) rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.5.3) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.3) sf 0.9-5 2020-07-14 [1] CRAN (R 3.5.3) sp * 1.4-2 2020-05-20 [1] CRAN (R 3.5.3) stringi 1.4.6 2020-02-17 [1] CRAN (R 3.5.3) testthat 2.3.2 2020-03-02 [1] CRAN (R 3.5.3) tibble 3.0.3 2020-07-10 [1] CRAN (R 3.5.3) tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.5.3) units 0.6-7 2020-06-13 [1] CRAN (R 3.5.3) usethis 1.6.1 2020-04-29 [1] CRAN (R 3.5.3) V8 3.2.0 2020-06-19 [1] CRAN (R 3.5.3) vctrs 0.3.2 2020-07-15 [1] CRAN (R 3.5.3) withr 2.2.0 2020-04-20 [1] CRAN (R 3.5.3) [1] C:/Users/David/Documents/lib/R [2] C:/Program Files/Microsoft/R Open/R-3.5.3/library ```
sckott commented 4 years ago

thx @dlebauer having a look

sckott commented 4 years ago

What is your default getOption("digits") value?

dlebauer commented 4 years ago

@sckott 7

sckott commented 4 years ago

Please try again after reinstalling like remotes::install_github("ropensci/geojsonio")

precision param added to geojson_json() and geojson_write.json()

dlebauer commented 4 years ago

Fantastic, it works! Thanks for the quick response!

> packageVersion(pkg = 'geojsonio')
[1] ‘0.9.2.93’
> options(digits = 10)
> summary(geojson_json(s, lat = "lat", lon = "lon", precision = 10))
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.12345679,10.12345679]},"properties":{"id":"A"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-20.12345679,10.12345679]},"properties":{"id":"B"}}]}
github-actions[bot] commented 1 year 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.