r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.35k stars 299 forks source link

Rescale sf* objects preserving the CRS #2413

Closed agila5 closed 2 months ago

agila5 commented 4 months ago

Dear all, I created this issue since I have a couple of questions (the second of which is probably just a feature request) regarding the possibility of rescaling sf* objects (i.e. coordinates expressed in metres -> kilometres). The approach I currently adopt is the following:

library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE

# pt represents a point near Milan. The coordinates are in metres
pt <- st_sfc(st_point(c(1.5e6, 5e6)), crs = 3003)

# Now I rescale it expressing the coordinates in km
st_transform(
  pt, 
  pipeline = "+proj=pipeline +step +proj=unitconvert +xy_out=km"
)
#> Geometry set for 1 feature 
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1500 ymin: 5000 xmax: 1500 ymax: 5000
#> CRS:           NA
#> POINT (1500 5000)

The previous code clearly works and it's really convenient. However, I was wondering:

  1. Are there more efficient (that maybe just include less typing and/or easier to remember πŸ˜…) alternatives?
  2. Is it possible to adjust the CRS of the output object using a correctly modified version of the input CRS (instead of the current NA_crs)?

Created on 2024-07-02 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United Kingdom.utf8 #> ctype English_United Kingdom.utf8 #> tz Europe/Rome #> date 2024-07-02 #> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [2] CRAN (R 4.3.1) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.1) #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.3) #> DBI 1.2.2 2024-02-16 [1] CRAN (R 4.3.3) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.3) #> e1071 1.7-14 2023-12-06 [1] CRAN (R 4.3.2) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.3) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.3.3) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> KernSmooth 2.23-21 2023-05-03 [2] CRAN (R 4.3.1) #> knitr 1.46 2024-04-06 [1] CRAN (R 4.3.3) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.1) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.1) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.1) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.1) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.3.3) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> sf * 1.0-16 2024-03-17 [1] Github (r-spatial/sf@67d41e4) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.1) #> units 0.8-5.4 2024-06-03 [1] local #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1) #> xfun 0.43 2024-03-25 [1] CRAN (R 4.3.3) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2) #> #> [1] C:/Users/user/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.1/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
edzer commented 4 months ago

I have wondered that myself: https://github.com/r-spatial/sf/blob/main/src/gdal.cpp#L664

agila5 commented 2 months ago

Closing as the same issue was also discussed in https://github.com/r-spatial/sf/issues/2439