I would like to polygonize every single cell of a raster dataset using rasterio
. I have been experimenting with the following function based on this question:
def polygonize_raster(dataset):
# Read the dataset's valid data mask as a ndarray. Dataset is a rasterio read object open for reading
mask = dataset.dataset_mask()
array = dataset.read(1)
generator = rasterio.features.shapes(source=array, mask=mask, transform=dataset.transform)
# Extract feature shapes and values from the array
geom_list = []
for geom, value in generator:
# Print GeoJSON shapes to stdout
geom = shapely.geometry.shape(geom)
geom_list.append(geom)
return geom_list
The part which vectorizes everything is:
generator = rasterio.features.shapes(source=array, mask=mask, transform=dataset.transform)
It seems from the docs that rasterio.features.shapes
has built-into it the fact that regions with identical values get their own polygon. I don't want this. I want every single cell to get its own polygon even if an adjacent cell has the same value.
One solution I thought of is to give each cell a unique value, but this would require writing another function which could be computationally expensive. I would much rather if a more built-in method within rasterio
or shapely
could do this?