Find The Best Route with OpenStreetMap using Python
Finding Nearest Hospitals with OpenStreetMap using Python
Introduction
OpenStreetMap is a project which is known as the Wikipedia of the mapping world. It is built by contributors from around the world. OSM can represent the visual symbols of our world and demonstrate the perfect idea of every place we have ever been, or will ever go to. OpenStreetMap (OSM) is now at the center of an alliance amongst the world’s largest and wealthiest technology companies. The biggest companies in the world are using OSM as a critical infrastructure for the most-used software ever written.
If we ever get into trouble or feel the urge to find places we need most, OpenStreetMap is the solution.
In this article, we are going to write a python script that uses OSM(Open Street Map) API to locate the nearest hospitals around us selecting them by their rating, location and number of empty beds. We use an API output which contains a list of hospitals, their rating, location and number of empty beds. In this system, we retrieve the list of hospitals and their distance from the starting point using dataset files and OpenStreetMap API and find the minimum distance we have to take in the minimum amount of time to get to our endpoint. Our case study location is Los Angeles and we will be working with graphs, nodes and edges to build up our map.
Requirements
Pyrosm
is a Python library for reading OpenStreetMap from Protocolbuffer Binary Format -files (*.osm.pbf) into Geopandas GeoDataFrames. Pyrosm can be installed via conda or pip package manager.
Install Pyrosm with pip command
pip install pyrosm
Install Pyrosm with conda
conda install -c conda-forge pyrosm
pandas
is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.
Install pandas library with pip
pip install pandas
Install pandas library with conda
conda install pandas
NumPy
is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects.
Install numpy library with pip
pip install numpy
Install numpy library with conda
conda install numpy
GeoPandas
is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types.
Intall Geopandas with pip
pip install geopandas
Intall Geopandas with conda
conda install geopandas
NetworkX
is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
pip install networkx
igraph
is on the Python Package Index with pre-compiled wheels for most Python distributions and platforms.
pip install igraph
OSMnx
is a Python package that lets you download geospatial data from OpenStreetMap and model, project, visualize, and analyze real-world street networks and any other geospatial geometries.
conda config --prepend channels conda-forge
folium
Manipulates your data in Python, then visualizes it in on a map.
pip install folium
API for retrieving hospitals
We will assume that the API json output is the json file below
We will now import our required libraries down below.
Loader.py
In the loader.py
file, we will first get the data of our case study location .In this article, we will be collecting data from Southern California. We will then use OSM get_data
to get the nodes and edges of the driving, cycling, walking and public transportation go_type
from the HDF data file
. After retrieving the required data, we can convert them into a graph using the Networkx
algorithm. On the graph, the distance from one node to another which is equal to edge length(km/h) and the time it takes to go from one node to another(minute) is calculated. In the final step, edge length and edge time will be added to the created graph via osmnx
methods which are add_edge_speeds
and ade_edge_travel_times
.
In get_route
function, we pass source_geo
, dest_geo
and the model we want as the go_type
(driving, cycling , walking or public service) as input parameters and get the best and most optimal route possible using shoretest_path
, the time it takes to traverse this route and the path of this route as output.
main.py
In the main.py
file, we have loaded the json file that contains the list of our hospitals. We have also located the source point which indicates our current location. Further, you can see that our distance from each hospital has been calculated. We reduce five score per each minute that it takes to get to the destination point from where we are.
In the final step, we have sorted the list of hospitals based on their corresponding score
.
Congrats! The output list is the best hospitals located around you!