3

I have a shapefile with points and I want to create square buffers around my points 6x6. My coordinate system is in meters. I use R and for the buffers I use:

bufers=gBuffer(file, width = 6,capStyle="SQUARE",byid=TRUE).

When I load the buffers in QGIS, I use the measure line plugin for my windows and instead of 6x6 I see that they are 11X11.

Is that normal?

nmtoken
  • 13,355
  • 5
  • 38
  • 87
geo_dd
  • 604
  • 9
  • 23

2 Answers2

3

If you want to create 6x6 square buffers around your points you need to give an option of width = 3 in gBuffer. Because the width is the distance radio r from the center.

Try the reproducible example below:

# Load packages
library('sp')
library('rgeos')
library('rgdal')

# Define projection
epsg.32721 <- "+proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs"

# Load data points
x <- c(794801.3, 795014.1, 795055.7, 795051.1, 795070.9, 794885.1, 795102.9, 795131.9, 795127.4, 795192.6, 795196.3, 795219.3, 795283.2, 795307.2, 795307.2, 795307.7, 795327.7, 795330.2, 795360.6, 795355.7, 794779.8)
y <- c(6194137, 6194211, 6194192, 6194192, 6194202, 6194210, 6194207, 6194217, 6194217, 6194231, 6194234, 6194275, 6194279, 6194713, 6194713, 6194714, 6194795, 6194335, 6194869, 6194865, 6194109)

# Create SpatialPoints object
spatialPointsObject <- SpatialPoints(coords = cbind(x,y), proj4string = CRS(epsg.32721))

# Create 6x6 square buffers
buffersWidth6 <- gBuffer(spgeom = spatialPointsObject, width = 6, capStyle = "SQUARE", byid = TRUE)
buffersWidth3 <- gBuffer(spgeom = spatialPointsObject, width = 3, capStyle = "SQUARE", byid = TRUE)

# Plot SpatialPoints + Buffers
plot(buffersWidth6[1,], xlab = "Longitude", ylab = "Latitude", main = "SpatialPoints + Buffers", col = rgb(0.1,0.2,0.1,0.3))
plot(buffersWidth3[1,], add = TRUE, col = rgb(0.8,0.3,0.8,0.3))
plot(spatialPointsObject[1,], pch = 19, add = TRUE)
box(lwd = 4)

# Convert to SpatialPolygonsDataFrame object
buffersWidth6 <- SpatialPolygonsDataFrame(Sr = buffersWidth6, data = data.frame("ID" = 1:length(buffersWidth6@polygons)))
buffersWidth3 <- SpatialPolygonsDataFrame(Sr = buffersWidth3, data = data.frame("ID" = 1:length(buffersWidth3@polygons)))

# Write shapefiles to open in QGIS
writeOGR(obj = buffersWidth6, dsn = "buffersWidth6", layer = "buffersWidth6", driver = "ESRI Shapefile")
writeOGR(obj = buffersWidth3, dsn = "buffersWidth3", layer = "buffersWidth3", driver = "ESRI Shapefile")

RPlot:

plotofbuffersinR

In QGIS:

QGISdistances

Guz
  • 3,176
  • 2
  • 18
  • 40
0

A square buffer around a point is defined by the four corners. So you could easily compute these "by hand" rather than having to dive into rgeos which might do some approximations.

Simply get the point coordinates from your SpatialPoints, then add plus and minus three to each column to get the four corner points. Then make those into SpatialPolygons. The polygons will be precisely 6x6 units.

Note this doesn't work if you have overlapping buffers that you want merged, but with byid=TRUE that won't happen.

Spacedman
  • 63,755
  • 5
  • 81
  • 115