Question

I would like to resample a raster from a high resolution to a low resolution (with different extent) in a defined grid cell. Is there a way of using an existing raster file as input for the snapping?

In the raster package, aggregate and resample seem to be adequate but I can't find how to do it.

Was it helpful?

Solution 2

You can launch an external command with system and call a gdal_translate or gdal_warp command. This requires of course the installation of gdal utilities

OTHER TIPS

You can use projectRaster for this if you have a raster in one projection and resolution and you need output in a different particular resolution and projetion.

The from argument is your high resolution raster and the to argument is your low res raster. Make sure you choose the correct method for aggregation (i.e. bilinear for continuous data and ngb (nearest neighbour) for categorical data.

require( raster )

#  Projection info
proj1 <- CRS("+proj=laea +lon_0=20 +lat_0=5 +ellps=sphere +unit=km +to_meter=1e3")
proj2 <-  CRS("+proj=longlat +datum=WGS84 +ellps=WGS84")
#  High res raster
r1km <- raster( nrows = 1515 , ncols = 2300 , xmn = -4000 , xmx = -1700 , ymn = -15 , ymx = 1500 , crs = proj1 )

#  Low res raster
r5km <- raster( nrows = 303 , ncols = 460 , xmn = -20 , xmx = -5 , ymn = 4 , ymx = 15 , crs = proj2 )

#  Set some values in high res raster
pts <- rasterToPoints(r1km)
values( r1km ) <-  0.01*pts[,1] + sin(0.02*pi*pts[,2])

#  Reproject using the attributes of the low res raster for output
out <- projectRaster( from = r1km , to = r5km , method = "bilinear" )

#  Plot - extent of second raster doesn't fully cover first so some data is missing
par( mfrow = c(1,2) )
plot( r1km )
plot( out )

enter image description here

If your input and output data are the same except in resolution you can use aggregate...

#  If same extent and resolution require use aggregate
r1 <- raster(system.file("external/rlogo.grd", package="raster"))
r5 <- aggregate( r1 , fact = 5 , method = "bilinear" )
par( mfrow = c(1,2) )
plot( r1 )
plot( r5 )

enter image description here

This solution works :

system(paste("gdalwarp"
,paste(dir_path,"fileHR.tif",sep="")
,paste(dir_path,"fileLR.tif",sep=""),sep=" "))

where dir_path is the directory where you file are stored, fileHR.tif is the High resolution file, fileLR.tif is the low resoltion file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top