Topics

Numpy error when masking a Landsat image with a polygon [Rasterio 1.0.28]


Juliano
 

Hi folks
I'm having the following errors when I'm trying to mask a landsat image with a kml polygon.

[Dbg]>>> a,b = rasterio.mask.mask(b2_bytes.open().read(1),roi['geometry'],crop=True)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Anaconda3\lib\site-packages\rasterio\mask.py", line 174, in mask
    if dataset.nodata is not None:
AttributeError: 'numpy.ndarray' object has no attribute 'nodata'

[Dbg]>>> a,b = rasterio.mask.mask(b2_bytes.open().read(1),roi['geometry'],crop=True,nodata=0)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Anaconda3\lib\site-packages\rasterio\mask.py", line 181, in mask
    pad=pad)
  File "C:\Anaconda3\lib\site-packages\rasterio\mask.py", line 76, in raster_geometry_mask
    north_up = dataset.transform.e <= 0
AttributeError: 'numpy.ndarray' object has no attribute 'transform


The metadata for scene LC08_L1TP_221077_20190815_20190820_01_T1 is 
{'count': 1,
 'crs': CRS.from_dict(init='epsg:32622'),
 'driver': 'GTiff',
 'dtype': 'uint16',
 'height': 7801,
 'nodata': None,
 'transform': Affine(30.0, 0.0, 491985.0,
       0.0, -30.0, -2599185.0),
 'width': 7721}


My code
from google.cloud import storage
import fiona
import geopandas
import rasterio
import rasterio.mask
 
#test.kml PATH 221 ROW 77 #file is attached
#[SENSOR]/01/PATH/ROW/[scene_id]/
PATH = '221'
ROW = '077'
 
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Download Image B2 band from google cloud to memory

client = storage.Client.from_service_account_json('credential.json')
 
bucket = client.get_bucket('gcp-public-data-landsat')
l1 = list(bucket.list_blobs(prefix='LC08/01/'+PATH+'/'+ROW+'/', max_results=100000)) #,delimiter='T1'
l2 = [i.name for i in l1 if "_T1" in i.name.split('/')[4] ] #only T1 images
 
last_image = l2[len(l2)-1].split('/')[4] #scene folder


b2_binary_blob = bucket.get_blob('LC08/01/'+PATH+'/'+ROW+'/'+last_image+"/"+last_image+"_B2.TIF").download_as_string()  #LC08_L1TP_221077_20190815_20190820_01_T1
b2_bytes = rasterio.MemoryFile(b2_binary_blob)
del b2_binary_blob
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Loading  polygon and trying to mask
fiona.drvsupport.supported_drivers['kml'] = 'rw' # enable KML support which is disabled by default
fiona.drvsupport.supported_drivers['KML'] = 'rw' # enable KML support which is disabled by default
geopandas.io.file.fiona.drvsupport.supported_drivers['kml'] = 'rw' # enable KML support which is disabled by default
geopandas.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw' # enable KML support which is disabled by default
 

roi = geopandas.GeoDataFrame.from_file("../data/geo/gmaps/teste_castro.kml") #roi.crs -> {'init': 'epsg:4326'}
roi = roi.to_crs(b2_bytes.open().meta['crs'].to_dict()) #convert polygon coordinate system to the one used on the image

#Mask
a,b = rasterio.mask.mask(b2_bytes.open().read(1),roi['geometry'],crop=True) #AttributeError: 'numpy.ndarray' object has no attribute 'nodata'
a,b = rasterio.mask.mask(b2_bytes.open().read(1),roi['geometry'],crop=True,nodata=0) #AttributeError: 'numpy.ndarray' object has no attribute 'transform
 


Alan Snow
 


Alan Snow
 

After looking at the docs, I think you need to remove `.read(1)` as that returns a numpy array and `mask` expects a dataset: https://rasterio.readthedocs.io/en/stable/api/rasterio.mask.html#rasterio.mask.mask


Juliano
 

Thank you Alan!

I remove '.read(1)' and it works.