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

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

.. _sphx_glr_io_examples_import_export_import_raster.py:


From/to Rasterio
================

This example demonstrates importing and exporting a :class:`rasterio.io.DatasetReader` or :class:`rasterio.io.DatasetReader` from and to a
:class:`~geoutils.Raster`.

.. GENERATED FROM PYTHON SOURCE LINES 8-11

.. code-block:: Python


    import rasterio as rio








.. GENERATED FROM PYTHON SOURCE LINES 12-13

A raster can be imported from a :class:`rasterio.io.DatasetReader` or :class:`rasterio.io.MemoryFile` simply by instantiating :class:`~geoutils.Raster`.

.. GENERATED FROM PYTHON SOURCE LINES 13-20

.. code-block:: Python

    import geoutils as gu

    filename_rast = gu.examples.get_path("exploradores_aster_dem")
    ds = rio.DatasetReader(filename_rast)
    rast = gu.Raster(ds)
    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, 618, 539); will load (618, 539)</i>
      <b>transform=</b>| 30.00, 0.00, 627175.00|
                | 0.00,-30.00, 4852085.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32718
      <b>nodata=</b>-9999.0)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 21-23

The data is unloaded, as when instantiated with a filename.
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 23-25

.. code-block:: Python

    rast.plot(cmap="terrain")




.. image-sg:: /io_examples/import_export/images/sphx_glr_import_raster_001.png
   :alt: import raster
   :srcset: /io_examples/import_export/images/sphx_glr_import_raster_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 26-27

We can also pass a :class:`rasterio.io.MemoryFile` during instantiation.

.. GENERATED FROM PYTHON SOURCE LINES 27-32

.. code-block:: Python


    mem = rio.MemoryFile(open(filename_rast, "rb"))
    rast = gu.Raster(mem)
    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>[[1271.3924560546875 1275.370849609375 1250.194091796875 ...
             1222.8572998046875 1220.2845458984375 1226.8214111328125]
            [1257.896484375 1259.9190673828125 1245.8509521484375 ...
             1224.7470703125 1221.00927734375 1228.203369140625]
            [1186.001953125 1160.00537109375 1154.0457763671875 ...
             1231.4954833984375 1227.606689453125 1236.4666748046875]
            ...
            [2690.725830078125 2695.724609375 2700.945556640625 ...
             1391.3909912109375 1411.371826171875 1436.488525390625]
            [2688.682373046875 2693.766845703125 2697.1826171875 ...
             1399.401123046875 1423.4818115234375 1451.060546875]
            [2686.294677734375 2692.3466796875 2694.712890625 ... 1413.9849853515625
             1437.3868408203125 1461.232177734375]]
      <b>transform=</b>| 30.00, 0.00, 627175.00|
                | 0.00,-30.00, 4852085.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:32718
      <b>nodata=</b>-9999.0)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 33-37

The data is, as expected, already in memory.

Finally, we can export a :class:`~geoutils.Raster` to a :class:`rasterio.io.DatasetReader` of a :class:`rasterio.io.MemoryFile` using
:class:`~geoutils.Raster.to_rio_dataset`

.. GENERATED FROM PYTHON SOURCE LINES 37-39

.. code-block:: Python


    rast.to_rio_dataset()




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

 .. code-block:: none


    <open DatasetReader name='/vsimem/8b7fe82f-e2a0-4ea2-bd3b-b01fea21fd2f/8b7fe82f-e2a0-4ea2-bd3b-b01fea21fd2f.tif' mode='r'>




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

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


.. _sphx_glr_download_io_examples_import_export_import_raster.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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