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

.. only:: html

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

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

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

.. _sphx_glr_examples_spatial_indexing_1d_network.py:


Spatial indexing of 1D networks and linear geometry
===================================================

This example demonstrates how to use the ``numba_celltree`` package to index 1D
grids. The package provides a :class:`EdgeCellTree` class that constructs a
cell tree for 1D networks and linear geometries. The package currently supports
the following operations:

* Locating points
* Locating line segments

This example provides an introduction to searching a cell tree for each of
these.

We'll start by importing the required packages with matplotlib for plotting.

.. GENERATED FROM PYTHON SOURCE LINES 18-28

.. code-block:: Python


    import os

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.collections import LineCollection

    os.environ["NUMBA_DISABLE_JIT"] = "1"  # small examples, avoid JIT overhead
    from numba_celltree import EdgeCellTree2d, demo  # noqa E402








.. GENERATED FROM PYTHON SOURCE LINES 29-30

Let's start with a simple 1D network.

.. GENERATED FROM PYTHON SOURCE LINES 30-39

.. code-block:: Python


    vertices, edges = demo.example_1d_network()

    node_x = vertices.T[0]
    node_y = vertices.T[1]

    fig, ax = plt.subplots()
    demo.plot_edges(node_x, node_y, edges, ax, color="black")




.. image-sg:: /examples/images/sphx_glr_spatial_indexing_1d_network_001.png
   :alt: spatial indexing 1d network
   :srcset: /examples/images/sphx_glr_spatial_indexing_1d_network_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-44

Locating points
---------------

We'll build a cell tree first, then look for some points.

.. GENERATED FROM PYTHON SOURCE LINES 44-56

.. code-block:: Python


    tree = EdgeCellTree2d(vertices, edges)
    points = np.array(
        [
            [0.25, 1.5],
            [0.75, 1.5],
            [2.0, 1.5],  # This one is outside the grid
        ]
    )
    i = tree.locate_points(points)
    i





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

 .. code-block:: none


    array([ 7,  1, -1])



.. GENERATED FROM PYTHON SOURCE LINES 57-60

This returns the indices of the edges that contain each point, with -1
indicating points outside the network. We'll have to filter those out first.
Let's plot them:

.. GENERATED FROM PYTHON SOURCE LINES 60-69

.. code-block:: Python



    ii = i[i != -1]

    fig, ax = plt.subplots()
    ax.scatter(*points.transpose())
    demo.plot_edges(node_x, node_y, edges, ax, color="black")
    demo.plot_edges(node_x, node_y, edges[ii], ax, color="blue", linewidth=3)




.. image-sg:: /examples/images/sphx_glr_spatial_indexing_1d_network_002.png
   :alt: spatial indexing 1d network
   :srcset: /examples/images/sphx_glr_spatial_indexing_1d_network_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 70-75

Locating line segments
-----------------------

Let's locate some line segments on the grid. We'll start off with creating
some line segments.

.. GENERATED FROM PYTHON SOURCE LINES 75-87

.. code-block:: Python


    segments = np.array(
        [
            [[0.0, 1.25], [1.5, 1.5]],
            [[1.5, 1.5], [2.25, 3.5]],
        ]
    )

    fig, ax = plt.subplots()
    demo.plot_edges(node_x, node_y, edges, ax, color="black")
    ax.add_collection(LineCollection(segments, color="gray", linewidth=3))




.. image-sg:: /examples/images/sphx_glr_spatial_indexing_1d_network_003.png
   :alt: spatial indexing 1d network
   :srcset: /examples/images/sphx_glr_spatial_indexing_1d_network_003.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.LineCollection object at 0x7fd8c03bacf0>



.. GENERATED FROM PYTHON SOURCE LINES 88-89

Let's now intersect these line segments with the edges in the network.

.. GENERATED FROM PYTHON SOURCE LINES 89-92

.. code-block:: Python

    segment_index, tree_edge_index, xy_intersection = tree.intersect_edges(segments)
    xy_intersection





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

 .. code-block:: none


    array([[0.6       , 1.35      ],
           [0.25      , 1.29166667],
           [2.02173913, 2.89130435],
           [1.875     , 2.5       ]])



.. GENERATED FROM PYTHON SOURCE LINES 93-98

The ``intersect_edges`` method returns three arrays: which input segments
intersect with the network, which network edges they intersect with, and the
coordinates of each intersection point.

Let's plot the results:

.. GENERATED FROM PYTHON SOURCE LINES 98-105

.. code-block:: Python


    fig, ax = plt.subplots()
    demo.plot_edges(node_x, node_y, edges, ax, color="black")
    demo.plot_edges(node_x, node_y, edges[tree_edge_index], ax, color="blue", linewidth=3)
    ax.add_collection(LineCollection(segments, color="gray", linewidth=3))
    ax.scatter(*xy_intersection.transpose(), s=60, color="darkgreen", zorder=2.5)




.. image-sg:: /examples/images/sphx_glr_spatial_indexing_1d_network_004.png
   :alt: spatial indexing 1d network
   :srcset: /examples/images/sphx_glr_spatial_indexing_1d_network_004.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.collections.PathCollection object at 0x7fd8c02806e0>




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

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


.. _sphx_glr_download_examples_spatial_indexing_1d_network.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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