Rasterio - hyperspectral, SAM

Analyzing hyperspectral images using open source libraries

My objective is to apply spectral angle mapper classifier

In [1]:
import rasterio
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Read the hyperspectral image

In [2]:
hy_raster = rasterio.open('/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img')
hy_raster
Out[2]:
<open RasterReader name='/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img' mode='r'>
In [3]:
# number of bands
hy_raster.count
Out[3]:
224

Get cell values

In [4]:
all_bands = hy_raster.read()
all_bands.shape
Out[4]:
(224, 737, 718)
In [8]:
def get_cell_value(raster, all_bands, x,y):
    array_coords = raster.index(x,y)
    n_bands = raster.count
    
    # get pixel values
    values= [all_bands[i, array_coords[0], array_coords[1]] 
            for i in range(raster.count)]
    
    # plot values
    plt.plot(values)
    return values
In [10]:
cell_values1 = get_cell_value(hy_raster, all_bands, 630297,3896378)

Read image using spectral python library

In [7]:
import spectral
from spectral.io import aviris
In [8]:
aviris.open('/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img')

# hy_raster2 = spectral.open_image('/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img')
# hy_raster2
---------------------------------------------------------------------------
InvalidFileError                          Traceback (most recent call last)
<ipython-input-8-3fb4ea539783> in <module>()
----> 1 aviris.open('/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img')
      2 
      3 # hy_raster2 = spectral.open_image('/Users/atma6951/Documents/GIS_data/Imagery/AVIRIS-mohave-clipped/mohave_clipped.img')
      4 # hy_raster2

~/anaconda3/envs/geopandasenv/lib/python3.6/site-packages/spectral/io/aviris.py in open(file, band_file)
     76     fileSize = os.stat(p.filename)[6]
     77     if fileSize % 275072 != 0:
---> 78         raise InvalidFileError('File size not consistent with AVIRIS format.')
     79     p.nrows = int(fileSize / 275072)
     80     p.byte_order = 1

InvalidFileError: File size not consistent with AVIRIS format.
In [ ]: