In [1]:
import numpy as np
import matplotlib.pyplot as plt
from osgeo import gdal
from osgeo import gdal_array
import rasterio
import cartopy.crs as ccrs
## DEM plot via rasterio, pyplot imshow
## Live 8.5 * darkblue-b
##
==
In [2]:
## Rasterio
## Clean and fast and geospatial raster I/O for Python programmers who use Numpy
with rasterio.open('SanMateo_CA.tif') as src:
data = src.read()
data = np.transpose( data, [1,2,0])
## show selected data attributes
## -------------------------------------
# type(data) numpy.ma.core.MaskedArray
# data.ndim 3
# data.shape (1080, 864, 1)
# data.dtype dtype('float32')
## NOTE: when using rasterio and matplotlib only,
## the column order for the trivial case of lat/lon/measure
## is NOT handled automatically..
## column reordering is REQUIRED np.transpose()
In [3]:
## Rasterio supplies a simple dictionary of important GeoTIFF metadata
src.meta
Out[3]:
In [4]:
## numpy ndarray indexing example
## show the measure values for a 2x2 area
## no-data options are available using a masked array
## http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html#what-is-a-masked-array
data[0:2,0:2,0]
Out[4]:
In [6]:
## pyplot Image-Show
## http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow
##
## Example of using matplotlib to directly display a GeoTIFF
##
## Cartopy supplies a library of mapping transformations
## use an idiom to calculate the correct display bounds
##
## data[:,:,0] refers to a numpy ndarray: all-x, all-y, 0th measure
##
xmin = src.transform[0]
xmax = src.transform[0] + src.transform[1]*src.width
ymin = src.transform[3] + src.transform[5]*src.height
ymax = src.transform[3]
## Mercator etc..
crs = ccrs.PlateCarree()
ax = plt.axes(projection=crs)
plt.imshow( data[:,:,0], origin='upper',
extent=[xmin, xmax, ymin, ymax],
cmap=plt.get_cmap('gist_earth'),
transform=crs, interpolation='nearest')
Out[6]:
==
In [7]:
## Show the same content, but with a colorbar legend for the data
##
plt.figure(figsize=(15, 10))
ax = plt.subplot(111, projection=ccrs.PlateCarree())
#elev, crs, extent = srtm_composite(12, 47, 1, 1)
plt.imshow( data[:,:,0], transform=ccrs.PlateCarree(),
cmap='gist_earth')
cb = plt.colorbar(orientation='vertical')
cb.set_label('Altitude')
plt.title("DEM")
Out[7]:
In [8]:
%whos
In [ ]: