Skip to content

Commit e9b98d0

Browse files
authored
Fix rasterio example in docs (#1881)
* Fix rasterio example in docs * Review
1 parent becd77c commit e9b98d0

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

doc/conf.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import datetime
2121
import importlib
2222

23+
allowed_failures = []
24+
2325
print("python exec:", sys.executable)
2426
print("sys.path:", sys.path)
2527
for name in ('numpy scipy pandas matplotlib dask IPython seaborn '
@@ -32,6 +34,11 @@
3234
print("%s: %s, %s" % (name, module.__version__, fname))
3335
except ImportError:
3436
print("no %s" % name)
37+
if name == 'rasterio':
38+
# not having rasterio should not break the build process
39+
allowed_failures = ['gallery/plot_rasterio_rgb.py',
40+
'gallery/plot_rasterio.py'
41+
]
3542

3643
import xarray
3744
print("xarray: %s, %s" % (xarray.__version__, xarray.__file__))
@@ -62,7 +69,8 @@
6269

6370
sphinx_gallery_conf = {'examples_dirs': 'gallery',
6471
'gallery_dirs': 'auto_gallery',
65-
'backreferences_dir': False
72+
'backreferences_dir': False,
73+
'expected_failing_examples': allowed_failures
6674
}
6775

6876
autosummary_generate = True

doc/gallery/plot_rasterio.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
These new coordinates might be handy for plotting and indexing, but it should
1414
be kept in mind that a grid which is regular in projection coordinates will
1515
likely be irregular in lon/lat. It is often recommended to work in the data's
16-
original map projection.
16+
original map projection (see :ref:`recipes.rasterio_rgb`).
1717
"""
1818

1919
import os
@@ -44,10 +44,13 @@
4444
da.coords['lon'] = (('y', 'x'), lon)
4545
da.coords['lat'] = (('y', 'x'), lat)
4646

47+
# Compute a greyscale out of the rgb image
48+
greyscale = da.mean(dim='band')
49+
4750
# Plot on a map
4851
ax = plt.subplot(projection=ccrs.PlateCarree())
49-
da.plot.imshow(ax=ax, x='lon', y='lat', rgb='band',
50-
transform=ccrs.PlateCarree())
52+
greyscale.plot(ax=ax, x='lon', y='lat', transform=ccrs.PlateCarree(),
53+
cmap='Greys_r', add_colorbar=False)
5154
ax.coastlines('10m', color='r')
5255
plt.show()
5356

doc/gallery/plot_rasterio_rgb.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
.. _recipes.rasterio_rgb:
4+
5+
============================
6+
imshow() and map projections
7+
============================
8+
9+
Using rasterio's projection information for more accurate plots.
10+
11+
This example extends :ref:`recipes.rasterio` and plots the image in the
12+
original map projection instead of relying on pcolormesh and a map
13+
transformation.
14+
"""
15+
16+
import os
17+
import urllib.request
18+
import xarray as xr
19+
import cartopy.crs as ccrs
20+
import matplotlib.pyplot as plt
21+
22+
# Download the file from rasterio's repository
23+
url = 'https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif'
24+
urllib.request.urlretrieve(url, 'RGB.byte.tif')
25+
26+
# Read the data
27+
da = xr.open_rasterio('RGB.byte.tif')
28+
29+
# Normalize the image
30+
da = da / 255
31+
32+
# The data is in UTM projection. We have to set it manually until
33+
# https://github.com/SciTools/cartopy/issues/813 is implemented
34+
crs = ccrs.UTM('18N')
35+
36+
# Plot on a map
37+
ax = plt.subplot(projection=crs)
38+
da.plot.imshow(ax=ax, rgb='band', transform=crs)
39+
ax.coastlines('10m', color='r')
40+
plt.show()
41+
42+
# Delete the file
43+
os.remove('RGB.byte.tif')

0 commit comments

Comments
 (0)