[R-sig-Geo] apply interpolation functions on a map

Jon Olav Skoien jon.skoien at jrc.ec.europa.eu
Wed Jul 23 11:53:09 CEST 2014


Hi Lorenzo,

I think you should be able to achieve what you are trying with mapply, 
passing function type and water depth for each pixel:

Cost[] <- mapply(FUN = function(dfun, wlevel) dfun(wlevel),
                              dfun = DDFun[getValues(LandUse)],
                              wlevel = getValues(WaterDepth))

I am not sure if this is the most efficient method if the raster is 
large though, it could even be that looping would be better. 
raster:::calc seems not to be able to take multiple functions as 
argument, otherwise it could have been a better option.

Cheers,
Jon

On 22-Jul-14 16:04, Lorenzo Alfieri wrote:
> Dear list,
> I can't find a solution to this problem:
> Let's say I have a map of water depth due to a flood , and the corresponding land-use:
>
> library(raster)
> WaterDepth<-raster(matrix(1:20, nrow = 5, ncol = 4))
> LandUse<-raster(WaterDepth)
> LandUse[]<-c(rep(1,12),rep(2,8))
>
> # each land use class has a different relation between water depth and damage, which is defined per points and that I can interpolate linearly:
> Damage<-data.frame(V1=c(0,100,150,230,340,340),V2=c(0,55,120,160,225,225))
> Depths<-c(0,0.5,1,2,4,100)     #Water depth in meters
> DDFun<-lapply(Damage, approxfun, x=Depths)
>
> # Now if I want to know the estimated cost of the damage on a specific grid point it seems to work:
> i<-3
> Cost<-raster(WaterDepth)
> Cost[i] <- DDFun[[LandUse[i]]](WaterDepth[i])
>
>> Cost[i]
>      
> 340
>
>
> But it doesn't work if I try to apply it to the entire map extent:
>
>> Cost[] <- DDFun[[LandUse[]]](WaterDepth[])
> Error in DDFun[[LandUse[]]] : recursive indexing failed at level 2
>
> Any suggestion on how to do this (possibly without looping on each land use class)?
>
> Thanks
>
> Lorenzo
>   		 	   		
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Environment and Sustainability (IES)
Climate Risk Management Unit

Via Fermi 2749, TP 100-01,  I-21027 Ispra (VA), ITALY

jon.skoien at jrc.ec.europa.eu
Tel:  +39 0332 789205

Disclaimer: Views expressed in this email are those of the individual and do not necessarily represent official views of the European Commission.



More information about the R-sig-Geo mailing list