I want to polygonize a raster file. The raster file looks like this.
Here is the raster file
Here is my codes in Python to polygonize the raster file. The codes source from Polygonize a Raster Band
#raster to polygons from osgeo import gdal, ogr # get raster datasource src_ds = gdal.Open( "C:\\Users\\select3.tif" ) srcband = src_ds.GetRasterBand(1) # create output datasource dst_layername = "output_select3" drv = ogr.GetDriverByName("ESRI Shapefile") dst_ds = drv.CreateDataSource( dst_layername + ".shp" ) dst_layer = dst_ds.CreateLayer(dst_layername, srs = None ) gdal.Polygonize( srcband, None, dst_layer, -1, , callback=None ) #The following two steps are very important, otherwise will generate error like (Coordinate not defined) dst_ds.Destroy() src_ds=None
I got the result which looks like this.
And here is the output shapefile
I do not satisfy with this, because I only want to have the polygon areas (Check mark) and exclude the extent (Cross mark) of the polygons. Could anybody tell me how to fix this in python?