We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Patrick Gray (patrick.c.gray at duke) - https://github.com/patrickcgray
Chapter 1: Exploring rasterio
Introduction
GDAL - the Geospatial Data Abstraction Library isa software library for reading and writing raster
and vector geospatial data formats and forms the basis of most software for processing geospatial
data.
There are many formats for using GDAL ranging from graphical tools like ArcGIS or QGIS te
command line GDAL tools but here we're using the fantastic rasterio python package which
provides a pythonic wrapping around GDAL. Basically it reads and writes geospatial formats anc
provides a Python API based on numpy N-dimensional arrays and GeoJSON
f you're coming from another language and want an overview of object oriented programming in
Python, see the python like you meant it short online course
Module import in Python
Before we can get started, we need to tell Python that we will be using functions, classes, and
variables from some packages. The technical wording for this is that we need to import these
modules into our namespace (see Python's documentation on the module system here).
We will do this using some import statements
import rasterio # import the main rasterio function
import matplotlib # matplotlib is the primary python plotting and viz Library
# this bit of magic allows matplotlib to plot inline in a jupyter notebook
Xmatplotlib inline
"_version_"
# We can check which version we're running by printing the variable
print("rasterio's version is: " + rasterio.__version_)
print(rasterio)
rasterio's version is: 1.2.3
Once we import these packages Python will know where to look on our system for the code that
implements them, When we want to access classes, variables, or functions within these packages, we
will need to reference the full path (e.g, rasterio.open() }
Examples
Open an imageWhen we open an image in rasterio we create a Dataset object. As the name would suggest, we car
open an image with the “open’ function within rasterio .
We will use an example image provided in the data directory for this chapter. This image is a subset
ofa Landsat 7 image containing the 8 bands on this sensor rearranged in order of wavelength (e.g,
Landsat 7's second SWIR channel comes before thermal channel in our stack). The last band in this
image is a cloud and cloud shadow mask from Fmask
# filepath to our image
img_fp = ‘../data/LE7@220492002106EDC00_stack.gtif
# Open a geospatial dataset
dataset = rasterio.open(img_ fp)
print (dataset)