Home > database >  Shapefile extent - from UTM to lat/long
Shapefile extent - from UTM to lat/long

Time:09-12

I have a SpatVector in a UTM projection, with the extent in easting/northing values, and I would like to reproject it to a lat/long projection and obtain the extent in degrees. I already tried the project() function of the terra package, but nothing changes.

This is the extent of my SpatVector: extent: 293596.6, 415718.1, 5048171, 5168476 (xmin, xmax, ymin, ymax).

And I would like to obtain something like this: extent: -10.4778, 70.2623, 29.94065, 68.34284 (xmin, xmax, ymin, ymax)

Thank you for your help!!

CodePudding user response:

This is what I use to convert UTM to lat/lon:

utm_to_latlon <- function(utme, utmn, utmzone) {
  y <- sp::SpatialPoints(cbind(utme, utmn))
  sp::proj4string(y) <- sp::CRS(paste0(" proj=utm  zone=", utmzone, "  ellps=WGS84  datum=WGS84"))
  y <- sp::spTransform(y, sp::CRS(" proj=longlat  ellps=WGS84  datum=WGS84"))
  colnames(y@coords) <- c("lon", "lat")
  data.frame(y@coords)
}

I don't know of a single function (in sp or rgeos or otherwise) that does this itself.

CodePudding user response:

Here is how you can use terra::project

Example data

library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)

lonlat to UTM

utm <- terra::project(v, " proj=utm  zone=32")
ext(utm) |> round()
#SpatExtent : 266046, 322164, 5481445, 5563062 (xmin, xmax, ymin, ymax)

And back

geo <- terra::project(v, " proj=longlat")
ext(geo) |> round(1)
#SpatExtent : 5.7, 6.5, 49.4, 50.2 (xmin, xmax, ymin, ymax)

To only project the extent:

e <- as.polygons(utm, ext=TRUE)
e <- terra::project(e,  " proj=longlat")
ext(e) |> round(1)
#SpatExtent : 5.7, 6.5, 49.4, 50.2 (xmin, xmax, ymin, ymax)

Or variations like this, depending on your ultimate goal:

as.points(ext(utm), crs=crs(utm)) |> project(" proj=longlat") |> crds()
#            x        y
#[1,] 5.772571 49.44059
#[2,] 5.723418 50.17350
#[3,] 6.508450 50.19301
#[4,] 6.545861 49.45960
  • Related