"""
++++++++++++++++++++++++++++++++++++++++++++++
基于Python的GDAL库将GeoTIFF文件转化为ESRI Polygon shape文件
# 注:脚本当前仅能转化单个波段的GeoTIFF file
author: ding xiaosong time:2021-04-22
+++++++++++++++++++++++++++++++++++++++++++++++\n
"""
from osgeo import gdal, osr, ogr
import os
def polygon_response(raster, poligonized_shp):
"""
将GeoTIFF转化为polygon shape file
"""
src_ds = gdal.Open(raster)
srcband = src_ds.GetRasterBand(1)
dst_layername = poligonized_shp
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)
dst_ds.Destroy()
if __name__ == '__main__':
print(__doc__)
raster = './Raster.tif'
poligonized_shp = './Polygon_result'
polygon_response(raster, poligonized_shp)
|