0

I have to transform the list of polygons that is in vector form into a raster mask that should be geographically aligned with the NDVI image that I have. I was advised to use rasterio.features.rasterize. But I am not sure how to use the function.

  • 1
    The rasterio docs are unfortunately not building on readthedocs at the moment, but you can find the same info in the docstring for rasterize. Post any code you've tried if you need more specific help. – mikewatt Mar 25 at 19:09
  • I don't really understand what the arguments should be. Can you give me a hand? – mandella Mar 25 at 19:20
  • Sure, I posted an answer elaborating a bit – mikewatt Mar 25 at 19:36
2
shapes : iterable of (geometry, value) pairs or iterable over
    geometries. `geometry` can either be an object that implements
    the geo interface or GeoJSON-like object.

A list, tuple, or generator of shapely geometries (or other GeoJSON-y objects as described, but shapely is easy). If you want them to be burnt in with the same default_value, then this is all you need in the list. If you have a specific value to burn for each geometry, then your list needs to pair the value with the geometry. e.g. [(geom0, 10), (geom1, 20), (geom2, 33)]. Sounds like you want a boolean raster, so just supply the geometries here (no pairs).

out_shape : tuple or list with 2 integers
    Shape of output numpy ndarray.

This should be the shape of the numpy array to be created. If source_raster is the handle returned by rasterio.open(), then the shape will be (source_raster.height, source_raster.width) (row-major since it's a numpy array).

fill : int or float, optional
    Used as fill value for all areas not covered by input
    geometries.

Whatever background/nodata value you want. Sounds like you either want 1 or 0.

out : numpy ndarray, optional
    Array of same shape and data type as `source` in which to store
    results.

You don't need this unless you want to store the results in an existing array to save memory

transform : Affine transformation object, optional
    Transformation from pixel coordinates of `source` to the
    coordinate system of the input `shapes`. See the `transform`
    property of dataset objects.

This should be source_raster.transform

all_touched : boolean, optional
    If True, all pixels touched by geometries will be burned in.  If
    false, only pixels whose center is within the polygon or that
    are selected by Bresenham's line algorithm will be burned in.

As described

merge_alg : MergeAlg, optional
    Merge algorithm to use.  One of:
        MergeAlg.replace (default): the new value will overwrite the
            existing value.
        MergeAlg.add: the new value will be added to the existing raster.

As described, for a mask you'll want the default

default_value : int or float, optional
    Used as value for all geometries, if not provided in `shapes`.

If you have a single value to burn in, supply it here. Otherwise you'll have to have specified this within shapes. Since you want a boolean raster, use 1 or 0.

dtype : rasterio or numpy data type, optional
    Used as data type for results, if `out` is not provided.

I think this should default to something appropriate based on the burn value(s). If not, use the smallest numpy dtype appropriate for what you've supplied. gdal doesn't like bitmasks, though, so for a boolean raster use np.uint8

See the official examples here: https://rasterio.readthedocs.io/en/stable/topics/features.html

  • Thank you for the help! I have a list of polygons that I had to convert it to NDVI image. Now I am not sure what list to use for the shapes. Also I was advised to use rasterio.transform.from_bounds to get an Affine function as a parameter for transform in the rasterize one. This part is very confusing. Do you have any advice? – mandella Mar 25 at 19:51
  • 1
    I'm confused, what do you have as polygons? If you have an NDVI image and want to apply the polygons as a mask, use the transform and array shape from the NDVI image. That way your output "polygon mask" array will match the NDVI raster. The transform essentially maps the world coordinates from the geometry onto the array. You could use from_bounds as well, but if you want what I think you do then you already have the transform available when reading the NDVI raster. – mikewatt Mar 25 at 21:23
  • Sorry for being unclear. I will write what I did to get the NDVI image in my question so you can see how I did it. And in the next step my taks says: In this step you have to transform the list of polygons, which is currently in vector form, into a raster mask, which should be geographically aligned with the NDVI image. By doing so you will be able to tell which pixels from NDVI image belong to which polygon. – mandella Mar 26 at 8:47
  • And the hint for this was: For rasterization use rasterio.features.rasterize function from rasterio Python package. For function’s parameter transform use an instance of affine.Affine class which is returned by rasterio.transform.from_bounds function. – mandella Mar 26 at 8:53
  • I am confused myself because I am new in this and don't exactly understand the task. – mandella Mar 26 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.