python栅格遥感影像转矢量

栅格影像转矢量

其中,读取栅格影像,见另一篇文章:python利用gdal读遥感卫星影像

from osgeo import gdal,ogr 
def createShpfile_from_tiffile(shpfile,tiffile):
    fieldName="MASK"
    geotiff = tifread(tifpath) ##读取栅格影像,具体代码见另一篇文章
    data = geotiff.dataarray
    data_dim = len(data.shape)
    if data_dim == 3:
        data = data[:,:,0]    
        
    driver = gdal.GetDriverByName('MEM')
    raster = driver.Create('',geotiff.cols, geotiff.rows, 1, gdal.GDT_Byte)
    raster.SetGeoTransform(geotiff.geo_transform)
    raster.SetProjection(geotiff.projection)
    raster.GetRasterBand(1).WriteArray(data)
    band = raster.GetRasterBand(1)
    
    driver = ogr.GetDriverByName("ESRI Shapefile")
    data_source = driver.CreateDataSource(shpfile)
    srs = osr.SpatialReference()
    srs.ImportFromWkt(geotiff.projection)
    layer = data_source.CreateLayer(shpfile,srs)
    # 添加属性列
    newField = ogr.FieldDefn(fieldName, ogr.OFTInteger)
    layer.CreateField(newField)   
    gdal.Polygonize(band, band, layer,0, [], callback=None )
    data_source.Destroy()
    geotiff= None
    band=None
if __name__ == "__main__":
	shpfile=r"D:/new.shp"
	tiffile=r"D:/data.tif"
	createShpfile_from_tiffile(shpfile,tiffile)

你可能感兴趣的:(python,python,数据库,矩阵,图像处理,数据库开发,json)