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:
In QGIS: