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

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

.. _sphx_glr_handling_examples_raster_vector_polygonize.py:


Polygonize a raster
===================

This example demonstrates the polygonizing of a raster using :func:`geoutils.Raster.polygonize`.

.. 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)
    rast = rast.crop([rast.bounds.left, rast.bounds.bottom, rast.bounds.left + 5000, rast.bounds.bottom + 5000])







.. GENERATED FROM PYTHON SOURCE LINES 18-19

Let's plot the raster.

.. GENERATED FROM PYTHON SOURCE LINES 19-21

.. code-block:: Python

    rast.plot(cmap="terrain")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_polygonize_001.png
   :alt: polygonize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_polygonize_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 22-23

We polygonize the raster.

.. GENERATED FROM PYTHON SOURCE LINES 23-27

.. code-block:: Python


    rast_polygonized = rast.polygonize()
    rast_polygonized.plot(ax="new")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_polygonize_002.png
   :alt: polygonize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_polygonize_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 28-30

By default, :func:`~geoutils.Raster.polygonize` will try to polygonize target all valid values. Instead, one can specify discrete values to target by
passing a number or :class:`list`, or a range of values by passing a :class:`tuple`.

.. GENERATED FROM PYTHON SOURCE LINES 30-35

.. code-block:: Python


    # A range of values to polygonize
    rast_polygonized = rast.polygonize((2500, 3000))
    rast_polygonized.plot(ax="new")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_polygonize_003.png
   :alt: polygonize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_polygonize_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 36-38

An even simpler way to do this is to compute a boolean :func:`~geoutils.Raster` to polygonize using logical
comparisons on the :func:`~geoutils.Raster`.

.. GENERATED FROM PYTHON SOURCE LINES 38-42

.. code-block:: Python


    rast_polygonized = ((2500 < rast) & (rast < 3000)).polygonize()
    rast_polygonized.plot(ax="new")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_polygonize_004.png
   :alt: polygonize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_polygonize_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 43-45

.. note::
          See :ref:`core-py-ops` for more details on casting to boolean :func:`~geoutils.Raster`.


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

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


.. _sphx_glr_download_handling_examples_raster_vector_polygonize.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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