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

.. only:: html

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

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

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

.. _sphx_glr_examples_vector_conversion.py:


Vector geometry conversion
==========================

A great deal of geospatial data is available not in gridded form, but in
vector form: as points, lines, and polygons. In the Python data ecosystem,
these geometries and their associated data are generally represented by a
geopandas GeoDataFrame.

Xugrid provides a number of utilities to use such data in combination with
unstructured grids. These are demonstrated below.

.. GENERATED FROM PYTHON SOURCE LINES 14-21

.. code-block:: Python


    import geopandas as gpd
    import matplotlib.pyplot as plt
    import pandas as pd

    import xugrid as xu








.. GENERATED FROM PYTHON SOURCE LINES 22-23

We'll once again use the surface elevation data example.

.. GENERATED FROM PYTHON SOURCE LINES 23-27

.. code-block:: Python


    uda = xu.data.elevation_nl()
    uda.ugrid.plot(vmin=-20, vmax=90, cmap="terrain")




.. image-sg:: /examples/images/sphx_glr_vector_conversion_001.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.PolyCollection object at 0x7f74dcdea780>



.. GENERATED FROM PYTHON SOURCE LINES 28-35

Conversion to GeoDataFrame
--------------------------

A UgridDataArray or UgridDataset can be directly converted to a GeoDataFrame,
provided it only contains a spatial dimension (and not a dimension such as
time). When calling
``.to_geodataframe``, a shapely Polygon is created for every face (cell).

.. GENERATED FROM PYTHON SOURCE LINES 35-39

