[R-sig-Geo] Correctly aligning two polygon grids

Robert J. Hijmans r.hijmans at gmail.com
Fri Aug 14 00:06:58 CEST 2009


Kenneth,

This is how I would do it with the raster package (
install.packages("raster", repos="http://R-Forge.R-project.org")

library(raster)
r100 <- raster(extent(1924835, 1926426, 484673, 485490))
res(r100) <- 100
r50 <- disaggregate(r100,2)
r200 <- aggregate(r100,2)

#to make a map
# here you could perhaps use the "polygon grid" values?
r100[] <- 1:ncell(r100)
plot(r100)

Robert


On Thu, Aug 13, 2009 at 11:13 AM, Roger Bivand<Roger.Bivand at nhh.no> wrote:
> On Thu, 13 Aug 2009, Kenneth Takagi wrote:
>
>>
>> Hi,
>>
>> I would like to align two polygon grids, one with 100x100 cells and the
>> other with 50x50 cells.  The idea is that they would align so that 4 50x50
>> cells would fall within one 100x100 cell.  Preferably, they would have a
>> common coordinate origin so that I could also make a grid of 200x200 cells
>> and they would also align properly.  It seems to be something to do with
>> where the centroids of each grid cell is placed? Below is a reproducible
>> example of where I am at.  I'm new with the sp package so sorry for the
>> length of the script!  I'm using R 9.1 with XP. Thanks!
>
> It gets much simpler if you start from the GridTopology object:
>
> x.range <- c(1924835, 1926426)
> y.range <- c(484673, 485490)
> cell.size <- 100
> cellcentre.offset <- c(x.range[1], y.range[1])+(cell.size/2)
> cellsize <- rep(cell.size, 2)
> cells.dim <- c(ceiling(abs(diff(x.range))/cell.size),
>  ceiling(abs(diff(y.range))/cell.size))
> GRD100 <- GridTopology(cellcentre.offset, cellsize, cells.dim)
> pl100 <- as.SpatialPolygons.SpatialPixels(as(SpatialGrid(GRD100),
>  "SpatialPixels"))
> cell.size <- 50
> cellcentre.offset <- c(x.range[1], y.range[1])+(cell.size/2)
> cellsize <- rep(cell.size, 2)
> cells.dim <- c(ceiling(abs(diff(x.range))/cell.size),
>  ceiling(abs(diff(y.range))/cell.size))
> GRD50 <- GridTopology(cellcentre.offset, cellsize, cells.dim)
> pl50 <- as.SpatialPolygons.SpatialPixels(as(SpatialGrid(GRD50),
>  "SpatialPixels"))
> plot(pl50, border="magenta")
> plot(pl100, add=TRUE)
> bbox(pl50)
> bbox(pl100)
>
> This lets you tesselate the same area, but using different offsets for the
> anchoring south-west cell, giving matching bounding boxes (the north edge
> doesn't match here, as the number of cells chosen to include the top of the
> range differs).
>
> Hope this helps,
>
> Roger
>
>>
>> Code:
>>
>> ### Range of coordinate space
>> x.range = c(1924835, 1926426)
>> y.range = c(484673, 485490)
>>
>> ### Create 100x100 grid
>> cell.size = 100
>> grd100 <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=cell.size),
>>    y=seq(from=y.range[1], to=y.range[2], by=cell.size))
>> coordinates(grd100) <- ~ x+y
>> gridded(grd100) <- TRUE
>> grd100.SP <- as.SpatialPolygons.SpatialPixels(grd100)
>> centroids <- coordinates(grd100.SP)
>> x <- centroids[,1]
>> y <- centroids[,2]
>> row.names100 <- sapply(slot(grd100.SP, "polygons"), function(i) slot(i,
>> "ID"))
>> grd100.SPDF <- SpatialPolygonsDataFrame(grd100.SP,
>>    data=data.frame(x=x, y=y, row.names=row.names100))
>>
>>
>> ### Create 50x50 grid
>> cell.size = 50
>> grd50 <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=cell.size),
>>    y=seq(from=y.range[1], to=y.range[2], by=cell.size))
>> coordinates(grd50) <- ~ x+y
>> gridded(grd50) <- TRUE
>> grd50.SP<-as.SpatialPolygons.SpatialPixels(grd50)
>> centroids <- coordinates(grd50.SP)
>> x <- centroids[,1]
>> y <- centroids[,2]
>> row.names50 <- sapply(slot(grd50.SP, "polygons"), function(i) slot(i,
>> "ID"))
>> grd50.SPDF <- SpatialPolygonsDataFrame(grd50.SP,
>>    data=data.frame(x=x, y=y, row.names=row.names50))
>>
>>
>> ### Plot both grids
>> s100 <- list("sp.lines", as(grd100.SPDF, "SpatialLinesDataFrame"),
>> "magenta",
>>   lwd=2)
>> spplot(as(grd50.SPDF, "SpatialLinesDataFrame"), "x", col.regions="black",
>>   sp.layout=list(s100),colorkey = F)
>>
>> _________________________________________________________________
>>
>>
>> _sync:082009
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> --
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand at nhh.no
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list