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

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

.. _sphx_glr_handling_examples_georeferencing_crop_vector.py:


Crop a vector
=============

This example demonstrates the cropping of a vector using :func:`geoutils.Vector.crop`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open a raster and vector.

.. GENERATED FROM PYTHON SOURCE LINES 10-18

.. code-block:: Python


    import geoutils as gu

    filename_rast = gu.examples.get_path("everest_landsat_b4_cropped")
    filename_vect = gu.examples.get_path("everest_rgi_outlines")
    rast = gu.Raster(filename_rast)
    vect = gu.Vector(filename_vect)








.. GENERATED FROM PYTHON SOURCE LINES 20-21

Let's plot the raster and vector. The raster has smaller extent than the vector.

.. GENERATED FROM PYTHON SOURCE LINES 21-24

.. code-block:: Python

    rast.plot(cmap="Greys_r", alpha=0.7)
    vect.plot(ref_crs=rast, fc="none", ec="tab:purple", lw=3)




.. image-sg:: /handling_examples/georeferencing/images/sphx_glr_crop_vector_001.png
   :alt: crop vector
   :srcset: /handling_examples/georeferencing/images/sphx_glr_crop_vector_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 25-27

**First option:** using the raster as a reference to match, we crop the vector. We simply have to pass the :class:`~geoutils.Raster` as single argument to
:func:`~geoutils.Vector.crop`. See :ref:`core-match-ref` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 27-30

.. code-block:: Python


    vect = vect.crop(rast)








.. GENERATED FROM PYTHON SOURCE LINES 31-34

.. note::
     By default, :func:`~geoutils.Vector.crop` is done in-place, replacing ``vect``. This behaviour can be modified by passing ``inplace=False``.


.. GENERATED FROM PYTHON SOURCE LINES 34-38

.. code-block:: Python


    rast.plot(ax="new", cmap="Greys_r", alpha=0.7)
    vect.plot(ref_crs=rast, fc="none", ec="tab:purple", lw=3)




.. image-sg:: /handling_examples/georeferencing/images/sphx_glr_crop_vector_002.png
   :alt: crop vector
   :srcset: /handling_examples/georeferencing/images/sphx_glr_crop_vector_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 39-41

The :func:`~geoutils.Vector.crop` keeps all features with geometries intersecting the extent to crop to. We can also force a clipping of the geometries
within the bounds using ``clip=True``.

.. GENERATED FROM PYTHON SOURCE LINES 41-46

.. code-block:: Python


    vect = vect.crop(rast, clip=True)
    rast.plot(ax="new", cmap="Greys_r", alpha=0.7)
    vect.plot(ref_crs=rast, fc="none", ec="tab:purple", lw=3)




.. image-sg:: /handling_examples/georeferencing/images/sphx_glr_crop_vector_003.png
   :alt: crop vector
   :srcset: /handling_examples/georeferencing/images/sphx_glr_crop_vector_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 47-49

**Second option:** we can pass other ``crop_geom`` argument to :func:`~geoutils.Vector.crop`, including another :class:`~geoutils.Vector` or a
simple :class:`tuple` of bounds.

.. GENERATED FROM PYTHON SOURCE LINES 49-55

.. code-block:: Python


    bounds = rast.get_bounds_projected(out_crs=vect.crs)
    vect = vect.crop(crop_geom=(bounds.left + 0.5 * (bounds.right - bounds.left), bounds.bottom, bounds.right, bounds.top))

    rast.plot(ax="new", cmap="Greys_r", alpha=0.7)
    vect.plot(ref_crs=rast, fc="none", ec="tab:purple", lw=3)



.. image-sg:: /handling_examples/georeferencing/images/sphx_glr_crop_vector_004.png
   :alt: crop vector
   :srcset: /handling_examples/georeferencing/images/sphx_glr_crop_vector_004.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_handling_examples_georeferencing_crop_vector.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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