forked from GlacioHack/xdem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor improvements to gallery examples (GlacioHack#493)
- Loading branch information
Showing
4 changed files
with
39 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,60 @@ | ||
""" | ||
DEM subtraction | ||
=============== | ||
DEM differencing | ||
================ | ||
Subtracting one DEM with another should be easy! | ||
This is why ``xdem`` (with functionality from `geoutils <https://geoutils.readthedocs.io>`_) allows directly using the ``-`` or ``+`` operators on :class:`xdem.DEM` objects. | ||
Subtracting a DEM with another one should be easy. | ||
Before DEMs can be compared, they need to be reprojected/resampled/cropped to fit the same grid. | ||
The :func:`xdem.DEM.reproject` method takes care of this. | ||
xDEM allows to use any operator on :class:`xdem.DEM` objects, such as :func:`+<operator.add>` or :func:`-<operator.sub>` as well as most NumPy functions | ||
while respecting nodata values and checking that georeferencing is consistent. This functionality is inherited from `GeoUtils' Raster class <https://geoutils.readthedocs.io>`_. | ||
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. | ||
""" | ||
import geoutils as gu | ||
import matplotlib.pyplot as plt | ||
|
||
import xdem | ||
|
||
# %% | ||
# We load two DEMs near Longyearbyen, Svalbard. | ||
|
||
dem_2009 = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem")) | ||
dem_1990 = xdem.DEM(xdem.examples.get_path("longyearbyen_tba_dem_coreg")) | ||
|
||
# %% | ||
# We can print the information about the DEMs for a "sanity check" | ||
# We can print the information about the DEMs for a "sanity check". | ||
|
||
print(dem_2009) | ||
print(dem_1990) | ||
dem_2009.info() | ||
dem_1990.info() | ||
|
||
# %% | ||
# In this particular case, the two DEMs are already on the same grid (they have the same bounds, resolution and coordinate system). | ||
# If they don't, we need to reproject one DEM to fit the other. | ||
# :func:`xdem.DEM.reproject` is a multi-purpose method that ensures a fit each time: | ||
# If they don't, we need to reproject one DEM to fit the other using :func:`geoutils.Raster.reproject`: | ||
|
||
_ = dem_1990.reproject(dem_2009) | ||
dem_1990 = dem_1990.reproject(dem_2009) | ||
|
||
# %% | ||
# Oops! | ||
# ``xdem`` just warned us that ``dem_1990`` did not need reprojection, but we asked it to anyway. | ||
# To hide this prompt, add ``.reproject(..., silent=True)``. | ||
# GeoUtils just warned us that ``dem_1990`` did not need reprojection. We can hide this output with ``.reproject(..., silent=True)``. | ||
# By default, :func:`xdem.DEM.reproject` uses "bilinear" resampling (assuming resampling is needed). | ||
# Other options are "nearest" (fast but inaccurate), "cubic_spline", "lanczos" and others. | ||
# 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. | ||
# 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>`_. | ||
# | ||
# Now, we are ready to generate the dDEM: | ||
# We now compute the difference by simply substracting, passing `stats=True` to :func:`geoutils.Raster.info` to print statistics. | ||
|
||
ddem = dem_2009 - dem_1990 | ||
|
||
print(ddem) | ||
ddem.info(stats=True) | ||
|
||
# %% | ||
# It is a new :class:`xdem.DEM` instance, loaded in memory. | ||
# Let's visualize it: | ||
|
||
ddem.plot(cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)") | ||
|
||
# %% | ||
# Let's add some glacier outlines | ||
# Let's visualize it, with some glacier outlines. | ||
|
||
# Load the outlines | ||
glacier_outlines = gu.Vector(xdem.examples.get_path("longyearbyen_glacier_outlines")) | ||
|
||
# Need to create a common matplotlib Axes to plot both on the same figure | ||
# The xlim/ylim commands are necessary only because outlines extend further than the raster extent | ||
ax = plt.subplot(111) | ||
ddem.plot(ax=ax, cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)") | ||
glacier_outlines.ds.plot(ax=ax, fc="none", ec="k") | ||
plt.xlim(ddem.bounds.left, ddem.bounds.right) | ||
plt.ylim(ddem.bounds.bottom, ddem.bounds.top) | ||
plt.title("With glacier outlines") | ||
plt.show() | ||
|
||
# %% | ||
# For missing values, ``xdem`` provides a number of interpolation methods which are shown in the other examples. | ||
glacier_outlines = glacier_outlines.crop(ddem, clip=True) | ||
ddem.plot(cmap="coolwarm_r", vmin=-20, vmax=20, cbar_title="Elevation change (m)") | ||
glacier_outlines.plot(ref_crs=ddem, fc="none", ec="k") | ||
|
||
# %% | ||
# Saving the output to a file is also very simple | ||
# And we save the output to file. | ||
|
||
ddem.save("temp.tif") | ||
|
||
# %% | ||
# ... and that's it! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters