Categories
Blog Transportation

Bridge City – Does Hamburg Really Have More Bridges Than Venice?

People from Hamburg claim that it has more bridges than Venice. Is this really true? Find out how we can quickly answer this question using Open Street Map and the Overpass API.

Intro

Venice, Italy – the city of romance. The city of canals, gondolas and bridges. When you visit Venice, you might get the impression that it is all bridges. After all, Venice is a city built on many small islands, so it is only natural that there should be many bridges. This is why many people think that Venice is the city with the most bridges.

Then there is Hamburg, Germany. The city is known for its harbour, for St. Pauli (which offers a different kind of romance) and for the Miniatur Wunderland. It is not known for the beauty or number of its bridges.
But when you talk to people from Hamburg, they often say that Hamburg has more bridges than Venice. But they don’t know if this is true and how many bridges there are in each city.

How do we answer this question? We have to count!

Counting Bridges

How do you count the bridges in a city? You could open a map and mark each bridge. Then you count your markings. But who wants to do that when a computer can do it for you?

Enter Open Street Map (www.osm.org). OSM is an open-source map of the entire world, created and maintained by thousands of enthusiasts around the world. You can use OSM to search for places or plan routes – similar to Google Maps. But OSM does much more. It is not just a map like an atlas, it is a huge database of information about every place in the world. This database can be accessed through a computer interface called an API.

The Overpass API

So my idea was, how can I use this API to find out how many bridges there are in a given city? A little research showed that there are actually several APIs that provide access to OSM. The one that can access the properties of each element of the map is called the “Overpass API”. The documentation for this API can be found here. This API provides its own query language. It is different from other database query languages like SQL because it has a special task: Finding information in a spatial database. But the documentation provides many examples, so it was quite easy to build a query that finds all the bridges in a given city. It has only three lines:

area[name="Venezia"];
nwr[man_made="bridge"](area);
out geom;

The first line looks for an area named “Venezia”. I noticed that you have to use the name of a city in its native language. To search using names in other languages, e.g. English, I think you need to use another API called Nomatim (link).

The second line searches for all bridges within the city found in the first line. It filters all bridges from the list of all map objects within the city by the key-value pair man_made="bridge". OSM has many objects and properties, and a list of all the properties you can filter on can be found in the OSM wiki (link).

The third line specifies the output of the query. In this case it will return the geometry of the objects.

Overpass Turbo

This query works, but how do we execute it to actually count bridges?
As a programmer, my first approach was to write a Python script that would run this query and draw the result. But as the saying goes: To a man with a hammer, everything looks like a nail.

There is a much easier way to get the same result: We can use a service called Overpass Turbo (link). On this site, you can enter any Overpass query, run it, and see the result on a map. You do not have to write a single line of code. Wonderful!

Here is the result of the query for Venice (or Venetia)

Bridge City - Does Hamburg Really Have More Bridges Than Venice?

Wow, that is a lot of bridges!
What about Hamburg?

Bridge City - Does Hamburg Really Have More Bridges Than Venice?


Hm, a lot too!

Just looking at the markers on the maps, we cannot tell which city has more bridges. But we can find out by simply changing the last line in the three-line query from above:

area[name="Venezia"];
nwr[man_made="bridge"](area);
count out;

This changes the type of output we get: Instead of the geometry of the bridges, we will see the number of hits:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.62.1 084b4234">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2024-05-10T05:39:15Z" areas="2024-05-10T03:37:33Z"/>

  <count id="0">
    <tag k="nodes" v="0"/>
    <tag k="ways" v="530"/>
    <tag k="relations" v="3"/>
    <tag k="areas" v="0"/>
    <tag k="total" v="533"/>
  </count>

</osm>

This looks a little intimidating. But don’t worry, there’s only one number we need: The number of “ways”. In the case of Venice, it is 530.

If you run the same query for Hamburg, Overpass reports 1096 bridges. So Hamburg has about twice as many bridges as Venice!

That should settle the question. The people of Hamburg can be proud that their city has more bridges than Venice!

Please note: The numbers for Hamburg are not entirely correct: The search for an area named Hamburg also returned a small village in the USA. This Hamburg only counts three bridges, so the results are not too far off. However, this is something to be aware of when working on more serious use cases with OSM.

Outlook

How does Hamburg compare with other big cities? I tried it for some other cities and found these amazing numbers: Amsterdam has 1296 bridges, even more than Hamburg! Paris has only 183. And New York has 2488! Who would have thought?

Are there any cities with even more bridges? To get a systematic overview of the number of bridges in all major cities around the world, I would first have to filter out all cities with a certain population and then count the number of bridges in each city. The Overpass API has features that are more advanced than my simple example. I have not tried it so far, but I think it should be possible to do this in Overpass alone. If you try it, please drop me a note.

If this does not work, we could still use Python to build a more advanced program. There are some Python libraries that can access the Overpass API and also other OSM APIs like Nomatim. I found this tutorial for OSMPythonTools to be very helpful (link).

Answering the question at the beginning of the article was a lot of fun for me. I learned that there is a lot to learn about the world using OSM and its APIs. Thanks to all the volunteers who made this possible!

Leave a Reply

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