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

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

.. _sphx_glr_handling_examples_raster_vector_rasterize.py:


Rasterize a vector
==================

This example demonstrates the rasterizing of a vector using :func:`geoutils.Vector.rasterize`.

.. 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_rasterize_001.png
   :alt: rasterize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_rasterize_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 rasterize 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.rasterize(rast)
    vect_rasterized.plot(ax="new", cmap="viridis")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_rasterize_002.png
   :alt: rasterize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_rasterize_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 32-36

By default, :func:`~geoutils.Vector.rasterize` will burn the index of the :class:`~geoutils.Vector`'s features in their geometry. We can specify the ``in_value`` to burn a
single value, or any iterable with the same length as there are features in the :class:`~geoutils.Vector`. An ``out_value`` can be passed to burn
outside the geometries.


.. GENERATED FROM PYTHON SOURCE LINES 36-40

.. code-block:: Python


    vect_rasterized = vect.rasterize(rast, in_value=1)
    vect_rasterized.plot(ax="new")




.. image-sg:: /handling_examples/raster_vector/images/sphx_glr_rasterize_003.png
   :alt: rasterize
   :srcset: /handling_examples/raster_vector/images/sphx_glr_rasterize_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 41-44

.. note::
        If the rasterized ``in_value`` is fixed to 1 and ``out_value`` to 0 (default), then :func:`~geoutils.Vector.rasterize` is creating a boolean mask.
        This is equivalent to using :func:`~geoutils.Vector.create_mask`, and will return a boolean :class:`~geoutils.Raster`.

.. GENERATED FROM PYTHON SOURCE LINES 45-48

.. 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>[[1 1 1 ... 1 1 1]
            [1 1 1 ... 1 1 1]
            [1 1 1 ... 1 1 1]
            ...
            [0 0 0 ... 0 1 1]
            [0 0 0 ... 0 1 1]
            [0 0 0 ... 1 1 1]]
      <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 49-51

**Second option:** we can pass any georeferencing parameter to :func:`~geoutils.Raster.rasterize`. 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 51-56

.. code-block:: Python



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








.. GENERATED FROM PYTHON SOURCE LINES 57-61

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


.. _sphx_glr_download_handling_examples_raster_vector_rasterize.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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