.. code-block:: Python


    gdf = uda.ugrid.to_geodataframe()
    print(gdf)





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

 .. code-block:: none

                   elevation  ...                                           geometry
    mesh2d_nFaces             ...                                                   
    0               1.170000  ...  POLYGON ((21303.903 363737.469, 25340.195 3630...
    1               9.810000  ...  POLYGON ((185417.879 419222.681, 184927.836 41...
    2              54.040001  ...  POLYGON ((181811.627 338063.592, 182510.446 33...
    3               0.990000  ...  POLYGON ((20177.702 368500.703, 17210.674 3644...
    4               2.600000  ...  POLYGON ((51139.349 357499.291, 48079.937 3617...
    ...                  ...  ...                                                ...
    5243            0.930000  ...  POLYGON ((38115.173 400096.935, 34838.247 3952...
    5244           36.189999  ...  POLYGON ((252916.585 467082.681, 255888.448 45...
    5245            0.280000  ...  POLYGON ((34838.247 395258.331, 34522.298 3998...
    5246          -15.830000  ...  POLYGON ((32729.434 403338.332, 32167.16 39737...
    5247           -0.450000  ...  POLYGON ((29985.831 394355.22, 32167.16 397379...

    [5248 rows x 4 columns]




.. GENERATED FROM PYTHON SOURCE LINES 40-48

We see that a GeoDataFrame with 5248 rows is created: one row for each face.

Conversion from GeoDataFrame
----------------------------

We can also make the opposite conversion: we can create a UgridDataSet from a
GeoDataFrame.


.. GENERATED FROM PYTHON SOURCE LINES 48-51

.. code-block:: Python

    back = xu.UgridDataset.from_geodataframe(gdf)
    back






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
    <defs>
    <symbol id="icon-database" viewBox="0 0 32 32">
    <path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
    <path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    <path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    </symbol>
    <symbol id="icon-file-text2" viewBox="0 0 32 32">
    <path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
    <path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    </symbol>
    </defs>
    </svg>
    <style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
     *
     */

    :root {
      --xr-font-color0: var(
        --jp-content-font-color0,
        var(--pst-color-text-base rgba(0, 0, 0, 1))
      );
      --xr-font-color2: var(
        --jp-content-font-color2,
        var(--pst-color-text-base, rgba(0, 0, 0, 0.54))
      );
      --xr-font-color3: var(
        --jp-content-font-color3,
        var(--pst-color-text-base, rgba(0, 0, 0, 0.38))
      );
      --xr-border-color: var(
        --jp-border-color2,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 10))
      );
      --xr-disabled-color: var(
        --jp-layout-color3,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 40))
      );
      --xr-background-color: var(
        --jp-layout-color0,
        var(--pst-color-on-background, white)
      );
      --xr-background-color-row-even: var(
        --jp-layout-color1,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 5))
      );
      --xr-background-color-row-odd: var(
        --jp-layout-color2,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 15))
      );
    }

    html[theme="dark"],
    html[data-theme="dark"],
    body[data-theme="dark"],
    body.vscode-dark {
      --xr-font-color0: var(
        --jp-content-font-color0,
        var(--pst-color-text-base, rgba(255, 255, 255, 1))
      );
      --xr-font-color2: var(
        --jp-content-font-color2,
        var(--pst-color-text-base, rgba(255, 255, 255, 0.54))
      );
      --xr-font-color3: var(
        --jp-content-font-color3,
        var(--pst-color-text-base, rgba(255, 255, 255, 0.38))
      );
      --xr-border-color: var(
        --jp-border-color2,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 10))
      );
      --xr-disabled-color: var(
        --jp-layout-color3,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 40))
      );
      --xr-background-color: var(
        --jp-layout-color0,
        var(--pst-color-on-background, #111111)
      );
      --xr-background-color-row-even: var(
        --jp-layout-color1,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 5))
      );
      --xr-background-color-row-odd: var(
        --jp-layout-color2,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 15))
      );
    }

    .xr-wrap {
      display: block !important;
      min-width: 300px;
      max-width: 700px;
    }

    .xr-text-repr-fallback {
      /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
      display: none;
    }

    .xr-header {
      padding-top: 6px;
      padding-bottom: 6px;
      margin-bottom: 4px;
      border-bottom: solid 1px var(--xr-border-color);
    }

    .xr-header > div,
    .xr-header > ul {
      display: inline;
      margin-top: 0;
      margin-bottom: 0;
    }

    .xr-obj-type,
    .xr-array-name {
      margin-left: 2px;
      margin-right: 10px;
    }

    .xr-obj-type {
      color: var(--xr-font-color2);
    }

    .xr-sections {
      padding-left: 0 !important;
      display: grid;
      grid-template-columns: 150px auto auto 1fr 0 20px 0 20px;
    }

    .xr-section-item {
      display: contents;
    }

    .xr-section-item input {
      display: inline-block;
      opacity: 0;
      height: 0;
    }

    .xr-section-item input + label {
      color: var(--xr-disabled-color);
      border: 2px solid transparent !important;
    }

    .xr-section-item input:enabled + label {
      cursor: pointer;
      color: var(--xr-font-color2);
    }

    .xr-section-item input:focus + label {
      border: 2px solid var(--xr-font-color0) !important;
    }

    .xr-section-item input:enabled + label:hover {
      color: var(--xr-font-color0);
    }

    .xr-section-summary {
      grid-column: 1;
      color: var(--xr-font-color2);
      font-weight: 500;
    }

    .xr-section-summary > span {
      display: inline-block;
      padding-left: 0.5em;
    }

    .xr-section-summary-in:disabled + label {
      color: var(--xr-font-color2);
    }

    .xr-section-summary-in + label:before {
      display: inline-block;
      content: "►";
      font-size: 11px;
      width: 15px;
      text-align: center;
    }

    .xr-section-summary-in:disabled + label:before {
      color: var(--xr-disabled-color);
    }

    .xr-section-summary-in:checked + label:before {
      content: "▼";
    }

    .xr-section-summary-in:checked + label > span {
      display: none;
    }

    .xr-section-summary,
    .xr-section-inline-details {
      padding-top: 4px;
      padding-bottom: 4px;
    }

    .xr-section-inline-details {
      grid-column: 2 / -1;
    }

    .xr-section-details {
      display: none;
      grid-column: 1 / -1;
      margin-bottom: 5px;
    }

    .xr-section-summary-in:checked ~ .xr-section-details {
      display: contents;
    }

    .xr-array-wrap {
      grid-column: 1 / -1;
      display: grid;
      grid-template-columns: 20px auto;
    }

    .xr-array-wrap > label {
      grid-column: 1;
      vertical-align: top;
    }

    .xr-preview {
      color: var(--xr-font-color3);
    }

    .xr-array-preview,
    .xr-array-data {
      padding: 0 5px !important;
      grid-column: 2;
    }

    .xr-array-data,
    .xr-array-in:checked ~ .xr-array-preview {
      display: none;
    }

    .xr-array-in:checked ~ .xr-array-data,
    .xr-array-preview {
      display: inline-block;
    }

    .xr-dim-list {
      display: inline-block !important;
      list-style: none;
      padding: 0 !important;
      margin: 0;
    }

    .xr-dim-list li {
      display: inline-block;
      padding: 0;
      margin: 0;
    }

    .xr-dim-list:before {
      content: "(";
    }

    .xr-dim-list:after {
      content: ")";
    }

    .xr-dim-list li:not(:last-child):after {
      content: ",";
      padding-right: 5px;
    }

    .xr-has-index {
      font-weight: bold;
    }

    .xr-var-list,
    .xr-var-item {
      display: contents;
    }

    .xr-var-item > div,
    .xr-var-item label,
    .xr-var-item > .xr-var-name span {
      background-color: var(--xr-background-color-row-even);
      border-color: var(--xr-background-color-row-odd);
      margin-bottom: 0;
      padding-top: 2px;
    }

    .xr-var-item > .xr-var-name:hover span {
      padding-right: 5px;
    }

    .xr-var-list > li:nth-child(odd) > div,
    .xr-var-list > li:nth-child(odd) > label,
    .xr-var-list > li:nth-child(odd) > .xr-var-name span {
      background-color: var(--xr-background-color-row-odd);
      border-color: var(--xr-background-color-row-even);
    }

    .xr-var-name {
      grid-column: 1;
    }

    .xr-var-dims {
      grid-column: 2;
    }

    .xr-var-dtype {
      grid-column: 3;
      text-align: right;
      color: var(--xr-font-color2);
    }

    .xr-var-preview {
      grid-column: 4;
    }

    .xr-index-preview {
      grid-column: 2 / 5;
      color: var(--xr-font-color2);
    }

    .xr-var-name,
    .xr-var-dims,
    .xr-var-dtype,
    .xr-preview,
    .xr-attrs dt {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 10px;
    }

    .xr-var-name:hover,
    .xr-var-dims:hover,
    .xr-var-dtype:hover,
    .xr-attrs dt:hover {
      overflow: visible;
      width: auto;
      z-index: 1;
    }

    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      display: none;
      border-top: 2px dotted var(--xr-background-color);
      padding-bottom: 20px !important;
      padding-top: 10px !important;
    }

    .xr-var-attrs-in + label,
    .xr-var-data-in + label,
    .xr-index-data-in + label {
      padding: 0 1px;
    }

    .xr-var-attrs-in:checked ~ .xr-var-attrs,
    .xr-var-data-in:checked ~ .xr-var-data,
    .xr-index-data-in:checked ~ .xr-index-data {
      display: block;
    }

    .xr-var-data > table {
      float: right;
    }

    .xr-var-data > pre,
    .xr-index-data > pre,
    .xr-var-data > table > tbody > tr {
      background-color: transparent !important;
    }

    .xr-var-name span,
    .xr-var-data,
    .xr-index-name div,
    .xr-index-data,
    .xr-attrs {
      padding-left: 25px !important;
    }

    .xr-attrs,
    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      grid-column: 1 / -1;
    }

    dl.xr-attrs {
      padding: 0;
      margin: 0;
      display: grid;
      grid-template-columns: 125px auto;
    }

    .xr-attrs dt,
    .xr-attrs dd {
      padding: 0;
      margin: 0;
      float: left;
      padding-right: 10px;
      width: auto;
    }

    .xr-attrs dt {
      font-weight: normal;
      grid-column: 1;
    }

    .xr-attrs dt:hover span {
      display: inline-block;
      background: var(--xr-background-color);
      padding-right: 10px;
    }

    .xr-attrs dd {
      grid-column: 2;
      white-space: pre-wrap;
      word-break: break-all;
    }

    .xr-icon-database,
    .xr-icon-file-text2,
    .xr-no-icon {
      display: inline-block;
      vertical-align: middle;
      width: 1em;
      height: 1.5em !important;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }

    .xr-var-attrs-in:checked + label > .xr-icon-file-text2,
    .xr-var-data-in:checked + label > .xr-icon-database,
    .xr-index-data-in:checked + label > .xr-icon-database {
      color: var(--xr-font-color0);
      filter: drop-shadow(1px 1px 5px var(--xr-font-color2));
      stroke-width: 0.8px;
    }
    </style><pre class='xr-text-repr-fallback'>&lt;xarray.Dataset&gt; Size: 147kB
    Dimensions:        (mesh2d_nFaces: 5248)
    Coordinates:
      * mesh2d_nFaces  (mesh2d_nFaces) int64 42kB 0 1 2 3 4 ... 5244 5245 5246 5247
    Data variables:
        elevation      (mesh2d_nFaces) float32 21kB 1.17 9.81 54.04 ... -15.83 -0.45
        mesh2d_face_x  (mesh2d_nFaces) float64 42kB 2.388e+04 1.86e+05 ... 3.03e+04
        mesh2d_face_y  (mesh2d_nFaces) float64 42kB 3.648e+05 ... 3.964e+05</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.Dataset</div></div><ul class='xr-sections'><li class='xr-section-item'><input id='section-0c5922fd-21b7-4eaf-985b-4557f54ed901' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-0c5922fd-21b7-4eaf-985b-4557f54ed901' class='xr-section-summary'  title='Expand/collapse section'>Dimensions:</label><div class='xr-section-inline-details'><ul class='xr-dim-list'><li><span class='xr-has-index'>mesh2d_nFaces</span>: 5248</li></ul></div><div class='xr-section-details'></div></li><li class='xr-section-item'><input id='section-1d964a5f-f0f9-4e58-b97c-9f9da4a3cc61' class='xr-section-summary-in' type='checkbox'  checked><label for='section-1d964a5f-f0f9-4e58-b97c-9f9da4a3cc61' class='xr-section-summary' >Coordinates: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>mesh2d_nFaces</span></div><div class='xr-var-dims'>(mesh2d_nFaces)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 ... 5244 5245 5246 5247</div><input id='attrs-728cf877-6d17-43cb-bf41-563f8fab0b4b' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-728cf877-6d17-43cb-bf41-563f8fab0b4b' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-c97d38b1-ee83-40f0-ab98-eee5d84e205c' class='xr-var-data-in' type='checkbox'><label for='data-c97d38b1-ee83-40f0-ab98-eee5d84e205c' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([   0,    1,    2, ..., 5245, 5246, 5247], shape=(5248,))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-9828b563-1862-4dd2-a7a5-e544961f675e' class='xr-section-summary-in' type='checkbox'  checked><label for='section-9828b563-1862-4dd2-a7a5-e544961f675e' class='xr-section-summary' >Data variables: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span>elevation</span></div><div class='xr-var-dims'>(mesh2d_nFaces)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>1.17 9.81 54.04 ... -15.83 -0.45</div><input id='attrs-3c52de88-4dba-4abc-ae6f-7e59cc82c4cf' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-3c52de88-4dba-4abc-ae6f-7e59cc82c4cf' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-c311c709-e3f5-4d4f-8a14-6505d91b6b65' class='xr-var-data-in' type='checkbox'><label for='data-c311c709-e3f5-4d4f-8a14-6505d91b6b65' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([  1.17,   9.81,  54.04, ...,   0.28, -15.83,  -0.45],
          shape=(5248,), dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>mesh2d_face_x</span></div><div class='xr-var-dims'>(mesh2d_nFaces)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>2.388e+04 1.86e+05 ... 3.03e+04</div><input id='attrs-04cbb039-6936-432b-a264-8656ce8961c4' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-04cbb039-6936-432b-a264-8656ce8961c4' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-1215950f-826a-4051-8d7b-977c141b1d68' class='xr-var-data-in' type='checkbox'><label for='data-1215950f-826a-4051-8d7b-977c141b1d68' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 23882.79376058, 186048.98609163, 183280.61324667, ...,
            33842.56847139,  33139.63056206,  30303.5164253 ], shape=(5248,))</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>mesh2d_face_y</span></div><div class='xr-var-dims'>(mesh2d_nFaces)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>3.648e+05 4.171e+05 ... 3.964e+05</div><input id='attrs-40212de6-9ef3-447f-a8ce-6d1d268a2428' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-40212de6-9ef3-447f-a8ce-6d1d268a2428' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-e62875d0-64a0-49fe-af2b-61106bd21dc5' class='xr-var-data-in' type='checkbox'><label for='data-e62875d0-64a0-49fe-af2b-61106bd21dc5' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([364821.96725663, 417102.96121876, 334623.01878379, ...,
           397494.51640391, 400187.85011645, 396399.29036318], shape=(5248,))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-51ed9f6a-8f97-49ca-b0d4-fd3fc846171a' class='xr-section-summary-in' type='checkbox'  ><label for='section-51ed9f6a-8f97-49ca-b0d4-fd3fc846171a' class='xr-section-summary' >Indexes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>mesh2d_nFaces</div></div><div class='xr-index-preview'>PandasIndex</div><input type='checkbox' disabled/><label></label><input id='index-894fc8cf-4023-49a5-92ef-39887e288e76' class='xr-index-data-in' type='checkbox'/><label for='index-894fc8cf-4023-49a5-92ef-39887e288e76' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(RangeIndex(start=0, stop=5248, step=1, name=&#x27;mesh2d_nFaces&#x27;))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-bf92da68-6161-44cd-b502-2604f489329a' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-bf92da68-6161-44cd-b502-2604f489329a' class='xr-section-summary'  title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 52-82

.. note::
  Not every GeoDataFrame can be converted to a ``xugrid`` representation!
  While an unstructured grid topology is generally always a valid collection
  of polygon geometries, not every collection of polygon geometries is a
  valid grid: polygons should be convex and non-overlapping to create a valid
  unstructured grid.

  Secondly, each polygon fully owns its vertices (nodes), while the face of a
  UGRID topology shares its nodes with its neighbors. All the vertices of the
  polygons must therefore be exactly snapped together to form a connected
  mesh.

  Hence, the ``.from_geodataframe()`` is primarily meant to create ``xugrid``
  objects from data that were originally created as triangulation or
  unstructured grid, but that were converted to vector geometry form.

"Rasterizing", or "burning" vector geometries
---------------------------------------------

Rasterizing is a common operation when working with raster and vector data.
While we cannot name the operation "rasterizing" when we're dealing with
unstructured grids, there is a clearly equivalent operation where we mark
cells that are covered or touched by a polygon.

In this example, we mark the faces that are covered by a certain province.

We start by re-projecting the provinces dataset to the coordinate reference
system (CRS), from WGS84 (EPSG:4326) to the Dutch National coordinate system
(RD New, EPSG: 28992). Then, we give each province a unique id, which we
burn into the grid.

.. GENERATED FROM PYTHON SOURCE LINES 82-88

.. code-block:: Python


    provinces = xu.data.provinces_nl().to_crs(28992)
    provinces["id"] = range(len(provinces))
    burned = xu.burn_vector_geometry(provinces, uda, column="id")
    burned.ugrid.plot()




.. image-sg:: /examples/images/sphx_glr_vector_conversion_002.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.PolyCollection object at 0x7f74dcf1e4b0>



.. GENERATED FROM PYTHON SOURCE LINES 89-91

This makes it very easy to classify and group data. Let's say
we want to compute the average surface elevation per province:

.. GENERATED FROM PYTHON SOURCE LINES 91-96

.. code-block:: Python


    burned = xu.burn_vector_geometry(provinces, uda, column="id")
    uda.groupby(burned).mean()







.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
    <defs>
    <symbol id="icon-database" viewBox="0 0 32 32">
    <path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
    <path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    <path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    </symbol>
    <symbol id="icon-file-text2" viewBox="0 0 32 32">
    <path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
    <path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    </symbol>
    </defs>
    </svg>
    <style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
     *
     */

    :root {
      --xr-font-color0: var(
        --jp-content-font-color0,
        var(--pst-color-text-base rgba(0, 0, 0, 1))
      );
      --xr-font-color2: var(
        --jp-content-font-color2,
        var(--pst-color-text-base, rgba(0, 0, 0, 0.54))
      );
      --xr-font-color3: var(
        --jp-content-font-color3,
        var(--pst-color-text-base, rgba(0, 0, 0, 0.38))
      );
      --xr-border-color: var(
        --jp-border-color2,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 10))
      );
      --xr-disabled-color: var(
        --jp-layout-color3,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 40))
      );
      --xr-background-color: var(
        --jp-layout-color0,
        var(--pst-color-on-background, white)
      );
      --xr-background-color-row-even: var(
        --jp-layout-color1,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 5))
      );
      --xr-background-color-row-odd: var(
        --jp-layout-color2,
        hsl(from var(--pst-color-on-background, white) h s calc(l - 15))
      );
    }

    html[theme="dark"],
    html[data-theme="dark"],
    body[data-theme="dark"],
    body.vscode-dark {
      --xr-font-color0: var(
        --jp-content-font-color0,
        var(--pst-color-text-base, rgba(255, 255, 255, 1))
      );
      --xr-font-color2: var(
        --jp-content-font-color2,
        var(--pst-color-text-base, rgba(255, 255, 255, 0.54))
      );
      --xr-font-color3: var(
        --jp-content-font-color3,
        var(--pst-color-text-base, rgba(255, 255, 255, 0.38))
      );
      --xr-border-color: var(
        --jp-border-color2,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 10))
      );
      --xr-disabled-color: var(
        --jp-layout-color3,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 40))
      );
      --xr-background-color: var(
        --jp-layout-color0,
        var(--pst-color-on-background, #111111)
      );
      --xr-background-color-row-even: var(
        --jp-layout-color1,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 5))
      );
      --xr-background-color-row-odd: var(
        --jp-layout-color2,
        hsl(from var(--pst-color-on-background, #111111) h s calc(l + 15))
      );
    }

    .xr-wrap {
      display: block !important;
      min-width: 300px;
      max-width: 700px;
    }

    .xr-text-repr-fallback {
      /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
      display: none;
    }

    .xr-header {
      padding-top: 6px;
      padding-bottom: 6px;
      margin-bottom: 4px;
      border-bottom: solid 1px var(--xr-border-color);
    }

    .xr-header > div,
    .xr-header > ul {
      display: inline;
      margin-top: 0;
      margin-bottom: 0;
    }

    .xr-obj-type,
    .xr-array-name {
      margin-left: 2px;
      margin-right: 10px;
    }

    .xr-obj-type {
      color: var(--xr-font-color2);
    }

    .xr-sections {
      padding-left: 0 !important;
      display: grid;
      grid-template-columns: 150px auto auto 1fr 0 20px 0 20px;
    }

    .xr-section-item {
      display: contents;
    }

    .xr-section-item input {
      display: inline-block;
      opacity: 0;
      height: 0;
    }

    .xr-section-item input + label {
      color: var(--xr-disabled-color);
      border: 2px solid transparent !important;
    }

    .xr-section-item input:enabled + label {
      cursor: pointer;
      color: var(--xr-font-color2);
    }

    .xr-section-item input:focus + label {
      border: 2px solid var(--xr-font-color0) !important;
    }

    .xr-section-item input:enabled + label:hover {
      color: var(--xr-font-color0);
    }

    .xr-section-summary {
      grid-column: 1;
      color: var(--xr-font-color2);
      font-weight: 500;
    }

    .xr-section-summary > span {
      display: inline-block;
      padding-left: 0.5em;
    }

    .xr-section-summary-in:disabled + label {
      color: var(--xr-font-color2);
    }

    .xr-section-summary-in + label:before {
      display: inline-block;
      content: "►";
      font-size: 11px;
      width: 15px;
      text-align: center;
    }

    .xr-section-summary-in:disabled + label:before {
      color: var(--xr-disabled-color);
    }

    .xr-section-summary-in:checked + label:before {
      content: "▼";
    }

    .xr-section-summary-in:checked + label > span {
      display: none;
    }

    .xr-section-summary,
    .xr-section-inline-details {
      padding-top: 4px;
      padding-bottom: 4px;
    }

    .xr-section-inline-details {
      grid-column: 2 / -1;
    }

    .xr-section-details {
      display: none;
      grid-column: 1 / -1;
      margin-bottom: 5px;
    }

    .xr-section-summary-in:checked ~ .xr-section-details {
      display: contents;
    }

    .xr-array-wrap {
      grid-column: 1 / -1;
      display: grid;
      grid-template-columns: 20px auto;
    }

    .xr-array-wrap > label {
      grid-column: 1;
      vertical-align: top;
    }

    .xr-preview {
      color: var(--xr-font-color3);
    }

    .xr-array-preview,
    .xr-array-data {
      padding: 0 5px !important;
      grid-column: 2;
    }

    .xr-array-data,
    .xr-array-in:checked ~ .xr-array-preview {
      display: none;
    }

    .xr-array-in:checked ~ .xr-array-data,
    .xr-array-preview {
      display: inline-block;
    }

    .xr-dim-list {
      display: inline-block !important;
      list-style: none;
      padding: 0 !important;
      margin: 0;
    }

    .xr-dim-list li {
      display: inline-block;
      padding: 0;
      margin: 0;
    }

    .xr-dim-list:before {
      content: "(";
    }

    .xr-dim-list:after {
      content: ")";
    }

    .xr-dim-list li:not(:last-child):after {
      content: ",";
      padding-right: 5px;
    }

    .xr-has-index {
      font-weight: bold;
    }

    .xr-var-list,
    .xr-var-item {
      display: contents;
    }

    .xr-var-item > div,
    .xr-var-item label,
    .xr-var-item > .xr-var-name span {
      background-color: var(--xr-background-color-row-even);
      border-color: var(--xr-background-color-row-odd);
      margin-bottom: 0;
      padding-top: 2px;
    }

    .xr-var-item > .xr-var-name:hover span {
      padding-right: 5px;
    }

    .xr-var-list > li:nth-child(odd) > div,
    .xr-var-list > li:nth-child(odd) > label,
    .xr-var-list > li:nth-child(odd) > .xr-var-name span {
      background-color: var(--xr-background-color-row-odd);
      border-color: var(--xr-background-color-row-even);
    }

    .xr-var-name {
      grid-column: 1;
    }

    .xr-var-dims {
      grid-column: 2;
    }

    .xr-var-dtype {
      grid-column: 3;
      text-align: right;
      color: var(--xr-font-color2);
    }

    .xr-var-preview {
      grid-column: 4;
    }

    .xr-index-preview {
      grid-column: 2 / 5;
      color: var(--xr-font-color2);
    }

    .xr-var-name,
    .xr-var-dims,
    .xr-var-dtype,
    .xr-preview,
    .xr-attrs dt {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 10px;
    }

    .xr-var-name:hover,
    .xr-var-dims:hover,
    .xr-var-dtype:hover,
    .xr-attrs dt:hover {
      overflow: visible;
      width: auto;
      z-index: 1;
    }

    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      display: none;
      border-top: 2px dotted var(--xr-background-color);
      padding-bottom: 20px !important;
      padding-top: 10px !important;
    }

    .xr-var-attrs-in + label,
    .xr-var-data-in + label,
    .xr-index-data-in + label {
      padding: 0 1px;
    }

    .xr-var-attrs-in:checked ~ .xr-var-attrs,
    .xr-var-data-in:checked ~ .xr-var-data,
    .xr-index-data-in:checked ~ .xr-index-data {
      display: block;
    }

    .xr-var-data > table {
      float: right;
    }

    .xr-var-data > pre,
    .xr-index-data > pre,
    .xr-var-data > table > tbody > tr {
      background-color: transparent !important;
    }

    .xr-var-name span,
    .xr-var-data,
    .xr-index-name div,
    .xr-index-data,
    .xr-attrs {
      padding-left: 25px !important;
    }

    .xr-attrs,
    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      grid-column: 1 / -1;
    }

    dl.xr-attrs {
      padding: 0;
      margin: 0;
      display: grid;
      grid-template-columns: 125px auto;
    }

    .xr-attrs dt,
    .xr-attrs dd {
      padding: 0;
      margin: 0;
      float: left;
      padding-right: 10px;
      width: auto;
    }

    .xr-attrs dt {
      font-weight: normal;
      grid-column: 1;
    }

    .xr-attrs dt:hover span {
      display: inline-block;
      background: var(--xr-background-color);
      padding-right: 10px;
    }

    .xr-attrs dd {
      grid-column: 2;
      white-space: pre-wrap;
      word-break: break-all;
    }

    .xr-icon-database,
    .xr-icon-file-text2,
    .xr-no-icon {
      display: inline-block;
      vertical-align: middle;
      width: 1em;
      height: 1.5em !important;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }

    .xr-var-attrs-in:checked + label > .xr-icon-file-text2,
    .xr-var-data-in:checked + label > .xr-icon-database,
    .xr-index-data-in:checked + label > .xr-icon-database {
      color: var(--xr-font-color0);
      filter: drop-shadow(1px 1px 5px var(--xr-font-color2));
      stroke-width: 0.8px;
    }
    </style><pre class='xr-text-repr-fallback'>&lt;xarray.DataArray &#x27;elevation&#x27; (id: 12)&gt; Size: 48B
    array([10.725979  , -3.806918  , -0.46903867, 17.090816  ,  0.3133026 ,
           52.252567  , 12.194658  , -1.7175047 , 11.021334  ,  3.0442472 ,
           -1.4584134 , -0.9470762 ], dtype=float32)
    Coordinates:
      * id       (id) float64 96B 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0
    Attributes:
        unit:     m NAP</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'elevation'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>id</span>: 12</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-50ab4e76-fdb4-4e31-a024-df55e58f1c5c' class='xr-array-in' type='checkbox' checked><label for='section-50ab4e76-fdb4-4e31-a024-df55e58f1c5c' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>10.73 -3.807 -0.469 17.09 0.3133 ... -1.718 11.02 3.044 -1.458 -0.9471</span></div><div class='xr-array-data'><pre>array([10.725979  , -3.806918  , -0.46903867, 17.090816  ,  0.3133026 ,
           52.252567  , 12.194658  , -1.7175047 , 11.021334  ,  3.0442472 ,
           -1.4584134 , -0.9470762 ], dtype=float32)</pre></div></div></li><li class='xr-section-item'><input id='section-eb4b525a-7f2d-4c71-a1c5-bf4e9fac5129' class='xr-section-summary-in' type='checkbox'  checked><label for='section-eb4b525a-7f2d-4c71-a1c5-bf4e9fac5129' class='xr-section-summary' >Coordinates: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>id</span></div><div class='xr-var-dims'>(id)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>0.0 1.0 2.0 3.0 ... 9.0 10.0 11.0</div><input id='attrs-3765469a-ef0f-46f9-87eb-3c91d20bbb3d' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-3765469a-ef0f-46f9-87eb-3c91d20bbb3d' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-3b7eb359-4210-4877-97b3-cc712da3a6e6' class='xr-var-data-in' type='checkbox'><label for='data-3b7eb359-4210-4877-97b3-cc712da3a6e6' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-ddeef982-29b8-4cab-8db8-5509fdceb3b8' class='xr-section-summary-in' type='checkbox'  ><label for='section-ddeef982-29b8-4cab-8db8-5509fdceb3b8' class='xr-section-summary' >Indexes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>id</div></div><div class='xr-index-preview'>PandasIndex</div><input type='checkbox' disabled/><label></label><input id='index-699cabbd-ac4d-487e-9154-96958220e8dd' class='xr-index-data-in' type='checkbox'/><label for='index-699cabbd-ac4d-487e-9154-96958220e8dd' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(Index([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0], dtype=&#x27;float64&#x27;, name=&#x27;id&#x27;))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-633bc3aa-25ab-43d0-b53b-250cbc4abd5e' class='xr-section-summary-in' type='checkbox'  checked><label for='section-633bc3aa-25ab-43d0-b53b-250cbc4abd5e' class='xr-section-summary' >Attributes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>unit :</span></dt><dd>m NAP</dd></dl></div></li></ul></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 97-98

