I'm trying to polygonize the following binary raster:
The output is a polygon layer (ESRI shapefile) that looks good, however, doesn't have FIDs for the features and thus I am not able to iterate through them in the later part of my script.
Is there any way to add feature IDs to the shapefile's attribute table while polygonizing the raster or afterwards?
I'm using the following code:
from osgeo import gdal, ogr, osr
raster = gdal.Open(path_raster)
band = raster.GetRasterBand(1)
# determine EPSG
sp_ref = osr.SpatialReference()
sp_ref.SetFromUserInput('EPSG:3857')
drv = ogr.GetDriverByName('ESRI Shapefile') # file type: shapefile
outfile = drv.CreateDataSource(path_vec_results) #file path
layer = outfile.CreateLayer(vector_file, geom_type = ogr.wkbMultiPolygon, srs = sp_ref)
layer.CreateField(ogr.FieldDefn('DN', ogr.OFTInteger)) #add DN column for raster valules
layer.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger)) #ID column to fill later?
# polygonize
vector_result = gdal.Polygonize(band, band, layer, 0, [])
outfile = None