如何使用GDAL光栅化(python API)按属性指定burn值?

2024-06-09 00:53:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用gdal.RasterizeLayer()使用GeoTiff模板将shapefile转换为GeoTiff,同时通过ATTRIBUTE烧录输出值。我想要输出的是一个.tif,其中burn值对应于给定属性的值。我发现gdal.RasterizeLayer()正在燃烧为与属性字段中的值不对应的奇怪值。以下是我目前拥有的:

    gdalformat = 'GTiff'
    datatype = gdal.GDT_Byte

    # Open Shapefile
    shapefile = ogr.Open(self.filename)
    shapefile_layer = shapefile.GetLayer()
    # Get projection info from reference image
    image = gdal.Open(ref_image, gdal.GA_ReadOnly)
    output = gdal.GetDriverByName(gdalformat).Create(output_tif, image.RasterXSize, image.RasterYSize, 1, datatype,
                                                     options=['COMPRESS=DEFLATE'])
    output.SetProjection(image.GetProjectionRef())
    output.SetGeoTransform(image.GetGeoTransform())

    # Write data to band 1
    band = output.GetRasterBand(1)
    band.SetNoDataValue(0)
    gdal.RasterizeLayer(output, [1], shapefile_layer, options=['ATTRIBUTE=FCode'])

    # Close datasets
    band = None
    output = None
    image = None
    shapefile = None

    # Build image overviews
    subprocess.call("gdaladdo --config COMPRESS_OVERVIEW DEFLATE " + output_tif + " 2 4 8 16 32 64", shell=True)

出现的情况是,output.tif为每个属性正确分配了不同的burn值,但该值与属性值不对应。例如,输入属性值FCode=46006变为燃烧值182(不清楚原因!)。我尝试添加和删除'COMPRESS=DEFLATE'选项,以及添加和删除gdal.RasterizeLayer()的“3D”选项。不影响输出燃烧值

您可以在此处看到输入shapefile和属性值:input .shp

输出值不正确,在这里:output raster


Tags: imagenoneoutputband属性attributeopencompress