This is a convenient way to create masks for specific regions:

.. GENERATED FROM PYTHON SOURCE LINES 98-110

.. code-block:: Python


    utrecht = provinces[provinces["name"] == "Utrecht"]
    burned = xu.burn_vector_geometry(utrecht, uda)
    xmin, ymin, xmax, ymax = utrecht.buffer(10_000).total_bounds

    fig, ax = plt.subplots()
    burned.ugrid.plot(ax=ax)
    burned.ugrid.plot.line(ax=ax, edgecolor="black", linewidth=0.5)
    utrecht.plot(ax=ax, edgecolor="red", facecolor="none", linewidth=1.5)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)




.. image-sg:: /examples/images/sphx_glr_vector_conversion_003.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_003.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (428343.70545104187, 489587.5779869387)



.. GENERATED FROM PYTHON SOURCE LINES 111-114

By default, ``burn_vector_geometry`` will only include grid faces whose
centroid are located in a polygon. We can also mark all intersected faces
by setting ``all_touched=True``:

.. GENERATED FROM PYTHON SOURCE LINES 114-124

.. code-block:: Python


    burned = xu.burn_vector_geometry(utrecht, uda, all_touched=True)

    fig, ax = plt.subplots()
    burned.ugrid.plot(ax=ax)
    burned.ugrid.plot.line(ax=ax, edgecolor="black", linewidth=0.5)
    utrecht.plot(ax=ax, edgecolor="red", facecolor="none", linewidth=1.5)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)




