Mask from raster with less RAM

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

Mask from raster with less RAM

Frederico Faleiro
Dear all,

I would like to generate a mask from a raster, but my workflow needs a lot
of RAM to process big rasters. I need this polygon mask to use in another's
rasters.
Do you know another approach that needs less RAM?

# reproducible example
library(raster)
# read data to create mask
r <- raster(system.file("external/test.grd", package="raster"))
r[!is.na(r)] <- 1
pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the data
# apply the mask in another raster
 r2 <- raster(extent(r), res(r))
r2[ ] <- 1
r2.mask <- mask(r2, pol)

Cheers!

--
Frederico Faleiro
Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
Department of Ecology | Institute of Biological Science | Federal
University of Goiás | Brazil
RG: https://www.researchgate.net/profile/Frederico_Faleiro
CV: http://lattes.cnpq.br/4926404840659003

        [[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: Mask from raster with less RAM

Stephen Stewart
Hi Frederico,

It may not solve all of your RAM issues, but in this situation I would skip
the rasterToPolygons (which is also usually very slow) and use raster math
to propagate NAs.

r <- raster(system.file("external/test.grd", package="raster"))
r[!is.na(r)] <- 1
# Can also be faster to do r = r / r, but add an offset (that cannot result
in 0) if you have valid 0s.
 r2 <- raster(extent(r), res(r))
r2[ ] <- 1
r2.mask <- r * r2

If you have a polygon to use as a mask, burn it in using the fasterize
package and then apply the above.

Hope that helps.

Cheers,

Steve

On Wed., 12 Aug. 2020, 6:43 am Frederico Faleiro, <[hidden email]>
wrote:

> Dear all,
>
> I would like to generate a mask from a raster, but my workflow needs a lot
> of RAM to process big rasters. I need this polygon mask to use in another's
> rasters.
> Do you know another approach that needs less RAM?
>
> # reproducible example
> library(raster)
> # read data to create mask
> r <- raster(system.file("external/test.grd", package="raster"))
> r[!is.na(r)] <- 1
> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> data
> # apply the mask in another raster
>  r2 <- raster(extent(r), res(r))
> r2[ ] <- 1
> r2.mask <- mask(r2, pol)
>
> Cheers!
>
> --
> Frederico Faleiro
> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
> Department of Ecology | Institute of Biological Science | Federal
> University of Goiás | Brazil
> RG: https://www.researchgate.net/profile/Frederico_Faleiro
> CV: http://lattes.cnpq.br/4926404840659003
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> 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
Stephen Stewart

PhD Student
School of Ecosystem and Forest Sciences
Faculty of Science
University of Melbourne
Reply | Threaded
Open this post in threaded view
|

Re: Mask from raster with less RAM

Frederico Faleiro
Hi guys, thank you for your reply.

Jaime, I have tried, but I can't install rgdal needed to use raster and
apparently other people too (
https://stackoverflow.com/questions/57617895/how-to-install-rgdal-and-or-upload-raster-on-google-collaboration).
Do you have a solution?
Hugo, I need the mask in vector format because the rasters have different
resolutions, so I can't use raster as a mask. I have modified the reprex to
be more precise about it (see below).
Steve, I think this approach has the same issue of Hugo Costa. I don't have
the polygon of the mask, so I am trying to create one to apply in the other
rasters of different resolutions.

# reproducible example
library(raster)
# read data to create mask
r <- raster(system.file("external/test.grd", package="raster"))
r[!is.na(r)] <- 1
pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the data
# raster of different resolution
res2 <- res(r) + 10
r2 <- raster(extent(r), resolution = res2)
r2[ ] <- 1
# apply the mask
r2.mask <- mask(r2, pol)
# plot
par(mfrow = c(1, 3))
plot(r)
plot(r2)
plot(r2.mask)

Cheers!

Em ter., 11 de ago. de 2020 às 19:26, Stephen Stewart <
[hidden email]> escreveu:

> Hi Frederico,
>
> It may not solve all of your RAM issues, but in this situation I would
> skip the rasterToPolygons (which is also usually very slow) and use raster
> math to propagate NAs.
>
> r <- raster(system.file("external/test.grd", package="raster"))
> r[!is.na(r)] <- 1
> # Can also be faster to do r = r / r, but add an offset (that cannot
> result in 0) if you have valid 0s.
>  r2 <- raster(extent(r), res(r))
> r2[ ] <- 1
> r2.mask <- r * r2
>
> If you have a polygon to use as a mask, burn it in using the fasterize
> package and then apply the above.
>
> Hope that helps.
>
> Cheers,
>
> Steve
>
> On Wed., 12 Aug. 2020, 6:43 am Frederico Faleiro, <[hidden email]>
> wrote:
>
>> Dear all,
>>
>> I would like to generate a mask from a raster, but my workflow needs a lot
>> of RAM to process big rasters. I need this polygon mask to use in
>> another's
>> rasters.
>> Do you know another approach that needs less RAM?
>>
>> # reproducible example
>> library(raster)
>> # read data to create mask
>> r <- raster(system.file("external/test.grd", package="raster"))
>> r[!is.na(r)] <- 1
>> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
>> data
>> # apply the mask in another raster
>>  r2 <- raster(extent(r), res(r))
>> r2[ ] <- 1
>> r2.mask <- mask(r2, pol)
>>
>> Cheers!
>>
>> --
>> Frederico Faleiro
>> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
>> Department of Ecology | Institute of Biological Science | Federal
>> University of Goiás | Brazil
>> RG: https://www.researchgate.net/profile/Frederico_Faleiro
>> CV: http://lattes.cnpq.br/4926404840659003
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> 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: Mask from raster with less RAM

Hugo Costa
Hi Frederico,
my first reaction is to project the raster object to be used as a mask to
the same resolution of the raster to be masked. Certainly it's faster then
and converting raster to vector. If the raster is big, gdalUtils::gdalwarp
can help in projecting your raster data.
Hope this helps
Hugo

Frederico Faleiro <[hidden email]> escreveu no dia quarta, 12/08/2020
à(s) 14:55:

> Hi guys, thank you for your reply.
>
> Jaime, I have tried, but I can't install rgdal needed to use raster and
> apparently other people too (
>
> https://stackoverflow.com/questions/57617895/how-to-install-rgdal-and-or-upload-raster-on-google-collaboration
> ).
> Do you have a solution?
> Hugo, I need the mask in vector format because the rasters have different
> resolutions, so I can't use raster as a mask. I have modified the reprex to
> be more precise about it (see below).
> Steve, I think this approach has the same issue of Hugo Costa. I don't have
> the polygon of the mask, so I am trying to create one to apply in the other
> rasters of different resolutions.
>
> # reproducible example
> library(raster)
> # read data to create mask
> r <- raster(system.file("external/test.grd", package="raster"))
> r[!is.na(r)] <- 1
> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> data
> # raster of different resolution
> res2 <- res(r) + 10
> r2 <- raster(extent(r), resolution = res2)
> r2[ ] <- 1
> # apply the mask
> r2.mask <- mask(r2, pol)
> # plot
> par(mfrow = c(1, 3))
> plot(r)
> plot(r2)
> plot(r2.mask)
>
> Cheers!
>
> Em ter., 11 de ago. de 2020 às 19:26, Stephen Stewart <
> [hidden email]> escreveu:
>
> > Hi Frederico,
> >
> > It may not solve all of your RAM issues, but in this situation I would
> > skip the rasterToPolygons (which is also usually very slow) and use
> raster
> > math to propagate NAs.
> >
> > r <- raster(system.file("external/test.grd", package="raster"))
> > r[!is.na(r)] <- 1
> > # Can also be faster to do r = r / r, but add an offset (that cannot
> > result in 0) if you have valid 0s.
> >  r2 <- raster(extent(r), res(r))
> > r2[ ] <- 1
> > r2.mask <- r * r2
> >
> > If you have a polygon to use as a mask, burn it in using the fasterize
> > package and then apply the above.
> >
> > Hope that helps.
> >
> > Cheers,
> >
> > Steve
> >
> > On Wed., 12 Aug. 2020, 6:43 am Frederico Faleiro, <[hidden email]>
> > wrote:
> >
> >> Dear all,
> >>
> >> I would like to generate a mask from a raster, but my workflow needs a
> lot
> >> of RAM to process big rasters. I need this polygon mask to use in
> >> another's
> >> rasters.
> >> Do you know another approach that needs less RAM?
> >>
> >> # reproducible example
> >> library(raster)
> >> # read data to create mask
> >> r <- raster(system.file("external/test.grd", package="raster"))
> >> r[!is.na(r)] <- 1
> >> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> >> data
> >> # apply the mask in another raster
> >>  r2 <- raster(extent(r), res(r))
> >> r2[ ] <- 1
> >> r2.mask <- mask(r2, pol)
> >>
> >> Cheers!
> >>
> >> --
> >> Frederico Faleiro
> >> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
> >> Department of Ecology | Institute of Biological Science | Federal
> >> University of Goiás | Brazil
> >> RG: https://www.researchgate.net/profile/Frederico_Faleiro
> >> CV: http://lattes.cnpq.br/4926404840659003
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> _______________________________________________
> >> 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
>

        [[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: Mask from raster with less RAM

Roger Bivand
Administrator
In reply to this post by Frederico Faleiro
On Wed, 12 Aug 2020, Frederico Faleiro wrote:

> Hi guys, thank you for your reply.
>
> Jaime, I have tried, but I can't install rgdal needed to use raster and
> apparently other people too (
> https://stackoverflow.com/questions/57617895/how-to-install-rgdal-and-or-upload-raster-on-google-collaboration).
> Do you have a solution?

Use CRAN Windows or MacOS binaries, or install the system requirements.
Unless you can do that, for example because you do not control the
platform you are using (are you working on a cloud instance?), do not
install packages needing external software
from source.

> Hugo, I need the mask in vector format because the rasters have different
> resolutions, so I can't use raster as a mask. I have modified the reprex to
> be more precise about it (see below).

Please use GRASS. What you are trying to do is something that has "just
worked" in GRASS since it was first created. GRASS can be run from the
shell, from Python and from R (rgrass7). Or use SAGA, another fast raster
processor. Both mask from raster directly. Write a shell script for GRASS
to resample your rasters, mask them, and complete. You can also use GRASS
and SAGA from QGIS. You might use Python or R to make file name handling
"easier" than in a script. Neither GRASS nor SAGA use much memory unless
rasters are huge, and then they are lean.

Roger

> Steve, I think this approach has the same issue of Hugo Costa. I don't have
> the polygon of the mask, so I am trying to create one to apply in the other
> rasters of different resolutions.
>
> # reproducible example
> library(raster)
> # read data to create mask
> r <- raster(system.file("external/test.grd", package="raster"))
> r[!is.na(r)] <- 1
> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the data
> # raster of different resolution
> res2 <- res(r) + 10
> r2 <- raster(extent(r), resolution = res2)
> r2[ ] <- 1
> # apply the mask
> r2.mask <- mask(r2, pol)
> # plot
> par(mfrow = c(1, 3))
> plot(r)
> plot(r2)
> plot(r2.mask)
>
> Cheers!
>
> Em ter., 11 de ago. de 2020 às 19:26, Stephen Stewart <
> [hidden email]> escreveu:
>
>> Hi Frederico,
>>
>> It may not solve all of your RAM issues, but in this situation I would
>> skip the rasterToPolygons (which is also usually very slow) and use raster
>> math to propagate NAs.
>>
>> r <- raster(system.file("external/test.grd", package="raster"))
>> r[!is.na(r)] <- 1
>> # Can also be faster to do r = r / r, but add an offset (that cannot
>> result in 0) if you have valid 0s.
>>  r2 <- raster(extent(r), res(r))
>> r2[ ] <- 1
>> r2.mask <- r * r2
>>
>> If you have a polygon to use as a mask, burn it in using the fasterize
>> package and then apply the above.
>>
>> Hope that helps.
>>
>> Cheers,
>>
>> Steve
>>
>> On Wed., 12 Aug. 2020, 6:43 am Frederico Faleiro, <[hidden email]>
>> wrote:
>>
>>> Dear all,
>>>
>>> I would like to generate a mask from a raster, but my workflow needs a lot
>>> of RAM to process big rasters. I need this polygon mask to use in
>>> another's
>>> rasters.
>>> Do you know another approach that needs less RAM?
>>>
>>> # reproducible example
>>> library(raster)
>>> # read data to create mask
>>> r <- raster(system.file("external/test.grd", package="raster"))
>>> r[!is.na(r)] <- 1
>>> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
>>> data
>>> # apply the mask in another raster
>>>  r2 <- raster(extent(r), res(r))
>>> r2[ ] <- 1
>>> r2.mask <- mask(r2, pol)
>>>
>>> Cheers!
>>>
>>> --
>>> Frederico Faleiro
>>> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
>>> Department of Ecology | Institute of Biological Science | Federal
>>> University of Goiás | Brazil
>>> RG: https://www.researchgate.net/profile/Frederico_Faleiro
>>> CV: http://lattes.cnpq.br/4926404840659003
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> 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
>
--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: Mask from raster with less RAM

Frederico Faleiro
Hi guys,

I really need to use the mask without modifying the original resolution of
the rasters, so vectorizing was my first try. I know I can resample first
before applying a raster mask, but this will not work for my objectives
that involve creating some visualisations to the same area with the
original resolution.
Roger, I have tried use the google colab in the cloud (e.g.
https://colab.research.google.com/drive/1BYnnbqeyZAlYnxR9IHC8tpW07EpDeyKR#scrollTo=uaE0kZ0vkqms)
and I think you are right about the restriction about the use of external
softwares like GDAL in this specific case.
Do you know if is it possible to do it in GRASS or QGIS? I only find the
function polygonize (as in raster::rasterToContour) that generates contours
around equal values from the raster.

Cheers,


Em qua., 12 de ago. de 2020 às 13:45, Roger Bivand <[hidden email]>
escreveu:

> On Wed, 12 Aug 2020, Frederico Faleiro wrote:
>
> > Hi guys, thank you for your reply.
> >
> > Jaime, I have tried, but I can't install rgdal needed to use raster and
> > apparently other people too (
> >
> https://stackoverflow.com/questions/57617895/how-to-install-rgdal-and-or-upload-raster-on-google-collaboration
> ).
> > Do you have a solution?
>
> Use CRAN Windows or MacOS binaries, or install the system requirements.
> Unless you can do that, for example because you do not control the
> platform you are using (are you working on a cloud instance?), do not
> install packages needing external software
> from source.
>
> > Hugo, I need the mask in vector format because the rasters have different
> > resolutions, so I can't use raster as a mask. I have modified the reprex
> to
> > be more precise about it (see below).
>
> Please use GRASS. What you are trying to do is something that has "just
> worked" in GRASS since it was first created. GRASS can be run from the
> shell, from Python and from R (rgrass7). Or use SAGA, another fast raster
> processor. Both mask from raster directly. Write a shell script for GRASS
> to resample your rasters, mask them, and complete. You can also use GRASS
> and SAGA from QGIS. You might use Python or R to make file name handling
> "easier" than in a script. Neither GRASS nor SAGA use much memory unless
> rasters are huge, and then they are lean.
>
> Roger
>
> > Steve, I think this approach has the same issue of Hugo Costa. I don't
> have
> > the polygon of the mask, so I am trying to create one to apply in the
> other
> > rasters of different resolutions.
> >
> > # reproducible example
> > library(raster)
> > # read data to create mask
> > r <- raster(system.file("external/test.grd", package="raster"))
> > r[!is.na(r)] <- 1
> > pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> data
> > # raster of different resolution
> > res2 <- res(r) + 10
> > r2 <- raster(extent(r), resolution = res2)
> > r2[ ] <- 1
> > # apply the mask
> > r2.mask <- mask(r2, pol)
> > # plot
> > par(mfrow = c(1, 3))
> > plot(r)
> > plot(r2)
> > plot(r2.mask)
> >
> > Cheers!
> >
> > Em ter., 11 de ago. de 2020 às 19:26, Stephen Stewart <
> > [hidden email]> escreveu:
> >
> >> Hi Frederico,
> >>
> >> It may not solve all of your RAM issues, but in this situation I would
> >> skip the rasterToPolygons (which is also usually very slow) and use
> raster
> >> math to propagate NAs.
> >>
> >> r <- raster(system.file("external/test.grd", package="raster"))
> >> r[!is.na(r)] <- 1
> >> # Can also be faster to do r = r / r, but add an offset (that cannot
> >> result in 0) if you have valid 0s.
> >>  r2 <- raster(extent(r), res(r))
> >> r2[ ] <- 1
> >> r2.mask <- r * r2
> >>
> >> If you have a polygon to use as a mask, burn it in using the fasterize
> >> package and then apply the above.
> >>
> >> Hope that helps.
> >>
> >> Cheers,
> >>
> >> Steve
> >>
> >> On Wed., 12 Aug. 2020, 6:43 am Frederico Faleiro, <[hidden email]>
> >> wrote:
> >>
> >>> Dear all,
> >>>
> >>> I would like to generate a mask from a raster, but my workflow needs a
> lot
> >>> of RAM to process big rasters. I need this polygon mask to use in
> >>> another's
> >>> rasters.
> >>> Do you know another approach that needs less RAM?
> >>>
> >>> # reproducible example
> >>> library(raster)
> >>> # read data to create mask
> >>> r <- raster(system.file("external/test.grd", package="raster"))
> >>> r[!is.na(r)] <- 1
> >>> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> >>> data
> >>> # apply the mask in another raster
> >>>  r2 <- raster(extent(r), res(r))
> >>> r2[ ] <- 1
> >>> r2.mask <- mask(r2, pol)
> >>>
> >>> Cheers!
> >>>
> >>> --
> >>> Frederico Faleiro
> >>> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/
> )
> >>> Department of Ecology | Institute of Biological Science | Federal
> >>> University of Goiás | Brazil
> >>> RG: https://www.researchgate.net/profile/Frederico_Faleiro
> >>> CV: http://lattes.cnpq.br/4926404840659003
> >>>
> >>>         [[alternative HTML version deleted]]
> >>>
> >>> _______________________________________________
> >>> 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
> >
>
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; e-mail: [hidden email]
> https://orcid.org/0000-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en

        [[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: Mask from raster with less RAM

Sourav Sarkar
In reply to this post by Frederico Faleiro
Dear Frederico,
With the very little experience that I have with raster data, it seems to
me (maybe I am wrong) that `crop' is computationally easier than `mask'. If
the crop command serves your purpose, maybe you can try:
r2.mask <- crop(r2, pol)

Kind regards,

বুধ, ১২ আগস্ট, ২০২০ তারিখে ২:১৩ AM টায় এ Frederico Faleiro <
[hidden email]> লিখেছেন:

> Dear all,
>
> I would like to generate a mask from a raster, but my workflow needs a lot
> of RAM to process big rasters. I need this polygon mask to use in another's
> rasters.
> Do you know another approach that needs less RAM?
>
> # reproducible example
> library(raster)
> # read data to create mask
> r <- raster(system.file("external/test.grd", package="raster"))
> r[!is.na(r)] <- 1
> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
> data
> # apply the mask in another raster
>  r2 <- raster(extent(r), res(r))
> r2[ ] <- 1
> r2.mask <- mask(r2, pol)
>
> Cheers!
>
> --
> Frederico Faleiro
> Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
> Department of Ecology | Institute of Biological Science | Federal
> University of Goiás | Brazil
> RG: https://www.researchgate.net/profile/Frederico_Faleiro
> CV: http://lattes.cnpq.br/4926404840659003
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


--
Sourav Sarkar,
Assistant Professor,
School of Arts and Sciences,
Ahmedabad University

        [[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: Mask from raster with less RAM

Frederico Faleiro
Hi guys,

thank you for your help. Sometimes we really need more RAM. I got access to
a cluster with more RAM and solved the problem.
Dear Sourav, you are right, but the crop only cuts the extent of the
rasters, so I really need to use mask. In addition, my main limitation was
in generating the polygon from raster (i.e. rasterToPolygons).

Cheers!

Em qua., 12 de ago. de 2020 às 15:20, Sourav Sarkar <
[hidden email]> escreveu:

> Dear Frederico,
> With the very little experience that I have with raster data, it seems to
> me (maybe I am wrong) that `crop' is computationally easier than `mask'. If
> the crop command serves your purpose, maybe you can try:
> r2.mask <- crop(r2, pol)
>
> Kind regards,
>
> বুধ, ১২ আগস্ট, ২০২০ তারিখে ২:১৩ AM টায় এ Frederico Faleiro <
> [hidden email]> লিখেছেন:
>
>> Dear all,
>>
>> I would like to generate a mask from a raster, but my workflow needs a lot
>> of RAM to process big rasters. I need this polygon mask to use in
>> another's
>> rasters.
>> Do you know another approach that needs less RAM?
>>
>> # reproducible example
>> library(raster)
>> # read data to create mask
>> r <- raster(system.file("external/test.grd", package="raster"))
>> r[!is.na(r)] <- 1
>> pol <- rasterToPolygons(r, dissolve = T) #  a lot of RAM to process the
>> data
>> # apply the mask in another raster
>>  r2 <- raster(extent(r), res(r))
>> r2[ ] <- 1
>> r2.mask <- mask(r2, pol)
>>
>> Cheers!
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
> --
> Sourav Sarkar,
> Assistant Professor,
> School of Arts and Sciences,
> Ahmedabad University
>


--
Frederico Faleiro
Postdoctoral Researcher in the INCT-EECBio (https://www.eecbio.ufg.br/)
Department of Ecology | Institute of Biological Science | Federal
University of Goiás | Brazil
RG: https://www.researchgate.net/profile/Frederico_Faleiro
CV: http://lattes.cnpq.br/4926404840659003

        [[alternative HTML version deleted]]

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