Search code examples
rimage-processingaggregategisraster

Masking one raster by another that was aggregated to lower resolution


I have two rasters: one ("log") that I want to use to mask out pixels in the other ("roi").

> log
class       : RasterLayer 
dimensions  : 11957, 13462, 160965134  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 621465, 1025325, 8464135, 8822845  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\drappap\AppData\Local\Temp\Rtmp21P4c1\raster\r_tmp_2017-02-06_095634_10544_50628.grd 
names       : layer 
values      : 1.1, 1.1  (min, max)

> roi
class       : RasterLayer 
dimensions  : 30, 47, 1410  (nrow, ncol, ncell)
resolution  : 50, 50  (x, y)
extent      : 717330, 719680, 8641184, 8642684  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 2.132891, 14.65027  (min, max)

Log has higher resolution than roi, so before attempting to mask, I aggregate log up to the resolution of roi like so:

require(spatial.tools)
require(raster)
log.new<-spatial_sync_raster(log, roi, method="bilinear")
roi.new<-mask(roi, log.new, inverse=T)

I want to mask out any of the roi pixels that overlap even slightly with the log pixels. As you can see from the two images below, this isn't the case. The problem is that when I aggregate the log pixels from 30m to 50m (to match the roi pixels), some of the individual 30m log pixels (highlighted by pen in images below) are lost when aggregated up to 50m (and are therefore not cropped out in the roi image) because they don't make up a sufficiently large proportion of the 50m pixels.

How can I fix the code such that any roi pixels that overlap even slightly with the log pixels will be masked out?

enter image description here

enter image description here


Solution

  • you need to resample dataset to get the same resolution (either 30x30 or 50x50)

    rs<-resample(r1,r2,method='bilinear') # you can  choose your own method