Reading raster files with Rasterio¶
Rasterio is a highly useful module for raster processing which you can use for reading and writing several different raster formats in Python. Rasterio is based on GDAL and Python automatically registers all known GDAL drivers for reading supported formats when importing the module. Most common file formats include for example TIFF and GeoTIFF, ASCII Grid and Erdas Imagine .img -files.
Landsat 8 bands are stored as separate GeoTIFF -files in the original package. Each band contains information of surface reflectance from different ranges of the electromagnetic spectrum.
Let’s start with inspecting one of the files we downloaded:
In [1]: import rasterio
In [2]: fp = r"C:\HY-DATA\HENTENKA\KOODIT\Opetus\Automating-GIS-processes\Data\Landsat\Helsinki_masked_p188r018_7t20020529_z34__LV-FIN.tif"
# Open the file:
In [3]: raster = rasterio.open(fp)
# Check type of the variable 'raster'
In [4]: type(raster)
Out[4]: rasterio._io.RasterReader
Okey so from here we can see that our raster
variable is a rasterio._io.RasterReader
type which means that we have opened the file for reading.
Read raster file properties¶
Let’s have a closer look at the properties of the file:
# Projection
In [5]: raster.crs
Out[5]: CRS({'proj': 'tmerc', 'lat_0': 0, 'lon_0': -183, 'k': 0.9996, 'x_0': 500000, 'y_0': 0, 'datum': 'WGS84', 'units': 'm', 'no_defs': True})
# Affine transform (how raster is scaled, rotated, skewed, and/or translated)
In [6]: raster.transform