.. image-sg:: /examples/images/sphx_glr_vector_conversion_004.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_004.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (428343.70545104187, 489587.5779869387)



.. GENERATED FROM PYTHON SOURCE LINES 125-126

We can also use such "masks" to e.g. modify specific parts of the grid data:

.. GENERATED FROM PYTHON SOURCE LINES 126-130

.. code-block:: Python


    modified = (uda + 50.0).where(burned == 1, other=uda)
    modified.ugrid.plot(vmin=-20, vmax=90, cmap="terrain")




.. image-sg:: /examples/images/sphx_glr_vector_conversion_005.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_005.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.PolyCollection object at 0x7f74d3325c70>



.. GENERATED FROM PYTHON SOURCE LINES 131-136

Note that ``all_touched=True`` is less suitable when differently valued
polygons are present that share borders. While the centroid of a face is
contained by only a single polygon, the area of the polygon may be located
in more than one polygon. In this case, the results of each polygon will
overwrite each other.

.. GENERATED FROM PYTHON SOURCE LINES 136-151

.. code-block:: Python


    by_centroid = xu.burn_vector_geometry(provinces, uda, column="id")
    by_touch = xu.burn_vector_geometry(provinces, uda, column="id", all_touched=True)

    fig, axes = plt.subplots(ncols=2, figsize=(10, 5))
    by_centroid.ugrid.plot(ax=axes[0], add_colorbar=False)
    by_touch.ugrid.plot(ax=axes[1], add_colorbar=False)

    for ax, title in zip(axes, ("centroid", "all touched")):
        burned.ugrid.plot.line(ax=ax, edgecolor="black", linewidth=0.5)
        utrecht.plot(ax=ax, edgecolor="red", facecolor="none", linewidth=1.5)
        ax.set_xlim(xmin, xmax)
        ax.set_ylim(ymin, ymax)
        ax.set_title(title)




