1

I would like to use the worldMap from rworldMap, but need this in UTM projection rather than long/lat. I first clip the map to my area of interest. How do I then convert this to UTM projection? My code below does not seem to work:

library(rworldmap)
library(rgeos)
library(raster)


# load land
worldMap <- getMap(resolution = "high")

# polygon holding area of interest in lat/long
CP <- as(extent(-70, -55, -60, -38), "SpatialPolygons")
projection(CP) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

# clip world map to area of interest
map.cp <- crop(worldMap, CP, byid = TRUE)
plot(map.cp)

# polygon holding area of interest in utm
CP.utm <- as(extent(-1000, 1500000, -6500000, -5000000), "SpatialPolygons")
projection(CP.utm) <- "+proj=utm +zone=20 +datum=WGS84 +units=m +no_defs"
projection(CP.utm)

# make projection of map.cp same as CP.utm
map.cp.utm <- map.cp
projection(map.cp.utm) <- CRS(proj4string(CP.utm))
projection(map.cp.utm)

# this shows that something is not right
plot(CP.utm)
plot(map.cp.utm, add = T)

1 Answer 1

1

This does not change the numbers in the data, it only sets the coordinate system metadata:

> projection(map.cp.utm) <- CRS(proj4string(CP.utm))

see how the extent is still in degrees:

> map.cp.utm
class       : SpatialPolygonsDataFrame 
features    : 3 
extent      : -70, -57.52767, -55.89173, -38  (xmin, xmax, ymin, ymax)
crs         : +proj=utm +zone=20 +datum=WGS84 +units=m +no_defs 

because the coordinate values for all the features aren't changed by assigning a new projection or coordinate system. You have to do a transform.

Use spTransform:

> map.cp.utm = spTransform(map.cp, proj4string(CP.utm))

which gives you an object with the correct new coordinate numbers and metadata:

> plot(map.cp.utm)
> map.cp.utm
class       : SpatialPolygonsDataFrame 
features    : 3 
extent      : -114952.6, 980634.2, -6203644, -4219970  (xmin, xmax, ymin, ymax)
crs         : +proj=utm +zone=20 +datum=WGS84 +units=m +no_defs 
1
  • Perfect! Thank you also for the explanation of why my code didnt work. That makes sense"!
    – user303287
    Commented May 8, 2022 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.