GISR

Bounding Box of vector layer in R

Often we want to find the bounding box of our vector layer. How do we do that in R? We can use the extent function from the raster package, which returns us the boundary coordinates of the layer:

library(raster)
extent(railroad)

> extent(railroad)
class       : Extent
xmin        : 638667.5
xmax        : 678777.7
ymin        : 490053.6
ymax        : 519583.1

If we want to get a vector layer that contains the extent, we can use the rgeos library we already used. This library contains a tool called gEnvelope, which returns a bounding box of layer that we store in the variable bbox:

library(rgeos)
bbox = gEnvelope(railroad)

Let’s display the result of the function:

plot(bbox)

Let’s also add the layer from which we created the bounding box:

plot(railroad, add = TRUE, col = "red")

We have generated the bounding box of the railroad layer. And how do we generate the areas for each line segment in the railroad layer. Nothing difficult, just we add to our gEnvelope function the argument we already used byid:

bbox_single = gEnvelope(railroad, byid = T)

Let’s display the result of our action:

plot(bbox_single)
plot(railroad,add = T,col="red")

Remember, you have to load the vector data first! We have used data from previous posts to show you the gEnvelope function.

Leave a Reply

Your email address will not be published. Required fields are marked *