.. image-sg:: /examples/images/sphx_glr_vector_conversion_006.png
   :alt: centroid, all touched
   :srcset: /examples/images/sphx_glr_vector_conversion_006.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 152-157

This function can also be used to burn points or lines into the faces of an
unstructured grid.

The exterior boundaries of the province polygons will provide
a collection of linestrings that we can burn into the grid:

.. GENERATED FROM PYTHON SOURCE LINES 157-168

.. code-block:: Python


    lines = gpd.GeoDataFrame(geometry=provinces.exterior)
    burned = xu.burn_vector_geometry(lines, uda)

    fig, ax = plt.subplots()
    burned.ugrid.plot(ax=ax)
    burned.ugrid.plot.line(ax=ax, edgecolor="black", linewidth=0.5)
    provinces.plot(ax=ax, edgecolor="red", facecolor="none", linewidth=1.5)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)




.. image-sg:: /examples/images/sphx_glr_vector_conversion_007.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_007.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (428343.70545104187, 489587.5779869387)



.. GENERATED FROM PYTHON SOURCE LINES 169-170

We can also burn points.

.. GENERATED FROM PYTHON SOURCE LINES 170-178

.. code-block:: Python


    province_centroids = gpd.GeoDataFrame(geometry=provinces.centroid)
    burned = xu.burn_vector_geometry(province_centroids, uda)

    fig, ax = plt.subplots()
    burned.ugrid.plot(ax=ax)
    provinces.plot(ax=ax, edgecolor="red", facecolor="none")




