I have a points shapefile and I want to create a rectangular buffer (22 x 16 km) around each point. My goal is to create a 16x22 km grid where each of the points is the centroid of the grid. Seed the attached image for an example of what I want to do.
4 Answers
Here is a suggestion using GridTopology
from R.
library(sp)
library(rgeos)
# coordinates of some points
x <- c(44, 66, 88, 22, 44, 66, 44, 66)
y <- c(64, 64, 64, 48, 48, 48, 32, 32)
sp <- SpatialPoints(cbind (x,y))
# dimensions of grid
topleftCorner <- bbox(sp)[,1]
columns <- length(unique(x))
rows <- length(unique(y))
cellWidth <- 22
cellHeight <- 16
# make the grid
grd <- GridTopology(topleftCorner, c(cellWidth,cellHeight), c(columns,rows))
grdPolys <- as.SpatialPolygons.GridTopology(grd) #rectangles
# remove the polys which don't have a point
grdPolys <- grdPolys[over(sp, grdPolys),]
plot(grdPolys)
plot(sp, col="red", add=T, pch=1)
Here is a approach made with GRASS on al Linux machine.
Import your vector layer with the point features:
v.in.ogr -o dsn=/your_shapefile_directory layer=your_vector_layer output=points_moved
Then move (in meter) your points with half of the size of the grid you want to have (first X, second Y direction):
v.edit map=points_moved type=point tool=move move=-11000,-8000 where="(id >= 1)"
(The where clause here is optional)
After this create the grid:
k=0;
for i in `v.out.ascii input=points_moved format=point fs=,`;
do k=$((k+1)); v.mkgrid map="pp$k" grid=1,1 position=coor coor=${i%,*} box=22000,16000; done
In the v.mkgrid function there you can define coordinates on which the grid will be snapped. To get the coordinates from the points use v.out.ascii.Defining coor=${i%,*} in v.mkgrid sets the coordinates for the grids (grid=1,1 > number of rows and columns in grid), avoiding a z-value as a third value from the ASCII output.
The pp* layer you can merge with v.patch.
names=""
for i in `g.mlist type=vect pattern="pp*"`; do names="$i,$names"; done
v.patch -e input=${names%,} output=merged_lines
Compare the points, points_moved and the merged layer to see the result.
!!! I have used spatial data with a metric coordinate reference system (DHDN / Gauss-Kruger zone 4)
When you have coordinate reference system like WGS84 (EPSG:4326) you have to use values in degrees.
If you change the "End cap style" to "Square" it will make squares around the point. Your points would have to be perfectly spaced for the grids to connect perfectly though and even then you may have some gaps or overlap.
Would it be just as easy to:
Create the grid layer with the dimensions that you want using the MMQGIS plugin and the Create | Create Grid option.
Create the grid as a polygon.
Select the grids that don't match your points and delete them.
Use the Vector | Geometry Tools | Polygon Centroids tool to 'regain' the centroids
I realise that my method doesn't use your existing centroid points but I wonder if your existing points are already in a set pattern that would easily fit into a grid anyway.
gBuffer
from thergeos
package?