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

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

.. _sphx_glr_handling_examples_raster_point_reduction.py:


Reduce raster around points
===========================

This example demonstrates the reduction of windowed raster values around a point using :func:`~geoutils.Raster.reduce_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_reduction_001.png
   :alt: reduction
   :srcset: /handling_examples/raster_point/images/sphx_glr_reduction_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 22-23

We generate a random subsample of 100 coordinates to extract.

.. GENERATED FROM PYTHON SOURCE LINES 23-34

.. code-block:: Python


    import geopandas as gpd
    import numpy as np

    # Replace by Raster function once done
    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.reduce_points((x_coords, y_coords))








.. GENERATED FROM PYTHON SOURCE LINES 35-36

We plot the resulting point cloud

.. GENERATED FROM PYTHON SOURCE LINES 36-38

.. code-block:: Python

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




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





.. GENERATED FROM PYTHON SOURCE LINES 39-41

By default, :func:`~geoutils.Raster.reduce_points` extracts the closest pixel value. But it can also be passed a window size and reductor function to
extract an average value or other statistic based on neighbouring pixels.

.. GENERATED FROM PYTHON SOURCE LINES 41-46

.. code-block:: Python


    pc_reduced = rast.reduce_points((x_coords, y_coords), window=5, reducer_function=np.nanmedian)

    np.nanmean(pc - pc_reduced)





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

 .. code-block:: none


    np.float32(0.1735498)



.. GENERATED FROM PYTHON SOURCE LINES 47-49

The mean difference in extracted values is quite significant at 0.3 meters!
We can visualize how the sampling took place in the windows.

.. GENERATED FROM PYTHON SOURCE LINES 49-63

.. code-block:: Python


    # Replace by Vector function once done
    coords = rast.coords(grid=True)
    x_closest = rast.copy(new_array=coords[0]).reduce_points((x_coords, y_coords), as_array=True).squeeze()
    y_closest = rast.copy(new_array=coords[1]).reduce_points((x_coords, y_coords), as_array=True).squeeze()
    from shapely.geometry import box

    geometry = [
        box(x - 2 * rast.res[0], y - 2 * rast.res[1], x + 2 * rast.res[0], y + 2 * rast.res[1])
        for x, y in zip(x_closest, y_closest)
    ]
    ds = gpd.GeoDataFrame(geometry=geometry, crs=rast.crs)
    ds["vals"] = pc_reduced.data
    ds.plot(column="vals", cmap="terrain", legend=True, vmin=np.nanmin(rast), vmax=np.nanmax(rast))



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


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

 .. code-block:: none


    <Axes: >




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

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


.. _sphx_glr_download_handling_examples_raster_point_reduction.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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