<div dir="ltr">Dear All;<div><br></div><div>I have some annual landcovder raster datasets (preciously 20) with 7 different types. <br>Since the datasets covers large area and I just want to see trends of some wetlands area changes over the year. I planned to subset the wetlands from the dataset. Then using polygonize.py to convert the raster to polygon and compute the area of each wetlands extent annually. The below is my concise code.  </div>
<div><br></div><div><br></div><div>file1 = "my_raster.tif"<br></div><div>OutFile = "wetlands.tif"<br></div><div>rastDriver = gdal.GetDriverByName('Gtiff') # reading gdal driver </div><div>rastDriver.Register() </div>
<div><br></div><div>src_ds = gdal.Open(file1)<br></div><div><br></div><div>srcband = src_ds.GetRasterBand(1)<br></div><div>cols = src_ds.RasterXSize</div><div>rows = src_ds.RasterYSize<br></div><div><br></div><div><div>Array1 = srcband.ReadAsArray(0, 0, cols, rows)</div>
<div>newArray = numpy.where(Array1 != 5, 0,Array1)</div></div><div><br></div><div><div><br></div><div>dst_ds = rastDriver.Create(OutFile, cols, rows, bands, GDT_Int16)<span class="" style="white-space:pre">                     </span></div>
<div>dst_ds.SetGeoTransform(geot)</div><div>dst_ds.SetProjection(proj)</div><div>dst_ds.GetRasterBand(1).WriteArray(newArray)<br></div></div><div>newArray = None<br></div><div><br></div><div><br></div><div>From the code above I created wetlands only raster and saved a file. Now using the line below I tried to convert the wetland raster to polygon.</div>
<div><br></div><div>PolyName = "wetland_poly"</div><div>ConvertPoly= "gdal_polygonize.py %s -f %s %s %s Value" %(OutFile, "ESRI Shapefile", PolyName + ".shp", PolyName)<br></div><div>
<div><br></div><div>os.system(ConvertPoly)</div></div><div><br></div><div><br></div><div>But it throws the error below.</div><div><br></div><div><div>dst_ds = drv.CreateDataSource( dst_filename )</div><div>AttributeError: 'NoneType' object has no attribute 'CreateDataSource'</div>
</div><div><br></div><div>When I tested qgis raster to vector conversion tool, I get the polygons as I expected from the wetlands.tif but the polygon file lost the coordinate system. if I use my_raster.tif in the code above it runs successfully but runs for more than a hour so obviously I don't want to do this.</div>
<div><br></div><div>Any elegant suggestions for alternative idea or help to fix the script would be highly appreciated.</div><div><br></div><div>Thank you very much</div><div><br></div><div>Deven</div></div>