[GRASS-user] Clip a raster using a polygon boundary with r.mask

Micha Silver tsvibar at gmail.com
Wed Jul 13 10:57:08 PDT 2022


Hi

You need a few more steps. Like many processes in GRASS, things look 
complicated at the beginning. But that's because you have full control 
of what you're doing.


The r.mask module is used to block out certain regions of a raster. You 
set the mask to an existing map (either vector or raster). Then you have 
to create the new, masked raster. So in this case:


# As always in GRASS set the computational region

g.region -ap rast=big_raster

# Create the mask

r.mask vector=small_vector

# Now create the masked raster

r.mapcalc "masked_raster = big_raster"


However, you mentioned "clipo a raster" in your question. So the above 
isn't enough. In order to actually clip to the bounds of the 
small_vector, you also must reset the computational region to the extent 
of the vector, So:


# As always in GRASS set the computational region

g.region -ap rast=big_raster  # to make sure the resolution matches the 
original raster

# But now reset the region to the vector

g.region -ap vect=small_vector

# Create the mask

r.mask vector=small_vector

# Now create the masked and clipped raster

r.mapcalc "clipped_raster = big_raster"


If you examine the two rasters created above (i.e. r.info... ) you'll 
see the difference

HTH



On 13/07/2022 17:35, Asim via grass-user wrote:
> Hello!
>
> I've recently started using GRASS and finding it very cool.  The only 
> other GIS software I'm familiar with is QGIS.
>
> Today's newbie question is how to clip a raster using a vector having 
> polygon geometries?  Judging by the docs, r.mask should be used with 
> "vector=..." parameter.  The command "r.mask raster=big_raster 
> vector=smaller_vector" was successful, mask was created.  However, 
> when "big_raster" was added to the map layers in GUI, it was rendered 
> in its entirety, without getting clipped. An operation such as 
> r.to.vect on big_raster also resulted in the entire raster being used.
>
> What am I missing?  Are there examples illustrating the use of 
> "vector=" parameter in r.mask?
>
> Asim
>
>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user

-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918



More information about the grass-user mailing list