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

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

.. _sphx_glr_handling_examples_raster_vector_create_mask.py:


Mask from a vector
==================

This example demonstrates the creation of a mask from a vector using :func:`geoutils.Vector.create_mask`.

.. GENERATED FROM PYTHON SOURCE LINES 9-10

We open a raster and vector.

.. GENERATED FROM PYTHON SOURCE LINES 10-18

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








.. GENERATED FROM PYTHON SOURCE LINES 20-21

Let's plot the raster and vector.

.. GENERATED FROM PYTHON SOURCE LINES 21-24

.. code-block:: Python

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




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_create_mask_001.png
   :alt: create mask
   :srcset: /handling_examples/raster_vector/images/sphx_glr_create_mask_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 25-27

**First option:** using the raster as a reference to match, we create a mask for the vector in any projection and georeferenced grid. We simply have to pass
the :class:`~geoutils.Raster` as single argument to :func:`~geoutils.Vector.rasterize`. See :ref:`core-match-ref` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 27-31

.. code-block:: Python


    vect_rasterized = vect.create_mask(rast)
    vect_rasterized.plot(ax="new")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_create_mask_002.png
   :alt: create mask
   :srcset: /handling_examples/raster_vector/images/sphx_glr_create_mask_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 32-35

.. note::
        This is equivalent to using :func:`~geoutils.Vector.rasterize` with ``in_value=1`` and ``out_value=0`` and
        will return a boolean :class:`~geoutils.Raster`.

.. GENERATED FROM PYTHON SOURCE LINES 35-38

.. code-block:: Python


    vect_rasterized






.. 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>[[ True  True  True ...  True  True  True]
            [ True  True  True ...  True  True  True]
            [ True  True  True ...  True  True  True]
            ...
            [False False False ... False  True  True]
            [False False False ... False  True  True]
            [False False False ...  True  True  True]]
      <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 39-41

**Second option:** we can pass any georeferencing parameter to :func:`~geoutils.Raster.create_mask`. Any unpassed attribute will be deduced from the
:class:`~geoutils.Vector` itself, except from the :attr:`~geoutils.Raster.shape` to rasterize that will default to 1000 x 1000.

.. GENERATED FROM PYTHON SOURCE LINES 41-46

.. code-block:: Python



    # vect_rasterized = vect.create_mask(xres=500)
    # vect_rasterized.plot()








.. GENERATED FROM PYTHON SOURCE LINES 47-51

.. important::
     The :attr:`~geoutils.Raster.shape` or the :attr:`~geoutils.Raster.res` are the only unknown arguments to rasterize a :class:`~geoutils.Vector`,
     one or the other can be passed.



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

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


.. _sphx_glr_download_handling_examples_raster_vector_create_mask.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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