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

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

.. _sphx_glr_handling_examples_georeferencing_reproj_raster.py:


Reproject a raster
==================

This example demonstrates the reprojection of a raster using :func:`geoutils.Raster.reproject`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open two example rasters.

.. GENERATED FROM PYTHON SOURCE LINES 10-18

.. code-block:: Python


    import geoutils as gu

    filename_rast1 = gu.examples.get_path("everest_landsat_b4")
    filename_rast2 = gu.examples.get_path("everest_landsat_b4_cropped")
    rast1 = gu.Raster(filename_rast1)
    rast2 = gu.Raster(filename_rast2)








.. GENERATED FROM PYTHON SOURCE LINES 19-20

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

.. GENERATED FROM PYTHON SOURCE LINES 20-23

.. code-block:: Python

    rast1.info()
    rast2.info()





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

    Driver:               GTiff 
    Opened from file:     /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4_cropped.tif 
    Filename:             /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4_cropped.tif 
    Loaded?               False 
    Modified since load?  False 
    Grid size:            492, 315
    Number of bands:      1
    Data types:           uint8
    Coordinate system:    ['EPSG:32645']
    Nodata value:         None
    Pixel interpretation: Area
    Pixel size:           30.0, 30.0
    Upper left corner:    483430.0, 3102710.0
    Lower right corner:   498190.0, 3093260.0





.. GENERATED FROM PYTHON SOURCE LINES 24-25

Let's plot the first raster, with the warped extent of the second one.

.. GENERATED FROM PYTHON SOURCE LINES 25-30

.. code-block:: Python


    rast1.plot(cmap="Blues")
    vect_bounds_rast2 = gu.Vector.from_bounds_projected(rast2)
    vect_bounds_rast2.plot(fc="none", ec="r", lw=2)




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





.. GENERATED FROM PYTHON SOURCE LINES 31-35

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

By default, a "bilinear" resampling algorithm is used. Any string or :class:`~rasterio.enums.Resampling` can be passed.

.. GENERATED FROM PYTHON SOURCE LINES 35-39

.. code-block:: Python


    rast1_warped = rast1.reproject(rast2)
    rast1_warped





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

 .. code-block:: none

    /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/raster/_geotransformations.py:123: UserWarning: For reprojection, nodata must be set. Default chosen value 255 exists in self.data. This may have unexpected consequences. Consider setting a different nodata with self.set_nodata().
      warnings.warn(
    /home/docs/checkouts/readthedocs.org/user_builds/adebardo-geoutils/checkouts/latest/geoutils/raster/georeferencing.py:225: UserWarning: One raster has a pixel interpretation "Area" and the other "Point". To silence this warning, either correct the pixel interpretation of one raster, or deactivate warnings of pixel interpretation with geoutils.config["warn_area_or_point"]=False.
      warnings.warn(message=msg, category=UserWarning)


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <pre><span style="white-space: pre-wrap"><b><em>Raster</em></b>(
      <b>data=</b>[[93 100 109 ... -- -- --]
            [109 115 120 ... -- -- --]
            [117 121 108 ... -- -- --]
            ...
            [54 59 52 ... 60 68 116]
            [70 60 48 ... 60 60 88]
            [67 52 37 ... 59 59 82]]
      <b>transform=</b>| 30.00, 0.00, 483430.00|
                | 0.00,-30.00, 3102710.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>255)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 40-46

.. note::
  Because no :attr:`geoutils.Raster.nodata` value is defined in the original image, the default value ``255`` for :class:`numpy.uint8` is used. This
  value is detected as already existing in the original raster, however, which raises a ``UserWarning``. If your :attr:`geoutils.Raster.nodata` is not defined,
  use :func:`geoutils.Raster.set_nodata`.

Now the shape and georeferencing should be the same as that of the second raster, shown above.

.. GENERATED FROM PYTHON SOURCE LINES 46-49

.. code-block:: Python


    rast1_warped.info()





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

 .. code-block:: none

    Driver:               None 
    Opened from file:     None 
    Filename:             None 
    Loaded?               True 
    Modified since load?  True 
    Grid size:            492, 315
    Number of bands:      1
    Data types:           uint8
    Coordinate system:    ['EPSG:32645']
    Nodata value:         255
    Pixel interpretation: Point
    Pixel size:           30.0, 30.0
    Upper left corner:    483430.0, 3102710.0
    Lower right corner:   498190.0, 3093260.0





.. GENERATED FROM PYTHON SOURCE LINES 50-51

We can plot the two rasters next to one another

.. GENERATED FROM PYTHON SOURCE LINES 51-55

.. code-block:: Python


    rast1_warped.plot(ax="new", cmap="Reds")
    rast2.plot(ax="new", cmap="Blues")




.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /handling_examples/georeferencing/images/sphx_glr_reproj_raster_002.png
         :alt: reproj raster
         :srcset: /handling_examples/georeferencing/images/sphx_glr_reproj_raster_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /handling_examples/georeferencing/images/sphx_glr_reproj_raster_003.png
         :alt: reproj raster
         :srcset: /handling_examples/georeferencing/images/sphx_glr_reproj_raster_003.png
         :class: sphx-glr-multi-img





.. GENERATED FROM PYTHON SOURCE LINES 56-58

**Second option:** we can pass any georeferencing argument to :func:`~geoutils.Raster.reproject`, such as ``dst_size`` and ``dst_crs``, and will only
deduce other parameters from the raster from which it is called (for ``dst_crs``, an EPSG code can be passed directly as :class:`int`).

.. GENERATED FROM PYTHON SOURCE LINES 58-64

.. code-block:: Python


    # Ensure the right nodata value is set
    rast2.set_nodata(0)
    # Pass the desired georeferencing parameters
    rast2_warped = rast2.reproject(grid_size=(100, 100), crs=32645)
    rast2_warped.info()




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

 .. code-block:: none

    Driver:               None 
    Opened from file:     None 
    Filename:             None 
    Loaded?               True 
    Modified since load?  True 
    Grid size:            100, 100
    Number of bands:      1
    Data types:           uint8
    Coordinate system:    ['EPSG:32645']
    Nodata value:         0
    Pixel interpretation: Area
    Pixel size:           147.6, 94.5
    Upper left corner:    483430.0, 3102710.0
    Lower right corner:   498190.0, 3093260.0






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

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


.. _sphx_glr_download_handling_examples_georeferencing_reproj_raster.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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