.. image-sg:: /examples/images/sphx_glr_vector_conversion_008.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_008.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <Axes: xlabel='x', ylabel='y'>



.. GENERATED FROM PYTHON SOURCE LINES 179-181

Finally, it's also possible to combine multiple geometry types in a single
burn operation.

.. GENERATED FROM PYTHON SOURCE LINES 181-186

.. code-block:: Python


    combined = pd.concat([lines, province_centroids])
    burned = xu.burn_vector_geometry(combined, uda)
    burned.ugrid.plot()




.. image-sg:: /examples/images/sphx_glr_vector_conversion_009.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_009.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.PolyCollection object at 0x7f74cfc5da30>



.. GENERATED FROM PYTHON SOURCE LINES 187-193

Polygonizing
------------

We can also do the opposite operation: turn collections of same-valued grid
faces into vector polygons. Let's classify the elevation data into below and
above the boundary of 5 m above mean sea level:

.. GENERATED FROM PYTHON SOURCE LINES 193-198

.. code-block:: Python


    classified = uda > 5
    polygonized = xu.polygonize(classified)
    polygonized.plot(facecolor="none")




.. image-sg:: /examples/images/sphx_glr_vector_conversion_010.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_010.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <Axes: >



.. GENERATED FROM PYTHON SOURCE LINES 199-217

