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

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

.. _sphx_glr_io_examples_import_export_raster_from_array.py:


Creating a raster from array
============================

This example demonstrates the creation of a raster through :func:`~geoutils.Raster.from_array`.

.. GENERATED FROM PYTHON SOURCE LINES 7-12

.. code-block:: Python


    import numpy as np
    import pyproj
    import rasterio as rio








.. GENERATED FROM PYTHON SOURCE LINES 13-14

We create a data array as :class:`~numpy.ndarray`, a transform as :class:`affine.Affine` and a coordinate reference system (CRS) as :class:`pyproj.CRS`.

.. GENERATED FROM PYTHON SOURCE LINES 14-30

.. code-block:: Python

    import geoutils as gu

    # A random 3 x 3 masked array
    rng = np.random.default_rng(42)
    arr = rng.normal(size=(5, 5))
    # Introduce a NaN value
    arr[2, 2] = np.nan
    # A transform with 3 x 3 pixels in a [0-1, 0-1] bound square
    transform = rio.transform.from_bounds(0, 0, 1, 1, 3, 3)
    # A CRS, here geographic (latitude/longitude)
    crs = pyproj.CRS.from_epsg(4326)

    # Create a raster
    rast = gu.Raster.from_array(data=arr, transform=transform, crs=crs, nodata=255)
    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>[[0.30471707975443135 -1.0399841062404955 0.7504511958064572
             0.9405647163912139 -1.9510351886538364]
            [-1.302179506862318 0.12784040316728537 -0.3162425923435822
             -0.016801157504288795 -0.85304392757358]
            [0.8793979748628286 0.7777919354289483 -- 1.1272412069680329
             0.4675093422520456]
            [-0.8592924628832382 0.36875078408249884 -0.9588826008289989
             0.8784503013072725 -0.049925910986252896]
            [-0.18486236354526056 -0.6809295444039414 1.2225413386740303
             -0.15452948206880215 -0.4283278221631072]]
      <b>transform=</b>| 0.33, 0.00, 0.00|
                | 0.00,-0.33, 1.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:4326
      <b>nodata=</b>255)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 31-32

We can print info on the raster.

.. GENERATED FROM PYTHON SOURCE LINES 32-34

.. code-block:: Python

    rast.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:            5, 5
    Number of bands:      1
    Data types:           float64
    Coordinate system:    ['EPSG:4326']
    Nodata value:         255
    Pixel interpretation: None
    Pixel size:           0.3333333333333333, 0.3333333333333333
    Upper left corner:    0.0, 1.0
    Lower right corner:   1.6666666666666665, -0.6666666666666665





.. GENERATED FROM PYTHON SOURCE LINES 35-36

The array has been automatically cast into a :class:`~numpy.ma.MaskedArray`, to respect :class:`~geoutils.Raster.nodata` values.

.. GENERATED FROM PYTHON SOURCE LINES 36-38

.. code-block:: Python

    rast.data





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

 .. code-block:: none


    masked_array(
      data=[[0.30471707975443135, -1.0399841062404955, 0.7504511958064572,
             0.9405647163912139, -1.9510351886538364],
            [-1.302179506862318, 0.12784040316728537, -0.3162425923435822,
             -0.016801157504288795, -0.85304392757358],
            [0.8793979748628286, 0.7777919354289483, --, 1.1272412069680329,
             0.4675093422520456],
            [-0.8592924628832382, 0.36875078408249884, -0.9588826008289989,
             0.8784503013072725, -0.049925910986252896],
            [-0.18486236354526056, -0.6809295444039414, 1.2225413386740303,
             -0.15452948206880215, -0.4283278221631072]],
      mask=[[False, False, False, False, False],
            [False, False, False, False, False],
            [False, False,  True, False, False],
            [False, False, False, False, False],
            [False, False, False, False, False]],
      fill_value=255.0)



.. GENERATED FROM PYTHON SOURCE LINES 39-40

We could also have created directly from a :class:`~numpy.ma.MaskedArray`.

.. GENERATED FROM PYTHON SOURCE LINES 40-49

.. code-block:: Python


    # A random mask, that will mask one out of two values on average
    mask = rng.integers(0, 2, size=(5, 5), dtype="bool")
    ma = np.ma.masked_array(data=arr, mask=mask)

    # This time, we pass directly the masked array
    rast = gu.Raster.from_array(data=ma, transform=transform, crs=crs, nodata=255)
    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>[[0.30471707975443135 -1.0399841062404955 0.7504511958064572
             0.9405647163912139 --]
            [-1.302179506862318 0.12784040316728537 -0.3162425923435822 --
             -0.85304392757358]
            [-- 0.7777919354289483 -- 1.1272412069680329 --]
            [-- 0.36875078408249884 -0.9588826008289989 0.8784503013072725 --]
            [-0.18486236354526056 -0.6809295444039414 1.2225413386740303 --
             -0.4283278221631072]]
      <b>transform=</b>| 0.33, 0.00, 0.00|
                | 0.00,-0.33, 1.00|
                | 0.00, 0.00, 1.00|
      <b>crs=</b>EPSG:4326
      <b>nodata=</b>255)</span></pre>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 50-52

The different functionalities of GeoUtils will respect :class:`~geoutils.Raster.nodata` values, starting with :func:`~geoutils.Raster.plot`,
which will ignore them during plotting (transparent).

.. GENERATED FROM PYTHON SOURCE LINES 52-53

.. code-block:: Python

    rast.plot(cmap="copper")



.. image-sg:: /io_examples/import_export/images/sphx_glr_raster_from_array_001.png
   :alt: raster from array
   :srcset: /io_examples/import_export/images/sphx_glr_raster_from_array_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_io_examples_import_export_raster_from_array.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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