[R-sig-Geo] how to mask a raster with a coarser resolution selected grid squares ?

Arnald Marcer arnald.marcer at uab.cat
Fri Sep 17 13:15:57 CEST 2010


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


-- 
-----------------------------------------------------------------
Arnald Marcer
Investigador

Centre de Recerca Ecològica i Aplicacions Forestals (CREAF)
Universitat Autònoma de Barcelona
08193 Barcelona

Tel: 93 5811312
Fax: 93 5814151
-----------------------------------------------------------------
Arnald Marcer
Researcher

Centre for Ecological Research and Forestry Applications (CREAF)
Autonomous University of Barcelona
08193 Barcelona

Phone: +34 93 5811312
Fax:   +34 93 5814151



More information about the R-sig-Geo mailing list