Created
April 7, 2021 21:04
-
-
Save epifanio/00d7467ac01511b63377e7a9e71b49c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from osgeo import gdal, ogr, osr | |
raster_path = "test.tif" | |
shapefile_path = "test.shp" | |
source_raster = gdal.Open(raster_path) | |
band = gdal.Open(raster_path).GetRasterBand(1) | |
driver = ogr.GetDriverByName("ESRI Shapefile") | |
out_data = driver.CreateDataSource(shapefile_path) | |
srs = osr.SpatialReference() | |
srs.ImportFromWkt(source_raster.GetProjectionRef()) | |
out_layer = out_data.CreateLayer(raster_path.split(".")[0], srs) | |
new_field = ogr.FieldDefn("field_name", ogr.OFTReal) | |
out_layer.CreateField(new_field) | |
gdal.FPolygonize(band, band, out_layer, 0, [], callback=None) | |
out_data.Destroy() | |
source_raster = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment