1

According to Rasterizing shapefiles with GDAL and Python? it seems that gdal allows rasterization of a layer according to a certain attribute

ds = gdal.RasterizeLayer(target_ds, [1], source_layer, options = ["ATTRIBUTE=ID"])

I would like to rasterize a layer with small square polygon geometries using rasterio

enter image description here

rasterio has a features.rasterize method (see docs), but it seems to only be able to output numpy arrays with 1's where the polygons occur. Since rasterio is a higher level library than gdal I am a bit surprised that selecting an attribute to burn the raster with is not supported functionality.

Is there any built-in method in rasterio to burn a raster by an attribute from the original vector layer?

6
  • Can you not pre-filter the "shapes" you pass to features.rasterize to include only the shapes with the desired attributes? As for the "1's where the polygons occur," isn't that what you want? I.e. a binary raster containing only pixels where your feature is present.
    – Jon
    Commented Oct 4, 2018 at 14:49
  • No, my polygons have an attribute which is float type and varies for each polygon. I am not looking for a binary raster but rather a raster with values from my polygon attribute
    – user32882
    Commented Oct 4, 2018 at 15:26
  • It looks like you can specify the value in the shapes object you pass: "default_value (int or float, optional) – Used as value for all geometries, if not provided in shapes." The shapes input says "shapes (iterable of (geometry, value) pairs or iterable over)..." so you just need to pass the float value along with each geometry as a tuple.
    – Jon
    Commented Oct 4, 2018 at 15:37
  • Indeed. I only realized this after writing the question... If you like you can provide that as your answer.
    – user32882
    Commented Oct 4, 2018 at 15:44
  • However I see that there is also no place to specify the nodata value for this newly created raster dataset
    – user32882
    Commented Oct 4, 2018 at 15:45

2 Answers 2

4

The inputs to features.rasterize include

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.

So you can just pass tuples of (geom, value), where value is the attribute value you wish to burn into the raster.

1

To give an example if you have col set to the column you want to rasterize

if col:
    shapes = ((geom,value) for geom, value in zip(dataframe.geometry, dataframe[col]))
else: 
    shapes = dataframe.geometry.values
    
varray = rasterize(shapes,
                   out_shape=(dst_height, dst_width),
                   transform=dst_transform,
                   fill=fill,
                   default_value=default_value,
                   all_touched=all_touched,
                   dtype=dtype)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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