AttributeError: модуль rasterio не имеет атрибута mask

Я пытаюсь изучить sentinelsat, следуя некоторым руководствам. Часть кодов выглядит так.

      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)

Что здесь пошло не так? Я новичок, поэтому не понимаю, что проверять.

1 ответ

Вам нужно импортировать из rasterio.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)
Другие вопросы по тегам