admin管理员组

文章数量:1123400

I have used the rgdal and raster packages for converting my data from UTM 32 to UTM 33. However, these libraries have been replaced. Could you advise on the equivalent codes in the new terra and sf packages to perform the same operation?

Example data:

library(StatDA) ## contain the data
data <- data("trondelagC")
d_Tr <- subset(trondelagC,X.Medium=="Till")  ##wgs32

proj4string(d_Tr) <- CRS("+init=epsg:32632") ## states the coordinate system

d1_Tr <- spTransform(x=d_Tr,CRS("+init=epsg:32633"))  ## gives coordinates in UTM 33

In the latest version of R and RStudio this does not work any longer, I would appreciate any suggestions

I have used the rgdal and raster packages for converting my data from UTM 32 to UTM 33. However, these libraries have been replaced. Could you advise on the equivalent codes in the new terra and sf packages to perform the same operation?

Example data:

library(StatDA) ## contain the data
data <- data("trondelagC")
d_Tr <- subset(trondelagC,X.Medium=="Till")  ##wgs32

proj4string(d_Tr) <- CRS("+init=epsg:32632") ## states the coordinate system

d1_Tr <- spTransform(x=d_Tr,CRS("+init=epsg:32633"))  ## gives coordinates in UTM 33

In the latest version of R and RStudio this does not work any longer, I would appreciate any suggestions

Share Improve this question edited 13 hours ago jpsmith 16.9k6 gold badges20 silver badges44 bronze badges asked 13 hours ago Belinda FlemBelinda Flem 12 bronze badges New contributor Belinda Flem is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 5
  • What does "does not work" mean - does it give you an error, return the wrong results, etc? – jpsmith Commented 13 hours ago
  • I get error message: could not find function "spTransform" – Belinda Flem Commented 13 hours ago
  • My question is: which codes can I use from terra and or sf instead of those from rgdal and raster. rgdal and raster has been phased out – Belinda Flem Commented 13 hours ago
  • Did you try anything from those packages that didn’t work? – jpsmith Commented 12 hours ago
  • Belinda, take a look to this book: r-spatial.org/book/07-Introsf.html. Your dataset seems to be from robCompositions. But you are probably looking to something like : d_Tr |> sf::st_as_sf(coords = c("E32wgs","N32wgs"),crs = 32632 ) |> sf::st_transform(crs=32633) – VinceGreg Commented 11 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Here is the general idea

library(terra) 
xy <- cbind(c(0, 10), c(10, 50)) 
project(xy, from="+proj=longlat", to="epsg:32633") 
#           [,1]    [,2]
#[1,] -1161976.4 1143849
#[2,]   141743.6 5550619

Or more formally:

v <- vect(xy, crs="lonlat")
p <- project(v, "epsg:32633") 
# class       : SpatVector 
# geometry    : points 
# dimensions  : 2, 0  (geometries, attributes)
# extent      : -1161976, 141743.6, 1143849, 5550619  (xmin, xmax, ymin, ymax)
# coord. ref. : WGS 84 / UTM zone 33N (EPSG:32633) 

crds(p)
#              x       y
#[1,] -1161976.4 1143849
#[2,]   141743.6 5550619

And see ?terra::project and/or ?sf::st_transform for additional possibilities.

本文标签: rHow to change UTM zone of a data setStack Overflow