
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "analysis_examples/array_numerics/python_arithmetic.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_python_arithmetic.py>`
        to download the full example code.

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

.. _sphx_glr_analysis_examples_array_numerics_python_arithmetic.py:


Python arithmetic
=================

This example demonstrates arithmetic operations using raster arithmetic on :class:`Rasters<geoutils.Raster>`. See :ref:`core-py-ops` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open a raster

.. GENERATED FROM PYTHON SOURCE LINES 10-17

.. code-block:: Python


    import geoutils as gu

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






.. 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><i>not_loaded; shape on disk (1, 655, 800); will load (655, 800)</i>
      <b>transform=</b>| 30.00, 0.00, 478000.00|
                | 0.00,-30.00, 3108140.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 19-21

.. code-block:: Python

    rast.plot(cmap="Greys_r")




.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_001.png
   :alt: python arithmetic
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 22-23

Performing arithmetic operations implicitly loads the data.

.. GENERATED FROM PYTHON SOURCE LINES 23-26

.. code-block:: Python

    rast = (rast + 1.0) ** 0.5 / 5
    rast.plot(cmap="Greys_r")




.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_002.png
   :alt: python arithmetic
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 27-32

.. important::
       Arithmetic operations cast to new :class:`dtypes<numpy.dtype>` automatically following NumPy coercion rules. If we had written ``(rast + 1)``,
       this calculation would have conserved the original :class:`numpy.uint8` :class:`dtype<numpy.dtype>` of the raster.

Logical comparison operations will naturally cast to a boolean :class:`Raster<geoutils.Raster>`.

.. GENERATED FROM PYTHON SOURCE LINES 33-37

.. code-block:: Python


    mask = rast == 200
    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, 478000.00|
                | 0.00,-30.00, 3108140.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 38-39

Boolean :class:`Rasters<geoutils.Raster>` support python logical operators to be combined together

.. GENERATED FROM PYTHON SOURCE LINES 39-43

.. code-block:: Python


    mask = (rast >= 3) | (rast % 2 == 0) & (rast != 80)
    mask.plot()




.. image-sg:: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_003.png
   :alt: python arithmetic
   :srcset: /analysis_examples/array_numerics/images/sphx_glr_python_arithmetic_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 44-45

Finally, boolean :class:`Rasters<geoutils.Raster>` can be used for indexing and assigning to a :class:`Rasters<geoutils.Raster>`

.. GENERATED FROM PYTHON SOURCE LINES 45-47

.. code-block:: Python


    values = rast[mask]








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

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


.. _sphx_glr_download_analysis_examples_array_numerics_python_arithmetic.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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