Skip to content
Prev 24809 / 29559 Next

How to do square buffers around spatial points

Here's a one-liner:

# for some SpatialPointsDataFrame `d`, and some half-width `w`, this
produces a set of square SpatialPolygons
# with square height and width equal to 2*w:

polys = SpatialPolygons(apply(apply(coordinates(d),1,function(r){cbind(r+c(w,w),r+c(w,-w),r+c(-w,-w),r+c(-w,w),r+c(w,w))}),2,function(v){Polygons(list(Polygon(matrix(v,ncol=2,byrow=TRUE))),ID=runif(1))}))

 > plot(polys)
 > plot(d,add=TRUE)

Barry



On Thu, Aug 25, 2016 at 2:11 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote: