How To Get Open Street Map Data with Python

How To Get Open Street Map Data with Python

Get Open Street Map Data with Python. When your app needs to know more about the world around us...

How To Get Open Street Map Data Using Python When your app needs to know more about the world around us

Have you ever been working on a project where you need some real-world geographical location data to, for example, say, how many highways cross through this particular city or how many restaurants are in this particular locality?

OpenStreetMap is a great open-source map of the world that can give us some insight into these and similar questions. There is a lot of data hidden in this data set, full of useful labels and geographic information.

The OpenStreetData Model

Let’s have a look at how OSM is structured.

We have three basic components in the OSM data model, which are nodes, ways, and relations, that all come with an ID. Many of the elements come with tags that describe specific features represented as key-value pairs.

In simple terms, nodes are points on the maps (in latitude and longitude) as in the next image of a well documented India Gate in Delhi.

Another way is an ordered list of nodes, which could correspond to a street or the outline of a house. Here is an example of NH 24 in India.

The final data element is a relation which is also an ordered list containing either nodes, ways, or even other relations.

It is used to model logical or geographic relationships between objects. This can be used for example for large structures like the Parliament of India which contains multiple polygons to describe the building.

Using the Overpass API

Now we’ll take a look at how to load data from OSM. The Overpass API uses a custom query language to define the queries.

It takes some time getting used to, but luckily there is Overpass Turbo by Martin Raifer which comes in handy to interactively evaluate our queries directly in the browser.

Let’s say you want to query nodes for cafes, then your query looks like this:

node["amenity"="cafe"]({{bbox}}); out;

Where each statement in the query source code ends with a semicolon. This query starts by specifying the component we want to query, which is, in this case, a node.

We are applying a filter by tag on our query which looks for all the nodes where the key-value pair is "amenity"="cafe". There are different options to filter by a tag that can be found in the documentation.

There are a variety of tags to choose from, one common key is amenity which covers various community facilities like a cafe, restaurant, or just a bench. To have an overview of most of the other possible tags in OSM take a look at the OSM map features or taginfo.

Another filter is the bounding box filter where {{bbox}} corresponds to the bounding box in which we want to search and work only in Overpass Turbo.

Otherwise, you can specify a bounding box by (south, west, north, east) in latitude and longitude which can look like:


Which you can try in Overpass Turbo. As we saw before in the OSM data model, there are also ways and relations that might hold the same attribute.

We can get those as well by using a union block statement, which collects all outputs from the sequence of statements inside a pair of parentheses as in:

