2

I want to convert R 'library:raster' 'extent' object to a shapefile. I have the CRS available too.

  e=  c(4304916, 4305325, 365216, 365439)#xmin, xmax, ymin, ymax
  proj4string(e) = CRS("+proj=utm +zone=18 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")

In older version of raster data there was a specific function "" 'extentTopolygon' which is depreciated now. My current approach is to create a raster object change its projection and assign the current extent and convert it to shapefile, which is not very efficient.

1 Answer 1

6

You can do the following:

library(terra)
e <- ext( c(4304916, 4305325, 365216, 365439) )
p <- as.polygons(e)
crs(p) <- "+proj=utm +zone=18 +ellps=GRS80 +datum=NAD83 +units=m"
writeVector(p, 'file.shp')

Or with the raster package

library(raster)
e <- extent( c(4304916, 4305325, 365216, 365439) )
p <- as(e, 'SpatialPolygons')
crs(p) <- "+proj=utm +zone=18 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
shapefile(p, 'file.shp')
4
  • 1
    Just a comment--the coordinates are given are swapped. The four million values are the 'y' or northing values while the three hundred thousand values are the 'x' or easting values.
    – mkennedy
    Commented Sep 27, 2015 at 14:15
  • @mkennedy. How so? I do not think so. See plot(p, axes=TRUE) Commented Sep 27, 2015 at 15:52
  • Okay, I don't see that in the question or answer (in the current versions), but UTM is always easting,northing so...? You know the software, not me.
    – mkennedy
    Commented Sep 27, 2015 at 16:42
  • right, and easting is x, and northing is y, hence the order Commented Sep 27, 2015 at 20:10

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.