Note
Go to the end to download the full example code.
Mask from a vector#
This example demonstrates the creation of a mask from a vector using create_mask().
We open a raster and vector.
import geoutils as gu
filename_rast = gu.examples.get_path("everest_landsat_b4")
filename_vect = gu.examples.get_path("everest_rgi_outlines")
rast = gu.open_raster(filename_rast)
vect = gu.open_vector(filename_vect)
Let’s plot the raster and vector.
rast.rst.plot(cmap="Purples")
vect.vct.plot(ref=rast, fc="none", ec="k", lw=2)

First option: using the raster as a reference to match, we create a mask for the vector in any projection and georeferenced grid. We simply have to pass
the raster as single argument to rasterize(). See Match-reference operations for more details.
vect_rasterized = vect.vct.create_mask(rast)
vect_rasterized.rst.plot(ax="new")

Note
This is equivalent to using rasterize() with in_value=1 and out_value=0 and
will return a boolean xarray.DataArray.
Second option: we can pass any georeferencing parameter to create_mask(). Any unpassed attribute will be deduced from the
vector itself, except from the raster shape that will default to 1000 x 1000.
# vect_rasterized = vect.vct.create_mask(res=500)
# vect_rasterized.rst.plot()
Important
The raster shape or resolution are the only unknown arguments to rasterize a vector, one or the other can be passed.
Total running time of the script: (0 minutes 1.666 seconds)