<commit_before>Network analysis in Python
==========================

Finding a shortest path using a specific street network is a common GIS problem that has many practical
applications. For example navigators are one of those "every-day" applications where **routing** using specific algorithms is used
to find the optimal route between two (or multiple) points.

It is also possible to perform network analysis such as tranposrtation routing in Python.
`Networkx <https://networkx.github.io/documentation/stable/>`__ is a Python module that provides
a lot tools that can be used to analyze networks on various different ways. It also contains algorithms
such as `Dijkstras algorithm <https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.weighted.single_source_dijkstra.html#networkx.algorithms.shortest_paths.weighted.single_source_dijkstra>`__ or
`A* <https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.astar.astar_path.html#networkx.algorithms.shortest_paths.astar.astar_path>`__ algoritm that are commonly used to find shortest paths along transportation network.

To be able to conduct network analysis, it is, of course, necessary to have a network that is used for the analyses.
`Osmnx <https://github.com/gboeing/osmnx>`__ package that we just explored in previous tutorial, makes it really easy to
retrieve routable networks from OpenStreetMap with different transport modes (walking, cycling and driving). Osmnx also
combines some functionalities from ``networkx`` module to make it straightforward to conduct routing along OpenStreetMap data.

Next we will test the routing functionalities of osmnx by finding a shortest path between two points based on drivable roads.

Let's first download the OSM data from Kamppi but this time include only such street segments that are walkable.
In omsnx it is possible to retrieve only such streets that are drivable by specifying ``'drive'`` into ``network_type`` parameter that can be used to
specify what kind of streets are retrieved from OpenStreetMap (other possibilities are ``walk`` and ``bike``).

.. ipython:: python

    import osmnx as ox
    import networkx as nx
    import geopandas as gpd
    import matplotlib.pyplot as plt
    import pandas as pd

    place_name = "Kamppi, Helsinki, Finland"
    graph = ox.graph_from_place(place_name, network_type='drive')
    @savefig street_network.png width=7in
    fig, ax = ox.plot_graph(graph)

Okey so now we have retrieved only such streets where it is possible to drive with a car. Let's confirm
this by taking a look at the attributes of the street network. Easiest way to do this is to convert the
graph (nodes and edges) into GeoDataFrames.

.. ipython:: python

    edges = ox.graph_to_gdfs(graph, nodes=False, edges=True)

- Let's check what columns do we have in our data

.. ipython:: python

    edges.columns

Okey, so we have quite many columns in our GeoDataFrame. Most of the columns are fairly self-exploratory but the following table describes all of them.

+----------------------------------------------------------------+----------------------------------------+------------------------+
| Column                                                         | Description                            | Data type              |
+================================================================+========================================+========================+
| `bridge <http://wiki.openstreetmap.org/wiki/Key:bridge>`__     | Bridge feature                         | boolean                |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| geometry                                                       | Geometry of the feature                | Shapely.geometry       |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| `highway <http://wiki.openstreetmap.org/wiki/Key:highway>`__   | Tag for roads, paths (road type)       | str (list if multiple) |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| `lanes <http://wiki.openstreetmap.org/wiki/Key:lanes>`__       | Number of lanes                        | int (or nan)           |
+----------------------------------------------------------------+----------------------------------------+------------------------+<commit_msg>Fixed typo.<commit_after>Network analysis in Python
==========================

Finding a shortest path using a specific street network is a common GIS problem that has many practical
applications. For example navigators are one of those "every-day" applications where **routing** using specific algorithms is used
to find the optimal route between two (or multiple) points.

It is also possible to perform network analysis such as tranposrtation routing in Python.
`Networkx <https://networkx.github.io/documentation/stable/>`__ is a Python module that provides
a lot tools that can be used to analyze networks on various different ways. It also contains algorithms
such as `Dijkstras algorithm <https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.weighted.single_source_dijkstra.html#networkx.algorithms.shortest_paths.weighted.single_source_dijkstra>`__ or
`A* <https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.shortest_paths.astar.astar_path.html#networkx.algorithms.shortest_paths.astar.astar_path>`__ algoritm that are commonly used to find shortest paths along transportation network.

To be able to conduct network analysis, it is, of course, necessary to have a network that is used for the analyses.
`Osmnx <https://github.com/gboeing/osmnx>`__ package that we just explored in previous tutorial, makes it really easy to
retrieve routable networks from OpenStreetMap with different transport modes (walking, cycling and driving). Osmnx also
combines some functionalities from ``networkx`` module to make it straightforward to conduct routing along OpenStreetMap data.

Next we will test the routing functionalities of osmnx by finding a shortest path between two points based on drivable roads.

Let's first download the OSM data from Kamppi but this time include only such street segments that are walkable.
In omsnx it is possible to retrieve only such streets that are drivable by specifying ``'drive'`` into ``network_type`` parameter that can be used to
specify what kind of streets are retrieved from OpenStreetMap (other possibilities are ``walk`` and ``bike``).

.. ipython:: python

    import osmnx as ox
    import networkx as nx
    import geopandas as gpd
    import matplotlib.pyplot as plt
    import pandas as pd

    place_name = "Kamppi, Helsinki, Finland"
    graph = ox.graph_from_place(place_name, network_type='drive')
    @savefig street_network.png width=7in
    fig, ax = ox.plot_graph(graph)

Okey so now we have retrieved only such streets where it is possible to drive with a car. Let's confirm
this by taking a look at the attributes of the street network. Easiest way to do this is to convert the
graph (nodes and edges) into GeoDataFrames.

.. ipython:: python

    edges = ox.graph_to_gdfs(graph, nodes=False, edges=True)

- Let's check what columns do we have in our data

.. ipython:: python

    edges.columns

Okey, so we have quite many columns in our GeoDataFrame. Most of the columns are fairly self-explanatory but the following table describes all of them.

+----------------------------------------------------------------+----------------------------------------+------------------------+
| Column                                                         | Description                            | Data type              |
+================================================================+========================================+========================+
| `bridge <http://wiki.openstreetmap.org/wiki/Key:bridge>`__     | Bridge feature                         | boolean                |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| geometry                                                       | Geometry of the feature                | Shapely.geometry       |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| `highway <http://wiki.openstreetmap.org/wiki/Key:highway>`__   | Tag for roads, paths (road type)       | str (list if multiple) |
+----------------------------------------------------------------+----------------------------------------+------------------------+
| `lanes <http://wiki.openstreetmap.org/wiki/Key:lanes>`__       | Number of lanes                        | int (or nan)           |
+----------------------------------------------------------------+----------------------------------------+------------------------+
{'fraction_non_alphanumeric': 0.23328234265734266, 'fraction_numerical': 0.0015297202797202797, 'mean_word_length': 2.019795447047179, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3854565', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1962, 'n_words': 835}