We see that the results consists of two large polygons, in which the
triangles of the triangular grid have been merged to form a single polygon,
and many smaller polygons, some of which correspond one to one to the
triangles of the grid.

.. note::
  The produced polygon edges will follow exactly the face boundaries. When
  the data consists of many unique values (e.g. unbinned elevation data), the
  result will essentially be one polygon per face. In such cases, it is more
  efficient to use ``xugrid.UgridDataArray.to_geodataframe``, which directly
  converts every face to a polygon.

Snap to grid
------------

The examples above deal with data on the faces of the grid. However, data can
also be associated with the nodes or edges of the grid. For example, we can
snap line data to exactly follow the edges (the face boundaries):

.. GENERATED FROM PYTHON SOURCE LINES 217-228

.. code-block:: Python


    linestrings = gpd.GeoDataFrame(geometry=utrecht.exterior)
    snapped_uds, snapped_gdf = xu.snap_to_grid(linestrings, uda, max_snap_distance=1.0)

    fig, ax = plt.subplots()
    snapped_gdf.plot(ax=ax)
    snapped_uds.ugrid.grid.plot(ax=ax, edgecolor="gray", linewidth=0.5)
    utrecht.plot(ax=ax, edgecolor="red", facecolor="none", linewidth=0.75)
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)




.. image-sg:: /examples/images/sphx_glr_vector_conversion_011.png
   :alt: vector conversion
   :srcset: /examples/images/sphx_glr_vector_conversion_011.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    (428343.70545104187, 489587.5779869387)



.. GENERATED FROM PYTHON SOURCE LINES 229-238

There are (arguably) many ways in which a linestring could be snapped to
edges. The function above uses the criterion the following criterion: if a
part of the linestring is located between the centroid of a face and the
centroid of an edge, it is snapped to that edge.

This sometimes result in less (visually) pleasing results, such as the two
triangles in the lower left corner which are surrounded by the snapped edges.
In general, results are best when the line segments are relatively large
compare to the grid faces.


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

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


.. _sphx_glr_download_examples_vector_conversion.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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