[R-sig-Geo] clip a raster layer to a mask layer using raster algebra in R?

Bede-Fazekas Ákos bf@levli@t @ending from gm@il@com
Thu Dec 6 10:59:37 CET 2018


Dear Christian,
raster::mask() and raster::crop() may solve your problem.
https://www.rdocumentation.org/packages/raster/versions/2.8-4/topics/mask
https://www.rdocumentation.org/packages/raster/versions/2.8-4/topics/crop

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2018.12.06. 9:40 keltezéssel, Christian Willmes írta:
> Sorry, I had an error in the main command.
>
> It should be:
>
> gebco_clipped <- ifelse(RSLmask, gebco, NA)
>
> But it still gives the same error.
>
> Best,
> Christian
>
> Am 06.12.18 um 09:37 schrieb Christian Willmes:
>> Hello,
>>
>> this should be normally a very basic simple one-liner, but I can't 
>> get my head around it. I only find solutions for cliping a raster 
>> against a vector, but none for clipping a raster against a raster mask.
>>
>> I want to clip a layer to a mask. In GRASS GIS r.mapcalc the syntax is:
>>
>> result = if(RSLmask, gebco, null())
>>
>> This clips the GEBCO dataset to the extent of the given raster mask 
>> 'rslmask'. The result raster has heigt values within the raster mask 
>> extent, the rest of the raster layer has NULL or NA values.
>>
>> In R I did the following, but it does not work as expected:
>>
>> gebco <- raster(gebco2014) #load raster
>>
>> RSLmask <- gebco >= 0  #create mask layer
>>
>> RSL <- ifelse(RSLmask, 1, NA)
>>
>> The last command gives an error about vector type 'S4'?!
>>
>> >In is.na(test) :
>>   >is.na() auf nicht-(Liste oder Vektor) des Typs 'S4' angewendet
>>
>> So, in short: what is the R way to simply clip a raster to a raster 
>> mask?
>>
>> Thanks and best regards,
>> Christian
>>
>> PS: If found many solutions for clipping a raster to a vector 
>> polygon, but this is not what I want.
>>



More information about the R-sig-Geo mailing list