⚠️ Our 0.1 release refactored several early-development functions for long-term stability, to update your code see here. ⚠️
Future changes will come with deprecation warnings! 🙂

Rasterize a vector

Rasterize a vector#

This example demonstrates the rasterizing of a vector using geoutils.Vector.rasterize().

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.Raster(filename_rast)
vect = gu.Vector(filename_vect)

Let’s plot the raster and vector.

rast.plot(cmap="Purples")
vect.plot(ref_crs=rast, fc="none", ec="k", lw=2)
rasterize

First option: using the raster as a reference to match, we rasterize the vector in any projection and georeferenced grid. We simply have to pass the Raster as single argument to rasterize(). See Match-reference functionality for more details.

rasterize

By default, rasterize() will burn the index of the Vector’s features in their geometry. We can specify the in_value to burn a single value, or any iterable with the same length as there are features in the Vector. An out_value can be passed to burn outside the geometries.

rasterize

Note

If the rasterized in_value is fixed to 1 and out_value to 0 (default), then rasterize() is creating a boolean mask. This is equivalent to using create_mask(), and will return a Mask.

Mask(
  data=[[ True  True  True ...  True  True  True]
        [ True  True  True ...  True  True  True]
        [ True  True  True ...  True  True  True]
        ...
        [False False False ... False  True  True]
        [False False False ... False  True  True]
        [False False False ...  True  True  True]]
  transform=| 30.00, 0.00, 478000.00|
            | 0.00,-30.00, 3108140.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32645)


Second option: we can pass any georeferencing parameter to rasterize(). Any unpassed attribute will be deduced from the Vector itself, except from the shape to rasterize that will default to 1000 x 1000.

# vect_rasterized = vect.rasterize(xres=500)
# vect_rasterized.plot()

Important

The shape or the res are the only unknown arguments to rasterize a Vector, one or the other can be passed.

Total running time of the script: (0 minutes 0.498 seconds)

Gallery generated by Sphinx-Gallery