首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gdal_rasterize nodata_value不工作

Gdal_rasterize nodata_value不工作
EN

Stack Overflow用户
提问于 2017-12-11 10:51:33
回答 1查看 956关注 0票数 3

我想把一个由多边形组成的源层进行栅格化。无论我为"NoData_value“分配了什么值,数组中的结果总是= 0。我正在使用python3.4。有人能帮我理解一下吗?

代码语言:javascript
复制
source_srs = source_layer.GetSpatialRef()
x_min, x_max, y_min, y_max = source_layer.GetExtent()

# parameters of output file
xSize = math.ceil((x_max - x_min) / pixel_size)  # number of pixels in the x direction given pixel_size, rounded up to the nearest pixel
ySize = math.ceil((y_max - y_min) / pixel_size)  # number of pixels in the y direction given pixel_size, rounded up to the nearest pixel
x_res = int((x_max - x_min) / xSize)  # size of pixel in meters rounded to fit bbox
y_res = int((y_max - y_min) / ySize)  # size of pixel in meters rounded to fit bbox

NoData_value = -9999

# Create output dataset as memory
target_ds = gdal.GetDriverByName('MEM').Create('', xSize, ySize, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, x_res, 0, y_max, 0, -y_res))
wkt_projection = source_srs.ExportToWkt()
target_ds.SetProjection(wkt_projection)

band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)

# rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, options=["ATTRIBUTE=expo" ])

# Read as numpy array
array = band.ReadAsArray()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-11 14:07:12

你想让多边形外的值有NoData_value吗?

然后,添加

代码语言:javascript
复制
band.Fill(NoData_value)

在打电话给gdal.RasterizeLayer之前。您需要这样做,因为gdal.RasterizeLayer只修改(刻录)多边形内的值。

您需要将gdal.GDT_Byte格式更改为可以处理-9999的格式,例如,GDT_Float32。

代码语言:javascript
复制
target_ds = gdal.GetDriverByName('MEM').Create('', int(xSize), int(ySize), 1, gdal.GDT_Float32)

注意事项:数据类型是第5个参数,而不是代码中的第4个参数。第四个参数应该用于频带数:链接

我测试过了,它适合我的尺寸。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47751296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档