Guest User

Untitled

a guest
Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import gdal,ogr, osr
  2.  
  3. in_path = 'C:\Test\Multiband Image.tif'
  4. out_path = 'C:\Test\'
  5.  
  6. ds = gdal.Open(in_path)
  7. ds_proj=ds.GetProjection()
  8.  
  9. raster_proj = osr.SpatialReference()
  10. raster_proj.ImportFromWkt(ds_proj)
  11.  
  12. for i in range (1,ds.RasterCount+1): ##Begin at band 1 through to to the last one
  13. #Save each band individually
  14. srcband = ds.GetRasterBand(i)
  15. out_ds = gdal.Translate(out_path + 'band' + str(i) + '.tiff', ds, format='GTiff', bandList=[i])
  16. out_ds=None
  17.  
  18. #Prepare shapefile
  19. outShapefile = "C:\Test\polygonized"
  20. driver = ogr.GetDriverByName("ESRI Shapefile")
  21. outDatasource = driver.CreateDataSource(outShapefile+ str(i) + ".shp")
  22. outLayer = outDatasource.CreateLayer("polygonized", srs=raster_proj)
  23. #Add the DN field
  24. newField = ogr.FieldDefn('DN', ogr.OFTInteger)
  25. outLayer.CreateField(newField)
  26. gdal.Polygonize(srcband, None, outLayer, 0, [], callback=None )
  27. outDatasource.Destroy()
  28. sourceRaster = None
  29.  
  30. print "Done"
Add Comment
Please, Sign In to add comment