我正在尝试重新创建这个光栅示例:
import numpy as np
import rasterio
from rasterio.features import rasterize
from rasterio.transform import IDENTITY
rows = cols = 10
geometry = {
'type': 'Polygon',
'coordinates': [[(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]]
}
with rasterio.Env():
result = rasterize([geometry], out_shape=(rows, cols))
with rasterio.open(
"test.tif",
'w',
driver='GTiff',
width=cols,
height=rows,
count=1,
dtype=np.uint8,
nodata=0,
transform=IDENTITY,
crs={'init': "EPSG:4326"}) as out:
out.write(result.astype(np.uint8), indexes=1)
我检查了rasterize
( print(result)
) 的结果,这似乎是正常的,并且结果是 2x2 像素正方形:
[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 1 1 0 0 0 0 0 0]
[0 0 1 1 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
但我得到一个完全黑色的图像(nodata
图像)。