I have a big (2GB) landcover shapefile I want to convert to a GeoTIFF file. For this purpose, I am using the gdal.Rasterize()
function with the following options:
rasterize_options = gdal.RasterizeOptions(
outputType=gdal.GDT_Byte,
xRes=1000, # 1km
yRes=1000, # 1km
noData=255,
allTouched=True,
attribute='CODE' # Shapefile field with each landcover class
)
gdal.Rasterize(dst, src, options=rasterize_options)
Although this works, it is not clear what cell assignment method the function is using to assign the output value to each pixel. Here is an example of the input shapefile and the output raster (with each color representing a different class):
I want to specify a cell assignment method where the output value in a given pixel corresponds to the class of the polygons that occupy most of the area in the pixel. Thus, the output raster shown above would end up being mostly orange and probably just a few pixels, if any, would be green.
This seems to be possible in other tools such as ArcGIS's Polygon to Raster, where one can specify the cell_assignment
parameter:
The method to determine how the cell will be assigned a value when more than one feature falls within a cell.
- CELL_CENTER —The polygon that overlaps the center of the cell yields the attribute to assign to the cell.
- MAXIMUM_AREA —The single feature with the largest area within the cell yields the attribute to assign to the cell.
- MAXIMUM_COMBINED_AREA —If there is more than one feature in a cell with the same value, the areas of these features will be combined. The combined feature with the largest area within the cell will determine the value to assign to the cell.
However, after reading the documentation, GDAL does not seem to have the option to specify the cell assignment method. I also took a look at the rasterio.features.rasterize()
function but my best guess is that this function is just a wrapper of GDAL's rasterize function and does not offer the option to specify the method either.
As I don't have access to ArcGIS and would rather use open source tools for this, the only workaround I have thought of is rasterizing the shapefile to a smaller cell size (30m maybe as the shapefile was produced using Landsat imagery) and then resampling the output using a mode algorithm. However, this does not seem like a good option given the extent of the original shapefile. What are my options here?