Skip to content
Prev 29148 / 29559 Next

Re-scale units of coordinates in sf geometry?

Thank you very much for your example! I briefly checked the examples
reported in ?st_transform and the docs at the PROJ website around
"Unit conversion"
(https://proj.org/operations/conversions/unitconvert.html) and I found
that the following code also seems to work (although with a warning
message that I'm not sure I understand):

library(sp)
demo(meuse, echo = FALSE, ask = FALSE)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
sf_proj_network(TRUE)
#> [1] "https://cdn.proj.org"
st_as_sf(meuse) |> st_bbox()
#>   xmin   ymin   xmax   ymax
#> 178605 329714 181390 333611
meuse2 <- st_as_sf(meuse) |>
  st_transform(pipeline = "+proj=pipeline +step +proj=unitconvert
+xy_in=m +xy_out=km")
#> Warning in st_transform.sfc(st_geometry(x), crs, ...): pipeline not found in
#> PROJ-suggested candidate transformations
st_bbox(meuse2)
#>    xmin    ymin    xmax    ymax
#> 178.605 329.714 181.390 333.611

I think this might be easier than manually adjusting the WKT
representation of the CRS, but I'm not sure that this is really a
recommended way to transform units since, for some reason, it does not
modify the CRS of the objects which makes any analysis very difficult:

all.equal(st_crs(meuse), st_crs(meuse2))
#> [1] TRUE
library(mapview)
mapview(meuse) # seems right
mapview(meuse2) # completely off

Andrea

Il giorno mar 31 gen 2023 alle ore 12:33 Edzer Pebesma
<edzer.pebesma at uni-muenster.de> ha scritto: