2

I have a .shp file and I would like to convert it into a GEOTIFF. My shape file consists of a large polygon with many polygons inside. I am using the following code, but the output TIF consists of only the large polygon.

from osgeo import ogr, gdal
import subprocess

InputVector = Shapefile
OutputImage = OutputfileName

gdalformat = 'GTiff'
datatype = gdal.GDT_Byte
burnVal = 1 
Shapefile = ogr.Open(InputVector)
Shapefile_layer = Shapefile.GetLayer()


Output = gdal.GetDriverByName(gdalformat).Create(OutputImage, RasterXSize, RasterYSize, 1, datatype, options=['COMPRESS=DEFLATE'])
Output.SetProjection(Projection)
Output.SetGeoTransform(GeoTransform) 

Band = Output.GetRasterBand(1)
Band.SetNoDataValue(0)
gdal.RasterizeLayer(Output, [1], Shapefile_layer, burn_values=[burnVal])

subprocess.call("gdaladdo --config COMPRESS_OVERVIEW DEFLATE "+OutputImage+" 2 4 8 16 32 64", shell=True)

I am unsure what I am doing wrong here.

Thanks

4

1 回答 1

1

我的第一个猜测是确保较大的多边形在较小的多边形出现的地方有“洞”/ NoData。我会尝试通过 ArcMap 的擦除工具中的示例来更清楚地说明。你希望你的大多边形看起来像蓝色勾号(擦除功能明显重叠),我认为你的大多边形目前看起来是一个带有红色勾号的,这将导致你的栅格只包含大多边形。如果我完全错了,我很抱歉,然后我们会找到另一个解决方案。

在此处输入图像描述

如果是这种情况(您的大多边形类似于蓝色勾号),将所有多边形烧成 1 也可能会造成混乱。您可以在 shapefile 中创建一个新字段(例如 UID)并为其指定一个唯一的数字 ID(它必须是数字)。您可以根据新字段进行栅格化,例如:

gdal.RasterizeLayer(Output, [1], Shapefile_layer, options = ['ATTRIBUTE=UID'])
于 2019-03-12T14:25:20.887 回答