Skip to content

Commit 61f97b5

Browse files
committed
Homogenize plotting in example gallery and streamline dem subtraction example
1 parent 1ad4ad1 commit 61f97b5

File tree

3 files changed

+33
-62
lines changed

3 files changed

+33
-62
lines changed

examples/advanced/plot_slope_methods.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ def plot_attribute(attribute, cmap, label=None, vlim=None):
3030
else:
3131
vlims = {}
3232

33-
plt.imshow(
34-
attribute.squeeze(),
35-
cmap=cmap,
36-
extent=[dem.bounds.left, dem.bounds.right, dem.bounds.bottom, dem.bounds.top],
37-
**vlims,
38-
)
39-
if label is not None:
40-
cbar = plt.colorbar()
41-
cbar.set_label(label)
33+
attribute.plot(cmap=cmap, cbar_title=label)
4234

4335
plt.xticks([])
4436
plt.yticks([])
@@ -51,14 +43,14 @@ def plot_attribute(attribute, cmap, label=None, vlim=None):
5143
# Slope with method of `Horn (1981) <http://dx.doi.org/10.1109/PROC.1981.11918>`_ (GDAL default), based on a refined
5244
# approximation of the gradient (page 18, bottom left, and pages 20-21).
5345

54-
slope_horn = xdem.terrain.slope(dem.data, resolution=dem.res)
46+
slope_horn = xdem.terrain.slope(dem)
5547

5648
plot_attribute(slope_horn, "Reds", "Slope of Horn (1981) (°)")
5749

5850
# %%
5951
# Slope with method of `Zevenbergen and Thorne (1987) <http://dx.doi.org/10.1002/esp.3290120107>`_, Equation 13.
6052

61-
slope_zevenberg = xdem.terrain.slope(dem.data, resolution=dem.res, method="ZevenbergThorne")
53+
slope_zevenberg = xdem.terrain.slope(dem, method="ZevenbergThorne")
6254

6355
plot_attribute(slope_zevenberg, "Reds", "Slope of Zevenberg and Thorne (1987) (°)")
6456

@@ -73,7 +65,7 @@ def plot_attribute(attribute, cmap, label=None, vlim=None):
7365
# The differences are negative, implying that the method of Horn always provides flatter slopes.
7466
# Additionally, they seem to occur in places of high curvatures. We verify this by plotting the maximum curvature.
7567

76-
maxc = xdem.terrain.maximum_curvature(dem.data, resolution=dem.res)
68+
maxc = xdem.terrain.maximum_curvature(dem)
7769

7870
plot_attribute(maxc, "RdYlBu", "Maximum curvature (100 m $^{-1}$)", vlim=2)
7971

@@ -102,8 +94,8 @@ def plot_attribute(attribute, cmap, label=None, vlim=None):
10294
# We perform the same exercise to analyze the differences in terrain aspect. We compute the difference modulo 360°,
10395
# to account for the circularity of aspect.
10496

105-
aspect_horn = xdem.terrain.aspect(dem.data)
106-
aspect_zevenberg = xdem.terrain.aspect(dem.data, method="ZevenbergThorne")
97+
aspect_horn = xdem.terrain.aspect(dem)
98+
aspect_zevenberg = xdem.terrain.aspect(dem, method="ZevenbergThorne")
10799

108100
diff_aspect = aspect_horn - aspect_zevenberg
109101
diff_aspect_mod = np.minimum(np.mod(diff_aspect, 360), 360 - np.mod(diff_aspect, 360))
Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,59 @@
11
"""
2-
DEM subtraction
3-
===============
2+
DEM differencing
3+
================
44
5-
Subtracting one DEM with another should be easy!
6-
This is why ``xdem`` (with functionality from `geoutils <https://geoutils.readthedocs.io>`_) allows directly using the ``-`` or ``+`` operators on :class:`xdem.DEM` objects.
5+
Subtracting a DEM with another one should be easy.
76
8-
Before DEMs can be compared, they need to be reprojected/resampled/cropped to fit the same grid.
9-
The :func:`xdem.DEM.reproject` method takes care of this.
7+
xDEM allows directly using any operator on :class:`xdem.DEM` objects, such as :func:`+<operator.add>` or :func:``-<operator.sub>`` as well as most NumPy functions,
8+
while respecting nodata values and checking that georeferencing is consistent.
9+
This functionality is inherited from `GeoUtils' Raster class <https://geoutils.readthedocs.io>`_.
10+
11+
Before DEMs can be compared, they need to be reprojected to the same grid and have the same 3D CRSs. The :func:`geoutils.Raster.reproject` and :func:`xdem.DEM.to_vcrs` methods are used for this.
1012
1113
"""
1214
import geoutils as gu
13-
import matplotlib.pyplot as plt
14-
1515
import xdem
1616

1717
# %%
18+
# We load two DEMs near Longyearbyen, Svalbard.
1819

1920
dem_2009 = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem"))
2021
dem_1990 = xdem.DEM(xdem.examples.get_path("longyearbyen_tba_dem_coreg"))
2122

