Skip to content

how to mask a raster with a coarser resolution selected grid squares ?

3 messages · Arnald Marcer, Edzer Pebesma

#
Dear listers,

Can anybody help me with this ?

How can I mask a 1km UTM grid raster with a list of coordinates
which represent the center of 10km UTM grid squares ? Ultimately
I want the 1km grid raster with only the pixels that fall inside
the 10km UTM grid squares.

Also, how can I build a SpatialPolygonsDataFrame representing 10km
UTM grid squares from a list of coordinates which represent their grid
center.

I will appreciate if you can give me some hints on how to do this.

Arnald Marcer
CREAF
#
On 09/17/2010 09:47 AM, Arnald Marcer wrote:
convert the first to SpatialPointsDataFrame, and create a SpatialPixels
object from the second, then use overlay() to get the indexes of the
points that lie within the 10km grid cells.
first create a SpatialPixelsDataFrame, say sx, then use

as(sx, "SpatialPolygonsDataFrame")

  
    
#
Dear listers,

Thanks to Robert and Edzer I finally found a way to do this. This is the
final solution I got to, in case anybody wants to use it:

##############################################
# This function returns a masks all zones of a 1km raster which
# do not fall inside 10km grid cells defined by a list of selected
# 10km-resolution cell centers. cell.centers must be a 
SpatialPointsDataFrame
##############################################
maskFromCoarserResolution <- function(r.1km, cell.centers.10k){
   fr1k <- paste(dirRefCarto,"catalonia_utm1k.asc",sep="")
   r1k <- raster(fr1k)
   r10k <-  aggregate(raster(r1k), 10)
   cells <- cellFromXY(r10k, cell.centers.10k)
   r10k[] <- NA
   r10k[cells] <- 1
   r10kto1k <- disaggregate(r10k, 10)
   extent.r1k <- extent(r1k)
   #in order to make sure both bounding boxes are equal
   r10kto1k <- crop(r10kto1k, extent.r1k)
   r1k.masked <- mask(r1k, r10kto1k)
}

Arnald Marcer
CREAF
On 09/17/2010 09:47 AM, Arnald Marcer wrote: