Introduction:
Network analysis is a powerful tool used to solve real-world problems involving connectivity and optimization. Whether it’s finding the shortest route, identifying service areas, or optimizing facility locations, network analysis enables smarter decision-making in transportation, logistics, urban planning, and more.
In this blog, we’ll explore the principles of network analysis, prepare data for analysis, and demonstrate how Python libraries like OSMnx, NetworkX, GeoPandas, Shapely, Matplotlib, Pandana, and Folium and others can help us analyze networks efficiently. By the end, you'll understand key concepts, algorithms, and applications, supported by hands-on Python code examples. You’ll also find hands-on Python examples for shortest route analysis, service area computation, and closest facility identification.
What is Network Analysis?
Network analysis examines the relationships and connectivity within a network composed of nodes and edges. Nodes represent significant points, such as road intersections or facilities, while edges denote the connections between them, such as roads, pipelines, or communication links. Together, nodes and edges form a network that can be analyzed to address real-world problems like routing, accessibility, and optimization.
In the context of transportation, network analysis helps answer questions like:
What is the shortest route between two points?
Which hospital is closest to an emergency?
What areas can a delivery vehicle cover in 10 minutes?
Preparing Network Dataset:
A network dataset is the foundation for performing accurate network-based analyses in GIS, representing interconnected pathways like roads or railways and their intersections. It includes key components such as edges (lines representing pathways) and junctions (points where pathways meet), along with attributes like travel costs, restrictions (e.g., one-way streets), and time-dependent data. The dataset models real-world conditions, enabling routing, service area delineation, and accessibility analysis. Ensuring data accuracy and topological correctness is crucial to address issues such as disconnected roads, overlaps, and dangles that could disrupt connectivity and compromise analysis results. Platforms like ArcGIS Pro or QGIS provide dedicated tools, Python libraries such as NetworkX, Osmnx, and GeoPandas can also be used to create and analyze network datasets, offering flexibility for advanced customization and integration into automated workflows.
In this blog, we use OSMnx to extract road network data directly from OpenStreetMap (OSM). OSM networks are pre-built with essential attributes, saving the effort of manually digitizing road data.
Python Libraries for Network Analysis and GIS
1.OSMnx:
Specializes in fetching real-world street networks from OpenStreetMap and converting them into graph structures for analysis. It allows extraction of road data with attributes like road type, length, and travel direction. This library is particularly useful for modeling urban networks, customizing data extractions, and visualizing network elements in geospatial contexts.
2.NetworkX:
A powerful tool for creating, analyzing, and manipulating graph-based structures. It supports operations like shortest path calculations, centrality measures, and network optimizations. Its versatility and compatibility with other libraries make it essential for analyzing network datasets.
3.GeoPandas:
Extends Pandas to handle geospatial data, supporting reading, writing, and processing formats such as shapefiles and GeoJSON. It includes tools for spatial joins, coordinate transformations, and data visualization, simplifying the handling of geographic data.
4.Shapely:
Facilitates the creation and manipulation of geometric shapes like points, lines, and polygons. It offers operations such as buffering, intersecting, and unioning, and is often paired with GeoPandas for geometry-based GIS workflows.
5.Pandana:
Optimized for high-performance accessibility analysis in large-scale networks. It enables rapid calculation of proximity metrics, such as distances to amenities, making it ideal for dense urban networks and big datasets.
6.Matplotlib:
A versatile plotting library for visualizing data and results. In network analysis, it displays graphs, routes, and structures through static plots, with extensive customization options for meaningful representation of outputs.
7.Folium:
Creates interactive, web-based maps for visualizing network results like routes, service areas, and facility locations. Its integration with various basemaps and interactive features bridges the gap between analysis and presentation.
Dijkstra's Algorithm
At the core of many network analysis problems is the shortest path problem. Dijkstra's algorithm, used by NetworkX, is a popular algorithm for solving this.
Principle
Dijkstra’s algorithm computes the shortest path between two nodes in a graph with weighted edges. The algorithm uses a greedy approach, always exploring the nearest unvisited node and updating distances. Once the shortest path to a node is finalized, it will not change. This principle ensures efficient routing and forms the foundation for navigation systems like GPS.
How It Works:
Start at the source node and initialize distances to all nodes as infinite, except the source (distance = 0).
Visit the nearest unvisited node.
Update the distances of neighboring nodes if a shorter path is found.
Repeat until all nodes are visited or the target node is reached.
Hands-On Code Examples
1. Extracting a Road Network with OSMnx
This retrieves the road network and visualizes it as a graph with nodes (intersections) and edges/ links (roads).
2. Inspecting Nodes and Edges with GeoPandas
You can analyze attributes of the road network by converting it to GeoDataFrames. This gives you insights into the network structure, such as road length, name, and directionality.
3. Shortest Route Analysis Using NetworkX and Folium
Shortest route analysis focuses on finding the most efficient path between two locations in a graph using Dijkstra’s algorithm. This method calculates the shortest path based on edge weights, such as distance, travel time, or cost. Using NetworkX, the graph is constructed from geographic data, and the shortest path is determined by snapping the origin and destination coordinates to the nearest graph nodes. Finally, Folium is used to visualize the path on an interactive map, highlighting the route with a green polyline overlaid on the city’s road network.
4. Closest Facility Analysis
Closest facility analysis identifies the nearest facility (e.g., hospitals or fire stations) to demand points (e.g., incidents or areas requiring services). First, demand points and candidate facilities are snapped to the network nodes. Using NetworkX, the shortest paths and distances between locations are computed. The closest facility for each demand point is identified based on the shortest distance. Results are visualized with Folium, showcasing demand points, facilities, and their connecting routes on an interactive map.
5. Service Area Analysis
Service area analysis maps regions around specific facilities (e.g., hospitals) to evaluate accessibility. Using NetworkX, a graph is created from geographic data, and facilities are snapped to the nearest network nodes. Subgraphs are then generated for various distance thresholds (e.g., 500m, 1000m, 1500m), representing service areas within those ranges. These subgraphs are converted into GeoDataFrames with GeoPandas, enabling spatial visualization of accessible regions around facilities on a map.
6. Facility Location Optimization
Facility location optimization aims to strategically position facilities (e.g., store locations, warehouses, etc.) to maximize accessibility for demand points. In this simulation, we generate random points to represent locations—10 for demand points and 5 for facilities. The program calculates a distance matrix between these points and applies linear programming optimization techniques using the PuLP library to minimize travel distances. The results are visualized using Matplotlib and GeoPandas, highlighting the selected facilities and their corresponding demand points, demonstrating the spatial efficiency achieved through the optimization.
Applications of Network Analysis
1.Urban Planning: Analyze traffic flow, design efficient road networks, and optimize public transportation routes to improve urban mobility.
2.Emergency Services: Identify the nearest facilities, such as hospitals or fire stations, to incidents and plan service areas to minimize response times.
3.Logistics: Optimize delivery routes, reduce transportation costs, and strategically place warehouses for efficient supply chain management.
4.Accessibility: Assess service areas for schools, parks, or stores to ensure communities have equitable access to essential facilities.
Why Use Python for Network Analysis?
1.Open-Source:
Python and its libraries are free to use, making it a cost-effective option for individuals and organizations.
2.Powerful Libraries:
Python’s ecosystem supports data retrieval, advanced analysis, and interactive visualization seamlessly.
3.Scalability:
Handle large-scale datasets efficiently, making Python suitable for city-wide or regional network analysis.
4.Customization:
Build tailored solutions with Python to address specific problems and integrate custom workflows.
5.Community Support:
Python has extensive documentation, active forums, and a large user community, ensuring accessible learning and support.
Conclusion:
Network analysis is a powerful tool for addressing real-world challenges in transportation, logistics, and urban planning. Using Python libraries like OSMnx, NetworkX, and GeoPandas, we can efficiently retrieve, analyze, and visualize networks to solve problems such as routing, accessibility, and facility placement. These tools offer a cost-effective, scalable, and flexible approach to spatial analysis, enabling data-driven decision-making. Whether optimizing delivery routes, designing emergency response networks, or improving urban infrastructure, Python's open-source ecosystem provides the necessary resources to turn complex datasets into actionable insights.