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

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

.. _sphx_glr_handling_examples_georeferencing_crop_raster.py:


Crop a raster
=============

This example demonstrates the cropping of a raster using :func:`geoutils.Raster.crop`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open a raster and vector, and subset the latter.

.. GENERATED FROM PYTHON SOURCE LINES 10-19

.. 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"]








.. GENERATED FROM PYTHON SOURCE LINES 21-22

The first raster has larger extent and higher resolution than the vector.

.. GENERATED FROM PYTHON SOURCE LINES 22-25

.. code-block:: Python

    rast.info()
    print(vect.bounds)





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

 .. code-block:: none

    Driver:               GTiff 
    Opened from file:     /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif 
    Filename:             /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif 
    Loaded?               False 
    Modified since load?  False 
    Grid size:            800, 655
    Number of bands:      1
    Data types:           uint8
    Coordinate system:    ['EPSG:32645']
    Nodata value:         None
    Pixel interpretation: Point
    Pixel size:           30.0, 30.0
    Upper left corner:    478000.0, 3108140.0
    Lower right corner:   502000.0, 3088490.0

    BoundingBox(left=np.float64(86.89820460200008), bottom=np.float64(28.00781958500005), right=np.float64(86.97674208800004), top=np.float64(28.095011473000056))




.. GENERATED FROM PYTHON SOURCE LINES 26-27

Let's plot the raster and vector.

.. GENERATED FROM PYTHON SOURCE LINES 27-30

.. code-block:: Python

    rast.plot(cmap="Purples")
    vect.plot(ref_crs=rast, fc="none", ec="k", lw=2)




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





.. GENERATED FROM PYTHON SOURCE LINES 31-33

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

.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: Python


    rast = rast.crop(vect)








.. GENERATED FROM PYTHON SOURCE LINES 37-42

Now the bounds should be the same as that of the vector (within the size of a pixel as the grid was not warped).

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


.. GENERATED FROM PYTHON SOURCE LINES 42-46

.. code-block:: Python


    rast.plot(ax="new", cmap="Purples")
    vect.plot(ref_crs=rast, fc="none", ec="k", lw=2)




.. image-sg:: /handling_examples/georeferencing/images/sphx_glr_crop_raster_002.png
   :alt: crop raster
   :srcset: /handling_examples/georeferencing/images/sphx_glr_crop_raster_002.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.Raster.crop`, including another :class:`~geoutils.Raster` or a
simple :class:`tuple` of bounds. For instance, we can re-crop the raster to be smaller than the vector.

.. GENERATED FROM PYTHON SOURCE LINES 49-54

.. code-block:: Python


    rast = rast.crop((rast.bounds.left + 1000, rast.bounds.bottom, rast.bounds.right, rast.bounds.top - 500))

    rast.plot(ax="new", cmap="Purples")
    vect.plot(ref_crs=rast, fc="none", ec="k", lw=2)



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






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

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


.. _sphx_glr_download_handling_examples_georeferencing_crop_raster.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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