Skip to content

plot extent and/or convert extent to SpatialPolygons

4 messages · Manuel Schneider, John Baumgartner, Agustin Lobo

#
Is it possible to plot an extent object (i.e. on top of a raster like
it would be done for a polygon with add=TRUE)
Or can I just convert the extent object to SpatialPolygons?

Thanks
Agus
#
Agustin Lobo <alobolistas <at> gmail.com> writes:
Try

bbox2square <- function(bb)
{	
	coord.mat <- data.frame(x=rep(bb[1,],each=2),y=c(bb[2,],bb[2,2:1]))
	coord.mat <- rbind(coord.mat, coord.mat[1,])
	sq <- SpatialPolygons(list(Polygons(list(Polygon(coord.mat)), ID = 1)))
	return(as(sq, "SpatialPolygonsDataFrame"))
}

where bb <- bbox(obj)
#
I just realised my reply only went to Agustin - here it is again...

You can convert extent e to SpatialPolygons with

as(e, 'SpatialPolygons')

... but yes, you can also plot the extent object with plot.

e.g.

r <- raster(matrix(runif(100), nc=10))
plot(r)
e <- extent(0.5, 0.75, 0.5, 0.75)
plot(e, add=TRUE)

On Mon, Sep 21, 2015 at 6:29 PM, Manuel Schneider <
manuel.schneider at agroscope.admin.ch> wrote:

            

  
  
#
Apologies.
I had done
ext128 <- c(128,1280-128,128,960-128)

instead of
ext128 <- extent(c(128,1280-128,128,960-128))

This is why plot() and as() were not working for me.
Agus
On Mon, Sep 21, 2015 at 10:31 AM, John Baumgartner <johnbaums at gmail.com> wrote: