[R-sig-Geo] Seeking example of making polygons from non-uniform grid

Michael Sumner mdsumner at utas.edu.au
Tue Jun 27 01:56:38 CEST 2006


Waichler, Scott R wrote:
> Hi,
>
> I have a rectangular grid with varying cell sizes in the x and y
> directions.  I am looking for an example on how to efficiently create sp
> polygon objects given a complete description of the grid.  For each
> cell, I have coordinates for the vertices and a value (z), which is my
> case is a categorical variable.  Previously Roger Bivand suggested how I
> could merge contiguous, like-valued cells into a single polygon, but the
> example assumed a regular grid with uniform dx and dy.
>
>   
Does this help? 

## bottom left corner of bottom left cell
llX <- 0
llY <- 0.5

## cell width and height
cellX <- 1
cellY <- 0.3

## number of cells
nX <- 10
nY <- 10

## z data - we have a z for every cell
zz <- rnorm(nX * nY)

## grid topology
cellsize <- c(cellX, cellY)
cellcentre.offset <- c(llX, llY) + cellsize/2

library(sp)
## define the grid topology, and convert to polygons
gt <- GridTopology(cellcentre.offset, cellsize, c(nX, nY))
polys <- as.SpatialPolygons.GridTopology(gt)

## add the data as a spatial polygons dataframe

poly.grd <- SpatialPolygonsDataFrame(polys, data.frame(z = zz, 
row.names=getSpPPolygonsIDSlots(polys)))

## compare to a SpGridDF version

grd <- SpatialGridDataFrame(gt, list(z = zz))
image(grd, axes = T)
plot(poly.grd, add = T)


sessionInfo()  ## I'm a little bit out of date
> Version 2.3.0 (2006-04-24)
> i386-pc-mingw32
>
> attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"     
> "datasets"
> [7] "base"    
>
> other attached packages:
>       sp
> "0.8-14"
>




More information about the R-sig-Geo mailing list