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

.. only:: html

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

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

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

.. _sphx_glr_analysis_examples_array_numerics_numpy_interfacing.py:


NumPy interfacing
=================

This example demonstrates NumPy interfacing with rasters on :class:`Rasters<geoutils.Raster>`. See :ref:`core-array-funcs` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open a raster.

.. GENERATED FROM PYTHON SOURCE LINES 10-16

.. code-block:: Python


    import geoutils as gu

    filename_rast = gu.examples.get_path("exploradores_aster_dem")
    rast = gu.Raster(filename_rast)








.. GENERATED FROM PYTHON SOURCE LINES 18-20

.. code-block:: Python

    rast.plot(cmap="terrain")




.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_001.png
   :alt: numpy interfacing
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 21-22

The NumPy interface allows to use almost any NumPy function directly on the raster.

.. GENERATED FROM PYTHON SOURCE LINES 23-34

.. code-block:: Python


    import numpy as np

    # Get the x and y gradient as 1D arrays
    gradient_y, gradient_x = np.gradient(rast)
    # Estimate the orientation in degrees casting to 2D
    aspect = np.arctan2(-gradient_x, gradient_y)
    aspect = (aspect * 180 / np.pi) + np.pi

    aspect.plot(cmap="twilight", cbar_title="Aspect (degrees)")




.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_002.png
   :alt: numpy interfacing
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 35-40

.. important::
       For rigorous slope and aspect calculation (matching that of GDAL), **check-out our sister package** `xDEM <https://xdem.readthedocs.io/en/latest/index.html>`_.

We use NumPy logical operations to isolate the terrain oriented South and above three thousand meters. The rasters will be logically cast to a
boolean :class:`Raster<geoutils.Raster>`.

.. GENERATED FROM PYTHON SOURCE LINES 41-45

.. code-block:: Python


    mask = np.logical_and.reduce((aspect > -45, aspect < 45, rast > 3000))
    mask






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <pre><span style="white-space: pre-wrap"><b><em>Raster</em></b>(
      <b>data=</b>[[False False False ... False False False]
            [False False False ... False False False]
            [False False False ... False False False]
            ...
            [False False False ... False False False]
            [False False False ... False False False]
            [False False False ... False False False]]
      <b>transform=</b>| 30.00, 0.00, 627175.00|
                | 0.00,-30.00, 4852085.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32718
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 46-47

We plot the mask.

.. GENERATED FROM PYTHON SOURCE LINES 47-49

.. code-block:: Python


    mask.plot()



.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_003.png
   :alt: numpy interfacing
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_numpy_interfacing_003.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_analysis_examples_array_numerics_numpy_interfacing.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: numpy_interfacing.zip <numpy_interfacing.zip>`


.. only:: html

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

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