
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "basic_examples/plot_terrain_attributes.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_basic_examples_plot_terrain_attributes.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_basic_examples_plot_terrain_attributes.py:


Terrain attributes
==================

Terrain attributes generated from a DEM have a multitude of uses for analytic and visual purposes.
Here is an example of how to generate these products.

For more information, see the :ref:`terrain-attributes` chapter and the
:ref:`sphx_glr_advanced_examples_plot_slope_methods.py` example.

.. GENERATED FROM PYTHON SOURCE LINES 11-15

.. code-block:: Python

    import matplotlib.pyplot as plt

    import xdem








.. GENERATED FROM PYTHON SOURCE LINES 17-18

**Example data**

.. GENERATED FROM PYTHON SOURCE LINES 18-46

.. code-block:: Python


    dem = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem"))


    def plot_attribute(attribute, cmap, label=None, vlim=None):

        add_cbar = True if label is not None else False

        fig = plt.figure(figsize=(8, 5))
        ax = fig.add_subplot(111)

        if vlim is not None:
            if isinstance(vlim, (int, float)):
                vlims = {"vmin": -vlim, "vmax": vlim}
            elif len(vlim) == 2:
                vlims = {"vmin": vlim[0], "vmax": vlim[1]}
        else:
            vlims = {}

        attribute.plot(ax=ax, cmap=cmap, add_cbar=add_cbar, cbar_title=label, **vlims)

        plt.xticks([])
        plt.yticks([])
        plt.tight_layout()

        plt.show()









.. GENERATED FROM PYTHON SOURCE LINES 47-49

Slope
-----

.. GENERATED FROM PYTHON SOURCE LINES 49-54

.. code-block:: Python


    slope = xdem.terrain.slope(dem)

    plot_attribute(slope, "Reds", "Slope (°)")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_001.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 55-56

Note that all functions also work with numpy array as inputs, if resolution is specified

.. GENERATED FROM PYTHON SOURCE LINES 56-59

.. code-block:: Python


    slope = xdem.terrain.slope(dem.data, resolution=dem.res)








.. GENERATED FROM PYTHON SOURCE LINES 60-62

Aspect
------

.. GENERATED FROM PYTHON SOURCE LINES 62-67

.. code-block:: Python


    aspect = xdem.terrain.aspect(dem)

    plot_attribute(aspect, "twilight", "Aspect (°)")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_002.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 68-70

Hillshade
---------

.. GENERATED FROM PYTHON SOURCE LINES 70-75

.. code-block:: Python


    hillshade = xdem.terrain.hillshade(dem, azimuth=315.0, altitude=45.0)

    plot_attribute(hillshade, "Greys_r")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_003.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 76-78

Curvature
---------

.. GENERATED FROM PYTHON SOURCE LINES 78-83

.. code-block:: Python


    curvature = xdem.terrain.curvature(dem)

    plot_attribute(curvature, "RdGy_r", "Curvature (100 / m)", vlim=1)




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_004.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 84-86

Planform curvature
------------------

.. GENERATED FROM PYTHON SOURCE LINES 86-91

.. code-block:: Python


    planform_curvature = xdem.terrain.planform_curvature(dem)

    plot_attribute(planform_curvature, "RdGy_r", "Planform curvature (100 / m)", vlim=1)




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_005.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_005.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 92-94

Profile curvature
-----------------

.. GENERATED FROM PYTHON SOURCE LINES 94-98

.. code-block:: Python

    profile_curvature = xdem.terrain.profile_curvature(dem)

    plot_attribute(profile_curvature, "RdGy_r", "Profile curvature (100 / m)", vlim=1)




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_006.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_006.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 99-101

Topographic Position Index
--------------------------

.. GENERATED FROM PYTHON SOURCE LINES 101-105

.. code-block:: Python

    tpi = xdem.terrain.topographic_position_index(dem)

    plot_attribute(tpi, "Spectral", "Topographic Position Index", vlim=5)




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_007.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_007.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 106-108

Terrain Ruggedness Index
------------------------

.. GENERATED FROM PYTHON SOURCE LINES 108-112

.. code-block:: Python

    tri = xdem.terrain.terrain_ruggedness_index(dem)

    plot_attribute(tri, "Purples", "Terrain Ruggedness Index")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_008.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_008.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 113-115

Roughness
---------

.. GENERATED FROM PYTHON SOURCE LINES 115-119

.. code-block:: Python

    roughness = xdem.terrain.roughness(dem)

    plot_attribute(roughness, "Oranges", "Roughness")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_009.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_009.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 120-122

Rugosity
--------

.. GENERATED FROM PYTHON SOURCE LINES 122-126

.. code-block:: Python

    rugosity = xdem.terrain.rugosity(dem)

    plot_attribute(rugosity, "YlOrRd", "Rugosity")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_010.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_010.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 127-129

Fractal roughness
-----------------

.. GENERATED FROM PYTHON SOURCE LINES 129-133

.. code-block:: Python

    fractal_roughness = xdem.terrain.fractal_roughness(dem)

    plot_attribute(fractal_roughness, "Reds", "Fractal roughness")




.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_011.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_011.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 134-136

Generating multiple attributes at once
--------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 136-162

.. code-block:: Python


    attributes = xdem.terrain.get_terrain_attribute(
        dem.data,
        resolution=dem.res,
        attribute=["hillshade", "slope", "aspect", "curvature", "terrain_ruggedness_index", "rugosity"],
    )

    plt.figure(figsize=(8, 6.5))

    plt_extent = [dem.bounds.left, dem.bounds.right, dem.bounds.bottom, dem.bounds.top]

    cmaps = ["Greys_r", "Reds", "twilight", "RdGy_r", "Purples", "YlOrRd"]
    labels = ["Hillshade", "Slope (°)", "Aspect (°)", "Curvature (100 / m)", "Terrain Ruggedness Index", "Rugosity"]
    vlims = [(None, None) for i in range(6)]
    vlims[3] = [-2, 2]

    for i in range(6):
        plt.subplot(3, 2, i + 1)
        plt.imshow(attributes[i].squeeze(), cmap=cmaps[i], extent=plt_extent, vmin=vlims[i][0], vmax=vlims[i][1])
        cbar = plt.colorbar()
        cbar.set_label(labels[i])
        plt.xticks([])
        plt.yticks([])

    plt.tight_layout()
    plt.show()



.. image-sg:: /basic_examples/images/sphx_glr_plot_terrain_attributes_012.png
   :alt: plot terrain attributes
   :srcset: /basic_examples/images/sphx_glr_plot_terrain_attributes_012.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 49.762 seconds)


.. _sphx_glr_download_basic_examples_plot_terrain_attributes.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_terrain_attributes.ipynb <plot_terrain_attributes.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_terrain_attributes.py <plot_terrain_attributes.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
