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

.. only:: html

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

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

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

.. _sphx_glr_handling_examples_raster_point_interpolation.py:


Interpolate raster at points
============================

This example demonstrates the 2D interpolation of raster values to points using :func:`~geoutils.Raster.interp_points`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open an example raster, a digital elevation model in South America.

.. GENERATED FROM PYTHON SOURCE LINES 10-20

.. code-block:: Python


    import geoutils as gu

    filename_rast = gu.examples.get_path("exploradores_aster_dem")
    rast = gu.Raster(filename_rast)
    rast = rast.crop([rast.bounds.left, rast.bounds.bottom, rast.bounds.left + 2000, rast.bounds.bottom + 2000])

    # Plot the raster
    rast.plot(cmap="terrain")




.. image-sg:: /handling_examples/raster_point/images/sphx_glr_interpolation_001.png
   :alt: interpolation
   :srcset: /handling_examples/raster_point/images/sphx_glr_interpolation_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 22-23

We generate a random subsample of 100 coordinates to interpolate.

.. GENERATED FROM PYTHON SOURCE LINES 23-32

.. code-block:: Python


    import numpy as np

    rng = np.random.default_rng(42)
    x_coords = rng.uniform(rast.bounds.left + 50, rast.bounds.right - 50, 50)
    y_coords = rng.uniform(rast.bounds.bottom + 50, rast.bounds.top - 50, 50)

    pc = rast.interp_points(points=(x_coords, y_coords))








.. GENERATED FROM PYTHON SOURCE LINES 33-34

We plot the resulting point cloud

.. GENERATED FROM PYTHON SOURCE LINES 34-36

.. code-block:: Python

    pc.plot(ax="new", cmap="terrain", marker="x", cbar_title="Elevation (m)")




.. image-sg:: /handling_examples/raster_point/images/sphx_glr_interpolation_002.png
   :alt: interpolation
   :srcset: /handling_examples/raster_point/images/sphx_glr_interpolation_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 37-40

.. important::
      The interpretation of where raster values are located can differ. The parameter ``shift_area_or_point`` (off by default) can be turned on to ensure
      that the pixel interpretation of your dataset is correct.

.. GENERATED FROM PYTHON SOURCE LINES 42-43

Let's look and redefine our pixel interpretation into ``"Point"``. This will shift interpolation by half a pixel.

.. GENERATED FROM PYTHON SOURCE LINES 43-47

.. code-block:: Python


    rast.area_or_point
    rast.area_or_point = "Point"








.. GENERATED FROM PYTHON SOURCE LINES 48-49

We can interpolate again by shifting according to our interpretation, and changing the resampling algorithm (default to "linear").

.. GENERATED FROM PYTHON SOURCE LINES 49-53

.. code-block:: Python


    pc_shifted = rast.interp_points(points=(x_coords, y_coords), shift_area_or_point=True, method="quintic")
    np.nanmean(pc - pc_shifted)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    np.float32(-0.048779298)



.. GENERATED FROM PYTHON SOURCE LINES 54-55

The mean difference in interpolated values is quite significant, with a 2-meter bias!


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

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


.. _sphx_glr_download_handling_examples_raster_point_interpolation.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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