This post is a quick example on how to read geotiff images with GDAL and plot them with Cartopy.
The image, and most of the code, is from Kelsey Jordahl original notebook. The main difference is that I compressed the geotiff with:
gdal_translate -co "TILED=YES" -co "COMPRESS=JPEG" manhattan.tif manhattan2.tif
First: read the geotiff image with GDAL.
In [2]:
from osgeo import gdal, osr
gdal.UseExceptions()
fname = './data/manhattan2.tif'
ds = gdal.Open(fname)
data = ds.ReadAsArray()
gt = ds.GetGeoTransform()
proj = ds.GetProjection()
inproj = osr.SpatialReference()
inproj.ImportFromWkt(proj)
print(inproj)
Second: convert the WKT projection information to a cartopy projection.
In [3]:
import cartopy.crs as ccrs
projcs = inproj.GetAuthorityCode('PROJCS')
projection = ccrs.epsg(projcs)
print(projection)
Third (and last): the figure.
In [4]:
import matplotlib.pyplot as plt
subplot_kw = dict(projection=projection)
fig, ax = plt.subplots(figsize=(9, 9), subplot_kw=subplot_kw)
extent = (gt[0], gt[0] + ds.RasterXSize * gt[1],
gt[3] + ds.RasterYSize * gt[5], gt[3])
img = ax.imshow(data[:3, :, :].transpose((1, 2, 0)), extent=extent,
origin='upper')
In [5]:
HTML(html)
Out[5]: