2017-12-11 32 views
0

다각형으로 만들어진 소스 레이어를 래스터 화하려고합니다. "NoData_value"에 할당 된 값에 관계없이 배열의 결과는 항상 0입니다. python 3.4를 사용하고 있습니다. 아무도 나를 이해할 수 있도록 도와 줄 수 있습니까?Gdal_rasterize nodata_value가 작동하지 않습니다.

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() 

답변

1

당신은 다각형 외부 값이 NoData_value을 하시겠습니까?

그런 다음, gdal.RasterizeLayer를 호출하기 전에

band.Fill(NoData_value) 

를 추가합니다. gdal.RasterizeLayer은 다각형 내부의 값만 수정 (굽기)하기 때문에 필요합니다.

target_ds = gdal.GetDriverByName('MEM').Create('', int(xSize), int(ySize), 1, gdal.GDT_Float32) 

:

당신은 -9999 같은 GDT_Float32 예를 에 대한로 처리 할 수있는 형식으로 포맷 gdal.GDT_Byte을 변경해야합니다 데이터 형식이 5 인수입니다, 코드에서와 같이 4 번째가 아닙니다. 네 번째 인수는 밴드 수에 사용되어야합니다 : http://www.gdal.org/classGDALDriver.html#adb7bff9007fa5190d6cf742cf76942a8

테스트를 거쳤으며 내 크기로 작동합니다.

+0

대단히 감사합니다. 그게 다 해결 :) – Marianne