Wednesday, 10 January 2018

qgis - Clip raster by raster with data extraction and resolution change


I need to stack multiple maps (tif) together and clip them all according to extent of one small raster map (second raster at the top of picture below). However, the size of pixels in each map is different. Is there any way how to make process visualized bellow?


Please, answer only R-solution or QGIS-solution.


enter image description here


library(raster)
e1<-extent(c(0,6,0,6))
r1<-raster(nrows=3,ncols=3,ext=e1)
values(r1)<-c(0,4,1,0,1,1,1,2,3)

plot(r1)
e2<-extent(c(2,5,2,5))
r2<-raster(nrows=4,ncols=3,ext=e2)
values(r2)<-c(0,8,2,1,4,9,9,4,0,8,0,0)
plot(r2)

Answer



You can:


Load required libraries:


library(raster)
library(rgdal)


Read rasters:


r1 = raster("./dir/r1.tif")
r2 = raster("./dir/r2.tif")

Resample to the finer grid


r.new = resample(r1, r2, "bilinear")

If required (for masking), set extents to match


ex = extent(r1)

r2 = crop(r2, ex)

Removed unrequired data


r.new = mask(r.new, r2)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...