( node["amenity"="cafe"]({{bbox}});

The next way to filter our queries is by element id. Here is the example for the query node(1); out;, which gives us the prime meridian of the world with longitude close to zero.

Another way to filter queries is by area which can be specified like area["ISO3166-1"="GB"][admin_level=2];, which gives us the area for Great Britain.

We can use this now as a filter for the query by adding (area) to our statement as in:


This query returns all cities in Great Britain. It is also possible to use a relation or a way as an area. In this case, area IDs need to be derived from an existing OSM way by adding 2400000000 to its OSM ID, or, in case of relation, by adding 3600000000.

Note that not all ways/relations have an area counterpart (i.e. those that are tagged with area=no, and most multipolygons and that don’t have a defined name=* will not be part of areas).

If we apply the relation of Great Britain to the previous example, we’ll then get:


Finally, we can specify the output of the queried data, which is configured by the out action. Until now, we specified the output as out;, but there are various additional values that can be appended.

The first set of values can control the verbosity or the detail of information of the output, such as ids, skel, body(default value), tags, meta, and count as described in the documentation.

Additionally, we can add modifiers for the geocoded information. geom adds the full geometry to each object. This is important when returning relations or ways that have no coordinates associated and you want to get the coordinates of their nodes and ways.

For example, the query rel["ISO3166-1"="GB"][admin_level=2]; out geom; would otherwise not return any coordinates. The value bb adds only the bounding box to each way and relation, and center adds only the center of the same bounding box.

The sort order can be configured by asc and qt, sorting by object ID or by quadtile index respectively, where the latter is significantly faster. Lastly, by adding an integer value, you can set the maximum number of elements to return.

After combining what we have learned so far, we can finally query the location of all Biergarten in Germany.


( node["amenity"="biergarten"](area);
out center;
Accessing with Python

To access the Overpass API with Python use the overpy package as a wrapper. Here you can see how we can translate the previous example with the overpy package:

import overpy

api = overpy.Overpass()
r = api.query("""
out center;

coords  = []
coords += [(float(node.lon), float( 
           for node in r.nodes]
coords += [(float(way.center_lon), float(way.center_lat)) 
           for way in r.ways]
coords += [(float(rel.center_lon), float(rel.center_lat)) 
           for rel in r.relations]

One nice thing about overpy is that it detects the content type (i.e. XML, JSON) from the response. For further information take a look at their documentation.

Thank you for reading !

Python Programming for Data Science and Machine Learning

Python Programming for Data Science and Machine Learning

This article provides an overview of Python and its application to Data Science and Machine Learning and why it is important.

Originally published by Chris Kambala  at

Python is a general-purpose, high-level, object-oriented, and easy to learn programming language. It was created by Guido van Rossum who is known as the godfather of Python.

Python is a popular programming language because of its simplicity, ease of use, open source licensing, and accessibility — the foundation of its renowned community, which provides great support and help in creating tons of packages, tutorials, and sample programs.

Python can be used to develop a wide variety of applications — ranging from Web, Desktop GUI based programs/applications to science and mathematics programs, and Machine learning and other big data computing systems.

Let’s explore the use of Python in Machine Learning, Data Science, and Data Engineering.

Machine Learning

Machine learning is a relatively new and evolving system development paradigm that has quickly become a mandatory requirement for companies and programmers to understand and use. See our previous article on Machine Learning for the background. Due to the complex, scientific computing nature of machine learning applications, Python is considered the most suitable programming language. This is because of its extensive and mature collection of mathematics and statistics libraries, extensibility, ease of use and wide adoption within the scientific community. As a result, Python has become the recommended programming language for machine learning systems development.

Data Science

Data science combines cutting edge computer and storage technologies with data representation and transformation algorithms and scientific methodology to develop solutions for a variety of complex data analysis problems encompassing raw and structured data in any format. A Data Scientist possesses knowledge of solutions to various classes of data-oriented problems and expertise in applying the necessary algorithms, statistics, and mathematic models, to create the required solutions. Python is recognized among the most effective and popular tools for solving data science related problems.

Data Engineering

Data Engineers build the foundations for Data Science and Machine Learning systems and solutions. Data Engineers are technology experts who start with the requirements identified by the data scientist. These requirements drive the development of data platforms that leverage complex data extraction, loading, and transformation to deliver structured datasets that allow the Data Scientist to focus on solving the business problem. Again, Python is an essential tool in the Data Engineer’s toolbox — one that is used every day to architect and operate the big data infrastructure that is leveraged by the data scientist.

Use Cases for Python, Data Science, and Machine Learning

Here are some example Data Science and Machine Learning applications that leverage Python.

  • Netflix uses data science to understand user viewing pattern and behavioral drivers. This, in turn, helps Netflix to understand user likes/dislikes and predict and suggest relevant items to view.
  • Amazon, Walmart, and Target are heavily using data science, data mining and machine learning to understand users preference and shopping behavior. This assists in both predicting demands to drive inventory management and to suggest relevant products to online users or via email marketing.
  • Spotify uses data science and machine learning to make music recommendations to its users.
  • Spam programs are making use of data science and machine learning algorithm(s) to detect and prevent spam emails.

This article provided an overview of Python and its application to Data Science and Machine Learning and why it is important.

Originally published by Chris Kambala  at


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Jupyter Notebook for Data Science

☞ Data Science, Deep Learning, & Machine Learning with Python

☞ Deep Learning A-Z™: Hands-On Artificial Neural Networks

☞ Machine Learning A-Z™: Hands-On Python & R In Data Science

☞ Python for Data Science and Machine Learning Bootcamp

☞ Machine Learning, Data Science and Deep Learning with Python

☞ [2019] Machine Learning Classification Bootcamp in Python

☞ Introduction to Machine Learning & Deep Learning in Python

☞ Machine Learning Career Guide – Technical Interview

☞ Machine Learning Guide: Learn Machine Learning Algorithms

☞ Machine Learning Basics: Building Regression Model in Python

☞ Machine Learning using Python - A Beginner’s Guide

Machine Learning, Data Science and Deep Learning with Python

Machine Learning, Data Science and Deep Learning with Python

Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks. Introducing Tensorflow, Using Tensorflow, Introducing Keras, Using Keras, Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Learning Deep Learning, Machine Learning with Neural Networks, Deep Learning Tutorial with Python

Machine Learning, Data Science and Deep Learning with Python

Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks

Explore the full course on Udemy (special discount included in the link):

In less than 3 hours, you can understand the theory behind modern artificial intelligence, and apply it with several hands-on examples. This is machine learning on steroids! Find out why everyone’s so excited about it and how it really works – and what modern AI can and cannot really do.

In this course, we will cover:
• Deep Learning Pre-requistes (gradient descent, autodiff, softmax)
• The History of Artificial Neural Networks
• Deep Learning in the Tensorflow Playground
• Deep Learning Details
• Introducing Tensorflow
• Using Tensorflow
• Introducing Keras
• Using Keras to Predict Political Parties
• Convolutional Neural Networks (CNNs)
• Using CNNs for Handwriting Recognition
• Recurrent Neural Networks (RNNs)
• Using a RNN for Sentiment Analysis
• The Ethics of Deep Learning
• Learning More about Deep Learning

At the end, you will have a final challenge to create your own deep learning / machine learning system to predict whether real mammogram results are benign or malignant, using your own artificial neural network you have learned to code from scratch with Python.

Separate the reality of modern AI from the hype – by learning about deep learning, well, deeply. You will need some familiarity with Python and linear algebra to follow along, but if you have that experience, you will find that neural networks are not as complicated as they sound. And how they actually work is quite elegant!

This is hands-on tutorial with real code you can download, study, and run yourself.

Python for Data Science and Machine Learning

Python for Data Science and Machine Learning

This Python tutorial for Data Science and Machine Learning will kick-start your learning of Python concepts needed for data science, as well as programming in general. Understand how to use the Jupyter Notebook, Understanding of Python from the beginning, Learn to use Object Oriented Programming with classes, Learn how to use NumPy, Pandas, Seaborn, Matplotlib, Plotly, Scikit-Learn, Machine Learning, Tensorflow, and more!

Master Python Complete Course

Python for Data Science and Machine Learning

This course will teach you from Python basics to advanced concepts in a practical manner, with Hands on exercises covered as well.

This Python tutorial for data science will kick-start your learning of Python concepts needed for data science, as well as programming in general. Python is required for data science because, Python programming is a versatile language commonly preferred by data scientists and big tech giant companies around the world, from startups to behemoths.

Whether you are a newbie in data science or already know about basic python for data science, this course is for you. In this python certification course, you will Learn Python programming in a practical manner with hands on coding assignments at the end of each section.

What you’ll learn

  • Get a complete understanding of Python from the beginning
  • Understand how to use the Jupyter Notebook
  • Master basics like variables, functions, tuples etc
  • Get hands-on with carefully designed coding assignments
  • Learn to use Object Oriented Programming with classes
  • Special Features and functions
  • Loops and condition formatting