首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rasterize几何实例不起作用

Rasterize几何实例不起作用
EN

Stack Overflow用户
提问于 2018-08-09 17:13:06
回答 1查看 1.7K关注 0票数 0

我正在尝试重新创建这个rasterio例子

代码语言:javascript
复制
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像素的平方:

代码语言:javascript
复制
[[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图像)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-10 09:50:17

虽然我不能定义黑色.tif图像的原因,但我的结论是,该过程按预期工作,我们可以使用matplolib.pyplot看到可视化的结果。

必须按照以下方式修改该示例以使用pyplot

代码语言:javascript
复制
import rasterio
from matplotlib import pyplot
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+', # Open the file in read/write mode
            driver='GTiff',
            width=cols,
            height=rows,
            count=1,
            dtype=rasterio.uint8,
            nodata=0,
            transform=IDENTITY,
            crs={'init': "EPSG:4326"}) as out:
        out.write(result.astype(rasterio.uint8), indexes=1)
        # Plot the image.
        pyplot.imshow(out.read(1))
        pyplot.show()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51772616

复制
相关文章

相似问题

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