Use Open Street Map To Show Geodata with python 3


open street maps python3
Advanced

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.

Familiarize yourself with open street map

Pre – requisite Python libraries

  • geopandas
  • rtree
  • OSMnx

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.

Installing OSMnx

OSMnx is on github you can install it with anaconda

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:

ox.plot_graph(blr_street_projected)
Plotted Graph of Bengaluru

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.

 ox.save_graph_shapefile(blr_street_projected, filename='Bengaluru')

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.

Segments of street data
Intersection 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')
Multiple Locations in single query

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:

  1. A bounding box
  2. Latitude-longitude co-ordinates
  3. Using an address and the distance we want to map
  4. A polygon of the desired street network’s boundaries
  5. Name of a place or multiple places

The second is how to traverse the network:

  1. drive: drive parameter will give us the paths that could be used to drive , excluding the service paths.
  2. drive service: drive_service parameter will give us the paths that could be used to drive, including the service paths.
  3. walk : renders all the paths that could be used by pedestrians, this excludes uni – directional paths.
  4. bike: gives us the paths that bikers and cyclists can use.
  5. all: collects all of the open(Public) part of maps that are available for use.
  6. 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)

Conclusion

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Content