data = gdal.Open(templateTifFileName, gdalconst.GA_ReadOnly)
geo_transform = data.GetGeoTransform()
x_min = geo_transform[0]
y_min = geo_transform[3]
x_res = data.RasterXSize
y_res = data.RasterYSize
mb_v = ogr.Open(shpFileName)
mb_l = mb_v.GetLayer()
pixel_width = geo_transform[1]
target_ds = gdal.GetDriverByName('GTiff').Create(outputFileName, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_width, 0, y_min, 0, -1 * pixel_width))
band = target_ds.GetRasterBand(1)
NoData_value = -999
band.SetNoDataValue(NoData_value)
band.FlushCache()
gdal.RasterizeLayer(target_ds, [1], mb_l, options=["ATTRIBUTE=" + field])
target_ds = None
注意:geo_transform设置不对,能出来图片,但是效果不对,field是以字段值作为栅格的值
以上就是python gdal 矢量转栅格的全部内容。