import rasterio # import the main rasterio function
import matplotlib # matplotlib is the primary python plotting and viz library
import matplotlib.pyplot as plt
# this bit of magic allows matplotlib to plot inline in a jupyter notebook
%matplotlib inline
import numpy as np
from rasterio.plot import show
img='/work/LE70220491999322EDC01_stack.gtif'
data=rasterio.open(img)
print(data)
<open DatasetReader name='/work/LE70220491999322EDC01_stack.gtif' mode='r'>
bands=data.count
print(bands)
8
rows, col=data.shape
print(rows, col)
250 250
desc=data.descriptions
meta=data.meta
print(desc)
print(meta)
('band 1 reflectance', 'band 2 reflectance', 'band 3 reflectance', 'band 4 reflectance', 'band 5 reflectance', 'band 7 reflectance', 'band 6 temperature', None)
{'driver': 'GTiff', 'dtype': 'int16', 'nodata': -9999.0, 'width': 250, 'height': 250, 'count': 8, 'crs': CRS.from_epsg(32615), 'transform': Affine(30.0, 0.0, 462405.0,
0.0, -30.0, 1741815.0)}
driver=data.driver
print(driver)
GTiff
prj=data.crs
print(prj)
EPSG:32615
gt=data.transform
print(gt)
| 30.00, 0.00, 462405.00|
| 0.00,-30.00, 1741815.00|
| 0.00, 0.00, 1.00|
nir=data.read(4) #open the 4th band of image which is near infrared
nir.shape # check dimensions
datatype=data.dtypes # check bands data type
print(datatype)
('int16', 'int16', 'int16', 'int16', 'int16', 'int16', 'int16', 'int16')
# display entire image
full_img=data.read()
full_img.shape # bands, rows, col
# plot image nir band
show(nir, transform=data.transform, cmap='gray')
# we can also use matplotlib to show nir band
plt.imshow(nir)
plt.colorbar()
plt.title('Overview - Band 4 {}'.format(nir.shape))
plt.xlabel('Column #')
plt.ylabel('Row #')