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

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

.. _sphx_glr_analysis_examples_distance_ops_proximity_metric.py:


Proximity to raster or vector
=============================

This example demonstrates the calculation of proximity distances to a raster or vector using :func:`~geoutils.Raster.proximity`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open an example raster, and a vector for which we select a single feature

.. GENERATED FROM PYTHON SOURCE LINES 10-24

.. code-block:: Python


    import geoutils as gu

    filename_rast = gu.examples.get_path("everest_landsat_b4")
    filename_vect = gu.examples.get_path("everest_rgi_outlines")
    rast = gu.Raster(filename_rast)
    vect = gu.Vector(filename_vect)
    vect = vect[vect["RGIId"] == "RGI60-15.10055"]
    rast = rast.crop(vect)

    # Plot the raster and vector
    rast.plot(cmap="Blues")
    vect.reproject(rast).plot(fc="none", ec="k", lw=2)




.. image-sg:: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_001.png
   :alt: proximity metric
   :srcset: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 26-27

We use the raster as a reference to match for rasterizing the proximity distances with :func:`~geoutils.Vector.proximity`. See :ref:`core-match-ref` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 27-31

.. code-block:: Python


    proximity = vect.proximity(rast)
    proximity.plot(cmap="viridis")




.. image-sg:: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_002.png
   :alt: proximity metric
   :srcset: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 32-33

Proximity can also be computed to target pixels of a raster, or that of a mask

.. GENERATED FROM PYTHON SOURCE LINES 33-40

.. code-block:: Python


    # Get mask of pixels within 30 of 200 infrared
    import numpy as np

    mask_200 = np.abs(rast - 200) < 30
    mask_200.plot()




.. image-sg:: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_003.png
   :alt: proximity metric
   :srcset: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 41-42

Because a mask is :class:`bool`, no need to pass target pixels

.. GENERATED FROM PYTHON SOURCE LINES 42-46

.. code-block:: Python


    proximity_mask = mask_200.proximity()
    proximity_mask.plot(cmap="viridis")




.. image-sg:: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_004.png
   :alt: proximity metric
   :srcset: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 47-49

By default, proximity is computed using the georeference unit from a :class:`~geoutils.Raster`'s :attr:`~geoutils.Raster.res`, here **meters**. It can also
be computed in pixels.

.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: Python


    proximity_mask = mask_200.proximity(distance_unit="pixel")
    proximity_mask.plot(cmap="viridis")



.. image-sg:: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_005.png
   :alt: proximity metric
   :srcset: /analysis_examples/distance_ops/images/sphx_glr_proximity_metric_005.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_analysis_examples_distance_ops_proximity_metric.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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