[GIS] Need to extract rectangle coordinates from a shapefile

extractrshapefile

New R user. I'm trying to create an observation window for a point pattern in R using a shapefile imported from ArcGIS. I've imported the shapefile and it is recognized by R as a feature class type:polygon. I need the xmin, xmax, ymin, ymax of the extent and that is all in order to specify these in creating the owin object. I can't seem to extract these values from the polygon. I know this is very basic but I can't put 2 and 2 together here.

Best Answer

You can create a rectangle with the boundary box of your polygon. I use rgdal package to make a reproducible example, but you could use only raster package with shapefile() function instead of readOGR():

library(rgdal)
library(raster)

dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
polygon <- readOGR(dsn=dsn, layer="scot_BNG")

Creates an extent object using bbox and convert it to polygon:

e <- extent(polygon@bbox)

e <- as(e,"SpatialPolygons")

See the results:

plot(polygon)
plot(e, add= T)

Rplot

Related Question