clip a raster layer to a mask layer using raster algebra in R?

Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

clip a raster layer to a mask layer using raster algebra in R?

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.

--
Dr. Christian Willmes
AG GIS & Fernerkundung      | GIS & RS Group
Geographisches Institut     | Institute of Geography
Universität zu Köln         | University of Cologne
Tel.: +49 (0)221 470 6234
http://www.geographie.uni-koeln.de/14126.html
http://www.sfb806.de
http://crc806db.uni-koeln.de
http://publons.com/a/1316706/
http://orcid.org/0000-0002-5566-6542

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

Christian Willmes
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.
>
--
Dr. Christian Willmes
AG GIS & Fernerkundung      | GIS & RS Group
Geographisches Institut     | Institute of Geography
Universität zu Köln         | University of Cologne
Tel.: +49 (0)221 470 6234
http://www.geographie.uni-koeln.de/14126.html
http://www.sfb806.de
http://crc806db.uni-koeln.de
http://publons.com/a/1316706/
http://orcid.org/0000-0002-5566-6542

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

Bede-Fazekas Ákos
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.
>>

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

Christian Willmes
Thank you.

It seems that both approaches need an additional, very expensive
computation (takes several minutes on a i7-4790 with SSD) to create the
mask or the extent, and in my case the mask computed by this command
below is already what I want. Only values above a given RSL (Sealevel)
height. The rest of the raster needs to be NA or NULL.

 >gebco_rsl <- gebco
 >gebco_rsl[gebco < rsl] <- NA

....

The easy (and faster) approach for creating a mask:

 >gebco_mask <- gebco >= rsl

writes 0's into the remaining cells, which does not work for me (and the
mask() function).

On the other hand, the GRASS mapcalc computation (given below) takes
less than a minute on that same raster.

I wanted to re-implement the whole analysis in R for better/easier
reproducibility, but now I think I will stick to R + GRASS GIS.

If anyone knows a faster approach for creating a mask in R, please let
me know.

Thanks and best regards,
Christian

Am 06.12.18 um 10:59 schrieb Bede-Fazekas Ákos:

> 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.
>>>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

--
Dr. Christian Willmes
AG GIS & Fernerkundung      | GIS & RS Group
Geographisches Institut     | Institute of Geography
Universität zu Köln         | University of Cologne
Tel.: +49 (0)221 470 6234
http://www.geographie.uni-koeln.de/14126.html
http://www.sfb806.de
http://crc806db.uni-koeln.de
http://publons.com/a/1316706/
http://orcid.org/0000-0002-5566-6542

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

Hugo Costa
Possibly there are alternatives, like exploring raster::overlay together
with raster:clusterR
Hugo

Christian Willmes <[hidden email]> escreveu no dia quinta,
6/12/2018 à(s) 13:34:

> Thank you.
>
> It seems that both approaches need an additional, very expensive
> computation (takes several minutes on a i7-4790 with SSD) to create the
> mask or the extent, and in my case the mask computed by this command
> below is already what I want. Only values above a given RSL (Sealevel)
> height. The rest of the raster needs to be NA or NULL.
>
>  >gebco_rsl <- gebco
>  >gebco_rsl[gebco < rsl] <- NA
>
> ....
>
> The easy (and faster) approach for creating a mask:
>
>  >gebco_mask <- gebco >= rsl
>
> writes 0's into the remaining cells, which does not work for me (and the
> mask() function).
>
> On the other hand, the GRASS mapcalc computation (given below) takes
> less than a minute on that same raster.
>
> I wanted to re-implement the whole analysis in R for better/easier
> reproducibility, but now I think I will stick to R + GRASS GIS.
>
> If anyone knows a faster approach for creating a mask in R, please let
> me know.
>
> Thanks and best regards,
> Christian
>
> Am 06.12.18 um 10:59 schrieb Bede-Fazekas Ákos:
> > 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.
> >>>
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> --
> Dr. Christian Willmes
> AG GIS & Fernerkundung      | GIS & RS Group
> Geographisches Institut     | Institute of Geography
> Universität zu Köln         | University of Cologne
> Tel.: +49 (0)221 470 6234
> http://www.geographie.uni-koeln.de/14126.html
> http://www.sfb806.de
> http://crc806db.uni-koeln.de
> http://publons.com/a/1316706/
> http://orcid.org/0000-0002-5566-6542
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

