6

I am trying to learn sentinelsat by following some tutorials. Part of the codes goes like this.

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)

The line out_image, out_transform = rio.mask.mask(src, nReserve_proj.geometry,crop=True) is giving me error. The error goes like this --

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'

But rasterio's documentation it shows rasterio.mask.mask() exists. from doc --

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

What went wrong here? I am new so I am not understanding what to check.

  • 1
    This could be caused by having a file called `rasterio.py` in the current working directory. Because of the order Python searches for things to import, the code in this file would be imported instead of the installed module. If you have this situation occurring, the solution is to rename your file to something that wouldn't block the correct import of any module. Possibly just: `my_rasterio.py`. – mechanical_meat Aug 19 '21 at 15:38
  • 2
    Thank you , it read. Probably what you mentioned was creating the problem. Instead of ```import rasterio as rio``` I used ```from rasterio import mask as msk``` and then writing the line like this ```out_image, out_transform = msk.mask(src, nReserve_proj.geometry,crop=True)``` Now it is not giving any error. – mir abir hossain Aug 19 '21 at 16:57

1 Answers1

4

You need to import mask from rasterio.mask. You will also need to change the line where you call the function so it says mask instead of 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)
Trex
  • 529
  • 3
  • 11