Have you ever wondered how many restaurants, buildings or schools are in your interested area? There is an amazing tool known as OSM(Open Street Map) which gives us the power to analyze and visualize any area in the most conveniently flexible way. In this blog you will learn how to use OpenStreetMap using OSMnx (a Python Package)
What is OSM (OpenStreetMap)?
Open Street Map is a global crowd sourced dataset which focuses at developing a free editable map of the world containing a lot of information about our environment, In short it is a great open source map of the world. You can learn a lot about Open street map here in the video below which will show you how to extract data from Open street map using web browser.
Pre – requisite Python libraries
you can drop first two if you are using Anaconda as it automatically handles all this stuff for us. otherwise we have to manually install these from the terminal using pip.
OSMnx Python Package for Map Data
OSMnx is a python package which offers really handy functions to retrieve data from OSM and analyze, visualize and construct properties of OSM street network and other spatial data from OSM. Users can download networks with small piece of code and then analyze them.
For example a simple line of code below will download, develop and model and visualize the street network for Banglore, India
import osmnx as ox banglore_street = ox.graph_from_place('Banglore, India', network_type = 'drive') blr_street_projected = ox.project_graph(banglore_street) ox.plot_graph(blr_street_projected)
What can you do with OSMnx
There are many operations you can do with the map data ranging from route analysis to bounding boxes. The following is a non-exhaustive list:
- Downloading networks with small piece of code
- Download infrastructures, footprints and Point of interest
- Downloading by name, a bounding box or point.
- Downloading walk area, bike area, different kind of streets
- Downloading nodes and edges
- Simplify network topology
- Fastest routes to closest graph edges
- Save networks to disks in different formats
- Load/Save networks from xml files
- Analyze shortest time to different edges in a graph.
conda install -c conda-forge osmnx
or on terminal using pip:
pip install osmnx
Working with OSMnx
Different GIS software packages like( QGIS, PostGIS and ArcGIS) could provide the interface to run the code here, but we will run the code in pure python here without any third party application as it will be more flexible and not incur any significant effort.
To begin with, let’s go through the above code to see what happened in there step by step.
First, let’s import the library.
import osmnx as ox
Let’s now create a variable and call the street network using place name we want to extra map data from
banglore_street = ox.graph_from_place('Banglore, India', network_type = 'drive')
Now we will assign projection (a model) to the street network
blr_street_projected = ox.project_graph(banglore_street)
We can finally visualize the imported layer by plotting the graph:
Check your directory in which you are working, it should be empty then we can save the shapefile using the save_graph_shapefile method which will require two arguments the projected place and the file name to be saved.
Now if you check the working directory it will contain a folder named ‘Bengaluru’ which will contain another folders named ‘edges’ and ‘nodes’ and they contain the relative data in different formats
Open any GIS application. We will use QGIS to open these files and analyze the data as shown below are the intersections and segments of street data.
Getting boundaries of places
As a next step, let’s find the boundary of a geographical region. If we go by the traditional way we will have to manually select an area and download it’s shapefile. However, we can do it easily using OSMnx as geopandas Geo Data Frames. The below code will get the boundary of Bengaluru (or ‘Bangalore’, which is the high tech center of India).
import osmnx as ox bengaluru_boundary = ox.geocode_to_gdf('Bengaluru, India') AXIS = ox.project_gdf(bengaluru_boundary).plot() _ = AXIS.axis('off')
You can also select any format to search for your desired location locality, city, State, Country.
place1 = ox.geocode_to_gdf('Mumbai, Maharashtra, INDIA') place2 = ox.geocode_to_gdf('Mumbai, INDIA') place3 = ox.geocode_to_gdf('Maharashtra, INDIA') place4 = ox.geocode_to_gdf('INDIA')
You can select multiple locations in your query to get a single shapefile from any geographical entity. These could be Countries, States, or cities.
places = ox.geocode_to_gdf(['India', 'China', 'Bangladesh']) places = ox.project_gdf(places) AXIS = places.plot() _ = AXIS.axis('off')
Downloading and modelling street network
OSMnx can be used to download the street network and build accurate topological street network, which can be used to plot the network. We can save the results in the form of SVG, GraphML or Shapefiles which can be later worked upon.
There are two key parameter types – one is the method for locating the map section:
- A bounding box
- Latitude-longitude co-ordinates
- Using an address and the distance we want to map
- A polygon of the desired street network’s boundaries
- Name of a place or multiple places
The second is how to traverse the network:
- drive: drive parameter will give us the paths that could be used to drive , excluding the service paths.
- drive service: drive_service parameter will give us the paths that could be used to drive, including the service paths.
- walk : renders all the paths that could be used by pedestrians, this excludes uni – directional paths.
- bike: gives us the paths that bikers and cyclists can use.
- all: collects all of the open(Public) part of maps that are available for use.
- all private” collects all the Open Street Map streets and paths, inclusive of the private ones
1. Download street networks using bounding box
This gives us the drivable street network within some latitude-longitude bounding box, in a short piece of python code
G = ox.graph_from_bbox(37.69, 37.68, -122.41, -122.43, network_type='drive') projected_G = ox.project_graph(G) ox.plot_graph(projected_G)
2. Download street networks using the latitude and longitude
This will get us the street network within 100 m (of the network) of a latitude-longitude point:
Graph = ox.graph_from_point((12.9716, 77.5946), dist=100, network_type='all') ox.plot_graph(Graph)
3. Download street network using the address
This will get us the street network within 1000 m (of network) of the Jail Road, Mumbai:
G = ox.graph_from_address('100 Jail Road, Mumbai, Maharashtra', network_type='drive') ox.plot_graph(G)
4. Download street network using a polygon
We can render a shapefile with geo-pandas, and then passing it’s geometrical shape to the OSMnx package. This will give us the street network of the Mission District in Mumbai:
Graph = ox.graph_from_polygon(mission_shape, network_type='drive') ox.plot_graph(Graph)
5. Download street network from a place name
This is where OSMnx outperforms. Pass any place name in OSMnx for which the OpenStreetMap has an edge data, and it automatically downloads & construct the street network within that perimeter. Here, we could create the driving network within the city of Mumbai, India:
Graph = ox.graph_from_place('Mumbai, India', network_type='drive') ox.plot_graph(Graph)
OpenStreetMaps are amazing to work with. We learnt that we can take out all the relevant information out of a map with just few simple lines of code, and transform that data into anything useful plan or layout for future.
Get Notified Automatically Of New Articles
Want to see more useful articles? Subscribe to our newsletter!