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.
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()