[R-sig-Geo] plot extent and/or convert extent to SpatialPolygons

John Baumgartner johnbaums at gmail.com
Mon Sep 21 10:31:33 CEST 2015


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:

> Agustin Lobo <alobolistas <at> gmail.com> writes:
>
> >
> > 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
> >
>
> 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)
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list