obrl soil
In reply to this post by Christian Willmes
Hello,

in R you're working without the benefit of the GRASS 'region' concept
for raster work (and all the hidden helper code that goes with it), so
its never going to be a one liner - but the following may help.

You can use raster algebra in R to do masking. Construct your mask so
that the cells you want to keep are 0 and the cells you want to lose
are NA.

Then you can just add the raster and its mask together:

masked_raster <- raster + mask

because data + 0 = data, and data + NA = NA. This will still work even
if the two rasters don't have the same extent, so long as they have
the same cell size and alignment. The operation will throw a warning
but will complete successfully, returning the intersection of the two
objects. This should complete fairly quickly if the mask extent is
small relative to the target dataset. Try using raster::trim() on the
mask beforehand if it has a lot of whitespace.

HTH
@obrl_soil

On Thu, Dec 6, 2018 at 6:38 PM Christian Willmes <[hidden email]> wrote:

>
> 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.
>
> --
> Dr. Christian Willmes
> AG GIS & Fernerkundung      | GIS & RS Group
> Geographisches Institut     | Institute of Geography
> Universität zu Köln         | University of Cologne
> Tel.: +49 (0)221 470 6234
> http://www.geographie.uni-koeln.de/14126.html
> http://www.sfb806.de
> http://crc806db.uni-koeln.de
> http://publons.com/a/1316706/
> http://orcid.org/0000-0002-5566-6542
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: clip a raster layer to a mask layer using raster algebra in R?

Christian Willmes
Thank you.

But the most expensive step is constructing the mask with NA values.

 >gebco_rsl[gebco < rsl] <- NA

The rest of the computations is quite fast indeed... Do you know a
faster alternative, for creating the mask, to the line above?

Best,
Christian

Am 09.12.18 um 09:59 schrieb obrl soil:

> Hello,
>
> in R you're working without the benefit of the GRASS 'region' concept
> for raster work (and all the hidden helper code that goes with it), so
> its never going to be a one liner - but the following may help.
>
> You can use raster algebra in R to do masking. Construct your mask so
> that the cells you want to keep are 0 and the cells you want to lose
> are NA.
>
> Then you can just add the raster and its mask together:
>
> masked_raster <- raster + mask
>
> because data + 0 = data, and data + NA = NA. This will still work even
> if the two rasters don't have the same extent, so long as they have
> the same cell size and alignment. The operation will throw a warning
> but will complete successfully, returning the intersection of the two
> objects. This should complete fairly quickly if the mask extent is
> small relative to the target dataset. Try using raster::trim() on the
> mask beforehand if it has a lot of whitespace.
>
> HTH
> @obrl_soil
>
> On Thu, Dec 6, 2018 at 6:38 PM Christian Willmes <[hidden email]> wrote:
>> 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.
>>
>> --
>> Dr. Christian Willmes
>> AG GIS & Fernerkundung      | GIS & RS Group
>> Geographisches Institut     | Institute of Geography
>> Universität zu Köln         | University of Cologne
>> Tel.: +49 (0)221 470 6234
>> http://www.geographie.uni-koeln.de/14126.html
>> http://www.sfb806.de
>> http://crc806db.uni-koeln.de
>> http://publons.com/a/1316706/
>> http://orcid.org/0000-0002-5566-6542
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

--
Dr. Christian Willmes
AG GIS & Fernerkundung      | GIS & RS Group
Geographisches Institut     | Institute of Geography
Universität zu Köln         | University of Cologne
Tel.: +49 (0)221 470 6234
http://www.geographie.uni-koeln.de/14126.html
http://www.sfb806.de
http://crc806db.uni-koeln.de
http://publons.com/a/1316706/
http://orcid.org/0000-0002-5566-6542

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo