gpt4 book ai didi

python - 属性错误 : module 'rasterio' has no attribute 'mask'

转载 作者:行者123 更新时间:2023-12-05 04:46:21 24 4
gpt4 key购买 nike

我正在尝试按照一些教程学习哨兵卫星。部分代码是这样的。

import rasterio as rio
import geopandas as gpd

nReserve = gpd.read_file('NReserve/NaturalReserve_Polygon.shp')

nReserve_proj = nReserve.to_crs({'init': 'epsg:32633'})

with rio.open("RGB.tiff") as src:
out_image, out_transform = rio.mask.mask(src, nReserve_proj.geometry,crop=True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})

with rio.open("RGB_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)

out_image, out_transform = rio.mask.mask(src, nReserve_proj.geometry,crop=True) 给我错误。错误是这样的——

AttributeError                            Traceback (most recent call last)
<ipython-input-45-c1fc22fa2c5d> in <module>()
2
3 with rio.open("RGB.tiff") as src:
----> 4 out_image, out_transform = rio.mask.mask(src, nReserve_proj.geometry,crop=True)
5 out_meta = src.meta.copy()
6 out_meta.update({"driver": "GTiff",

AttributeError: module 'rasterio' has no attribute 'mask'

但是 rasterio 的文档显示 rasterio.mask.mask() 存在。来自文档——

rasterio.mask.mask(dataset, shapes, all_touched=False, invert=False, nodata=None, filled=True, crop=False, pad=False, pad_width=0.5, indexes=None)

这里出了什么问题?我是新手,所以我不知道要检查什么。

最佳答案

您需要从rasterio.mask 导入mask。您还需要更改调用该函数的行,使其显示 mask 而不是 rio.mask.mask

import rasterio as rio
import geopandas as gpd
from rasterio.mask import mask

nReserve = gpd.read_file('NReserve/NaturalReserve_Polygon.shp')

nReserve_proj = nReserve.to_crs({'init': 'epsg:32633'})

with rio.open("RGB.tiff") as src:
out_image, out_transform = mask(src, nReserve_proj.geometry,crop=True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})

with rio.open("RGB_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)

关于python - 属性错误 : module 'rasterio' has no attribute 'mask' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68850555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com