2223
# %%
23-
# We can print the information about the DEMs for a "sanity check"
24+
# We can print the information about the DEMs for a "sanity check".
2425

25-
print(dem_2009)
26-
print(dem_1990)
26+
dem_2009.info()
27+
dem_1990.info()
2728

2829
# %%
2930
# In this particular case, the two DEMs are already on the same grid (they have the same bounds, resolution and coordinate system).
30-
# If they don't, we need to reproject one DEM to fit the other.
31-
# :func:`xdem.DEM.reproject` is a multi-purpose method that ensures a fit each time:
31+
# If they don't, we need to reproject one DEM to fit the other using :func:`geoutils.Raster.reproject`:
3232

33-
_ = dem_1990.reproject(dem_2009)
33+
dem_1990 = dem_1990.reproject(dem_2009)
3434

3535
# %%
3636
# Oops!
37-
# ``xdem`` just warned us that ``dem_1990`` did not need reprojection, but we asked it to anyway.
38-
# To hide this prompt, add ``.reproject(..., silent=True)``.
37+
# GeoUtils just warned us that ``dem_1990`` did not need reprojection. We can hide this output with ``.reproject(..., silent=True)``.
3938
# By default, :func:`xdem.DEM.reproject` uses "bilinear" resampling (assuming resampling is needed).
40-
# Other options are "nearest" (fast but inaccurate), "cubic_spline", "lanczos" and others.
41-
# See `geoutils.Raster.reproject() <https://geoutils.readthedocs.io/en/latest/api.html#geoutils.raster.Raster.reproject>`_ and `rasterio.enums.Resampling <https://rasterio.readthedocs.io/en/latest/api/rasterio.enums.html#rasterio.enums.Resampling>`_ for more information about reprojection.
39+
# Other options are detailed at `geoutils.Raster.reproject() <https://geoutils.readthedocs.io/en/latest/api.html#geoutils.raster.Raster.reproject>`_ and `rasterio.enums.Resampling <https://rasterio.readthedocs.io/en/latest/api/rasterio.enums.html#rasterio.enums.Resampling>`_.
4240
#
43-
# Now, we are ready to generate the dDEM:
41+
# We now compute the difference by simply substracting, passing `stats=True` to :func:`geoutils.Raster.info` to print statistics.
4442

4543
ddem = dem_2009 - dem_1990
4644

47-
print(ddem)
45+
ddem.info(stats=True)
4846

4947
# %%
5048
# It is a new :class:`xdem.DEM` instance, loaded in memory.
51-
# Let's visualize it:
52-
53-
ddem.plot(cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)")
54-
55-
# %%
56-
# Let's add some glacier outlines
49+
# Let's visualize it, with some glacier outlines.
5750

5851
# Load the outlines
5952
glacier_outlines = gu.Vector(xdem.examples.get_path("longyearbyen_glacier_outlines"))
60-
61-
# Need to create a common matplotlib Axes to plot both on the same figure
62-
# The xlim/ylim commands are necessary only because outlines extend further than the raster extent
63-
ax = plt.subplot(111)
64-
ddem.plot(ax=ax, cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)")
65-
glacier_outlines.ds.plot(ax=ax, fc="none", ec="k")
66-
plt.xlim(ddem.bounds.left, ddem.bounds.right)
67-
plt.ylim(ddem.bounds.bottom, ddem.bounds.top)
68-
plt.title("With glacier outlines")
69-
plt.show()
70-
71-
# %%
72-
# For missing values, ``xdem`` provides a number of interpolation methods which are shown in the other examples.
53+
ddem.plot(cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)")
54+
glacier_outlines.plot(ref_crs=ddem, fc="none", ec="k")
7355

7456
# %%
75-
# Saving the output to a file is also very simple
57+
# And we save the output to file.
7658

7759
ddem.save("temp.tif")
78-
79-
# %%
80-
# ... and that's it!

examples/basic/plot_icp_coregistration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
2-
Iterative Closest Point coregistration
2+
Iterative closest point coregistration
33
======================================
4-
Some DEMs may for one or more reason be erroneously rotated in the X, Y or Z directions.
5-
Established coregistration approaches like :ref:`coregistration-nuthkaab` work great for X, Y and Z *translations*, but rotation is not accounted for at all.
4+
Some DEMs are erroneously rotated in the X, Y or Z directions.
5+
Coregistration approaches such as :ref:`coregistration-nuthkaab` work well for X, Y and Z *translations*, but rotation is not accounted for at all.
66
7-
Iterative Closest Point (ICP) is one method that takes both rotation and translation into account.
7+
Iterative Closest Point (ICP) is a method that takes both rotation and translation into account.
88
It is however not as good as :ref:`coregistration-nuthkaab` when it comes to sub-pixel accuracy.
9-
Fortunately, ``xdem`` provides the best of two worlds by allowing a combination of the two.
9+
Fortunately, xDEM provides the best of two worlds by allowing a combination of the two in a pipeline.
1010
1111
**Reference**: `Besl and McKay (1992) <https://doi.org/10.1117/12.57955>`_.
1212
"""

0 commit comments

Comments
 (0)