Reading and writing raster data with Rasterio

After covering how to read and write various vector data formats in Python, we'll now do the same for raster data. We'll start with the Rasterio library and have a look at how we can read and write raster data. Open up a new Jupyter Notebook where you have access to the Rasterio library and type the following code:

In: import rasterio    
dataset = rasterio.open(r"C:datagdalNE50m_rasterNE1_50M_SR_W
NE1_50M_SR_W.tif")

This imports the rasterio library and opens a GeoTIFF file. We can now perform some simple data description commands, such as printing the number of image bands.

Raster images contain either a single or multiple bands. All bands are contained in a single file, with each band covering the same area. When the image is read by your computer, these bands are overlayed on top of each other so that you'll see one single image. Each band contains a 2D array with rows and columns of data. Each data cell of each array contains a numeric value that corresponds to a color value (or elevation value, which is also possible). If a raster image has multiple bands, each band corresponds to a segment of the electromagnetic spectrum that was collected by a sensor. Users can display one or multiple bands, combining different bands together to make their own color composites. Chapter 9ArcGIS API for Python and ArcGIS Online features some examples of these color composites when discussing displaying raster data using the ArcGIS API for Python.

In this case, there are three different bands:

In: dataset.count
Out: 3

A dataset band is an array of values representing the partial distribution of a single variable in a 2D space. The number of columns is returned by the width attribute:

In: dataset.width
Out: 10800

The number of rows is returned by the height attribute:

In:  dataset.height
Out: 5400

The following code returns the spatial bounding box in meters, so you can calculate the area it covers:

In:  dataset.bounds
Out: BoundingBox(left=-179.99999999999997, bottom=-89.99999999998201,
right=179.99999999996405, top=90.0)

The CRS of the dataset can be printed as follows:

In:  dataset.crs
Out: CRS({'init': 'epsg:4326'})

You can access and return a NumPy ndarray with the raster array for a raster band as follows:

In:   band1 = dataset.read(1)
band1
Out: array([[124, 124, 124, ..., 124, 124, 124], ...

If you want to visualize the image, use the following code:

In: %matplotlib inline
from matplotlib import pyplot
pyplot.imshow(dataset.read(1))
pyplot.show()
The output map will look like this:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset