PyMapGIS Documentation

Modern GIS toolkit for Python - Simplifying geospatial workflows with built-in data sources, intelligent caching, and fluent APIs

View the Project on GitHub pymapgis/core

Advanced Network Analysis Example: Shortest Path & Isochrones

This example demonstrates how to perform common network analysis tasks—calculating the shortest path and generating isochrones (reachability polygons)—using osmnx, a powerful Python library for street network analysis. pymapgis would typically wrap or provide similar functionalities, often leveraging osmnx or networkx in the backend.

Functionality

The network_analysis_example.py script performs the following:

  1. Fetches Network Data: It downloads the drivable street network for a specified area (“Piedmont, California, USA”) using osmnx.graph_from_place. The graph is then projected to a suitable UTM zone for metric calculations.
  2. Shortest Path Calculation:
    • Defines an origin and a destination coordinate pair.
    • Finds the nearest network nodes to these coordinates.
    • Calculates the shortest path between these nodes based on street length.
    • Prints the sequence of node IDs in the path and the total path length in meters.
    • Plots the street network graph with the shortest path highlighted, along with markers for origin and destination.
  3. Isochrone Generation:
    • Defines a central point (using the origin from the shortest path example).
    • Calculates isochrones for several travel distances (e.g., 500m, 1km, 2km).
    • The isochrones are generated by creating an “ego graph” for each distance (a subgraph of all nodes reachable within that distance) and then finding the convex hull of those nodes. This is a simplified approach; more complex methods exist for more accurate isochrones (e.g., using ox.isochrones.isochrones_from_node which considers travel speeds, or alpha shapes).
    • Prints the number of isochrone polygons generated.
    • Plots the street network graph with the generated isochrone polygons overlaid, and the center point marked.
  4. Error Handling: Includes basic error handling for issues like network data download failures or no path found.

Dependencies

To run this example, you will need:

You can typically install these using pip:

pip install osmnx networkx matplotlib geopandas shapely pandas descartes

How to Run

  1. Navigate to the example directory:
    cd path/to/your/pymapgis/docs/examples/network_analysis_advanced/
    
  2. Run the script:
    python network_analysis_example.py
    

    The script requires an active internet connection to download map data from OpenStreetMap.

Expected Output

The script will print information to the console and display two plot windows (one after the other):

Console Output:

Plot Windows:

  1. Shortest Path Plot: A window will pop up displaying the street network of Piedmont, CA. The calculated shortest path will be highlighted in red, with the origin marked (e.g., green) and destination marked (e.g., blue).
  2. Isochrone Plot: A second window will pop up showing the same street network. Isochrone polygons (e.g., semi-transparent blue areas) will be overlaid, representing reachable areas from the center point for the defined travel distances. The center point will be marked (e.g., red).

Note: The plot windows might require you to close them manually to allow the script to proceed or finish. The appearance of plots can vary based on your Matplotlib backend and environment.

If you encounter errors, ensure all dependencies are correctly installed and that you have a stable internet connection. Some locations might occasionally have incomplete data on OpenStreetMap, which could affect osmnx’s ability to build a graph.