
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "io_examples/open_save/read_raster.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_io_examples_open_save_read_raster.py>`
        to download the full example code.

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

.. _sphx_glr_io_examples_open_save_read_raster.py:


Open/save a raster
==================

This example demonstrates the instantiation of a raster through :class:`~geoutils.Raster` and saving with :func:`~geoutils.Raster.to_file`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open an example raster. The data is, by default, unloaded.

.. GENERATED FROM PYTHON SOURCE LINES 10-16

.. code-block:: Python

    import geoutils as gu

    filename_rast = gu.examples.get_path("everest_landsat_b4")
    rast = gu.Raster(filename_rast)
    rast






.. 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><i>not_loaded; shape on disk (1, 655, 800); will load (655, 800)</i>
      <b>transform=</b>| 30.00, 0.00, 478000.00|
                | 0.00,-30.00, 3108140.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 17-20

A raster is composed of four main attributes: a :class:`~geoutils.Raster.data` array, an affine :class:`~geoutils.Raster.transform`,
a coordinate reference system :class:`~geoutils.Raster.crs` and a :class:`~geoutils.Raster.nodata` value.
All other attributes are derivatives of those or the file on disk, and can be found in the :ref:`dedicated section of the API<api-raster-attrs>`. See also :ref:`raster-class`.

.. GENERATED FROM PYTHON SOURCE LINES 22-26

.. note::
       A raster can also be instantiated with a :class:`rasterio.io.DatasetReader` or a :class:`rasterio.io.MemoryFile`, see :ref:`sphx_glr_io_examples_import_export_import_raster.py`.

We can print more info on the raster.

.. GENERATED FROM PYTHON SOURCE LINES 27-29

.. code-block:: Python

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





.. GENERATED FROM PYTHON SOURCE LINES 30-31

The data will be loaded explicitly by any function requiring its :attr:`~geoutils.Raster.data`, such as :func:`~geoutils.Raster.show`.

.. GENERATED FROM PYTHON SOURCE LINES 31-33

.. code-block:: Python

    rast.plot(cmap="Greys_r")




.. image-sg:: /io_examples/open_save/images/sphx_glr_read_raster_001.png
   :alt: read raster
   :srcset: /io_examples/open_save/images/sphx_glr_read_raster_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 34-36

Opening can be performed with several parameters, for instance choosing a single band with ``index`` and re-sampling with ``downsample``, to subset a 3-band
raster to its second band, and using 1 pixel out of 4.

.. GENERATED FROM PYTHON SOURCE LINES 36-39

.. code-block:: Python

    rast = gu.Raster(gu.examples.get_path("everest_landsat_rgb"), bands=2, downsample=4)
    rast






.. 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><i>not_loaded; shape on disk (3, 655, 800); will load (164, 200)</i>
      <b>transform=</b>| 120.00, 0.00, 478000.00|
                | 0.00,-120.00, 3108140.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 40-42

The data is not loaded by default, even if when specifying a band or re-sampling.
We can load it explicitly by calling :func:`~geoutils.Raster.load` (could have also passed ``load_data=True`` to :class:`~geoutils.Raster`).

.. GENERATED FROM PYTHON SOURCE LINES 42-45

.. code-block:: Python

    rast.load()
    rast






.. 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>[[255 255 255 ... 255 255 255]
            [255 255 255 ... 255 255 255]
            [255 255 255 ... 255 255 255]
            ...
            [ 38  66  92 ... 114 147 157]
            [ 86  54  29 ... 122 142 171]
            [ 94  35  35 ... 150 151 223]]
      <b>transform=</b>| 120.00, 0.00, 478000.00|
                | 0.00,-120.00, 3108140.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32645
      <b>nodata=</b>None)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 46-47

Finally, a raster is saved using :func:`~geoutils.Raster.to_file`:

.. GENERATED FROM PYTHON SOURCE LINES 47-48

.. code-block:: Python

    rast.to_file("myraster.tif")








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

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


.. _sphx_glr_download_io_examples_open_save_read_raster.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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