eyecatch

Add OpenStreetMap Data to QGIS from Geofabrik

Published: Last updated:
This article uses QGIS 3.40. The current LTR is 3.44.

What you'll learn


  • An overview of Geofabrik and when to use it
  • How to download OpenStreetMap data from the Geofabrik website
  • How to extract only specific OpenStreetMap features

Recommended for


  • Anyone who wants to add OpenStreetMap data to QGIS
  • Anyone who wants to use the Geofabrik website with QGIS

Introduction

Geofabrik is a website that provides OpenStreetMap data. With Geofabrik, you can easily download data for a specific region. This article explains how to use Geofabrik, which cuts OpenStreetMap data out by country and region. It covers practical steps: getting regional data from Geofabrik, adding it to QGIS, and extracting only specific features such as mailboxes.

For the background knowledge this article assumes, see the following article as well.

What is Geofabrik?

Geofabrik is a website that publishes OpenStreetMap data cut out by country and region.

The Geofabrik website
The Geofabrik website

The data OpenStreetMap provides officially covers the entire planet. However, the full dataset runs to hundreds of gigabytes, which is inconvenient when you only want data for one area.

Geofabrik lets you download data for a specific country or region individually. It cuts the official OpenStreetMap data at many scales, from wide regions such as Europe and South America, to countries such as Japan and the Philippines, down to areas such as Hokkaido and Shikoku. You can get just the data you need.

Geofabrik is originally a German company that works with OpenStreetMap. The site seems to be called Geofabrik because a company of that name provides it.

Pros and cons of using Geofabrik

One way to add OpenStreetMap data to QGIS is the QuickOSM plugin. However, QuickOSM fetches and extracts data from a server over the internet, so complex queries or large amounts of data can take a long time or fail to return any data.

The Geofabrik download method in this article is better suited than QuickOSM to large areas and many features. Conversely, when the target area is small or you need few features, QuickOSM adds the data faster.

For how to add data with QuickOSM, see the following article.

Download data from Geofabrik

This section walks through downloading data from the Geofabrik site, using Hokkaido as an example.

Go to the Geofabrik home page. The Sub Regions table at the lower left of the page lists regions. Find Asia and click it.

Click Asia in the Sub Regions table
Click Asia in the Sub Regions table

Selecting Asia on the home page opens the page below. Scroll down the Sub Regions table to find Japan, and click Japan.

Click Japan in the Sub Regions table
Click Japan in the Sub Regions table

Next, find the Hokkaido row in the Sub Regions table and click [.osm.pbf]. The download starts.

Find the Hokkaido row in the Sub Regions table and click [.osm.pbf]
Find the Hokkaido row in the Sub Regions table and click [.osm.pbf]

Add Geofabrik data to QGIS

Next, add the data you downloaded from Geofabrik to QGIS.

The downloaded data is in .osm.pbf format, but you can add it by drag and drop, just like an ordinary vector layer.

When you add the file, a dialog like the one below opens. For Geofabrik data, you can choose whether to import each feature shape, such as points and lines, into QGIS.

By default, all items are selected. Select the ones you need and click Add Layers
By default, all items are selected. Select the ones you need and click Add Layers

The five items in the dialog group features by the following criteria.

  • lines: What OpenStreetMap calls ways, generally linestrings. Examples are roads and railway tracks.
  • multilinestrings: Relations that are not necessarily closed (not polygons), such as public transport routes. Examples are bus and railway routes.
  • multipolygons: Relations that are closed (polygons), such as municipal boundaries and multipolygons (polygons with holes cut out of their interior), or ways that form polygons. Examples are buildings, parking lots, and municipal boundaries.
  • other_relations: Relations that meet neither of the conditions above.
  • points: What OpenStreetMap calls nodes, generally points. This includes features represented by a single point, such as mailboxes, as well as the points that make up lines and polygons.

For example, mailboxes are represented as points, so select at least points for them. For roads, select at least lines and multilinestrings. For buildings, select at least multipolygons. Then click Add Layers.

This time, select all five items and click Add Layers. The data takes a while to load, so wait for a moment.

When loading finishes, you can confirm that data such as buildings, railway lines, and roads has been added to QGIS.

The QGIS window with data around Sapporo Station loaded (©OpenStreetMap contributors)
The QGIS window with data around Sapporo Station loaded (©OpenStreetMap contributors)

Narrow down to specific features

As you can see once the data is loaded, there are too many features, so processing and loading take time. This section explains how to display only the data you need. As an example, extract and display only convenience stores.

First, convenience stores are represented both as points and as buildings (polygons). So hide every layer except the corresponding points and multipolygons layers.

Layers panel set to show only the points and multipolygons layers
Layers panel set to show only the points and multipolygons layers
A convenience store can be represented either as a point or as a building (polygon) because OpenStreetMap sometimes distinguishes between a store that is one tenant in a building and a store that is a whole building.

In OpenStreetMap, convenience store features carry the shop=convenience tag, so you can normally narrow the data down to convenience stores with the QGIS filter function.

However, in Geofabrik data, OpenStreetMap tags are not always available as QGIS attributes as they are. For example, the multipolygons layer has the shop key as an attribute, but in the points layer the shop key is contained inside the other_tags attribute, as shown below.

Attributes of a feature in the multipolygons layer (©OpenStreetMap contributors)
Attributes of a feature in the multipolygons layer (©OpenStreetMap contributors)
Attributes of a feature in the points layer (©OpenStreetMap contributors)
Attributes of a feature in the points layer (©OpenStreetMap contributors)

The next two sections therefore introduce two ways to handle the data inside the other_tags attribute.

This section often uses similar words such as “attribute” and “tag”. This article distinguishes them as follows.Attribute: In QGIS, data linked to the location information of a feature (such as a point, line, or polygon) stored in a layer.Tag: In OpenStreetMap, data linked to a feature (such as a node, way, or relation).

Method 1: Search the other_tags attribute as a string

The simplest way to narrow down to specific features with this data structure is to treat the other_tags attribute as a string and search it.

To search an attribute as a string, first open the Processing Toolbox at the center right of the QGIS window and click Vector selection → Extract by attribute.

Click Vector selection → Extract by attribute in the Processing Toolbox
Click Vector selection → Extract by attribute in the Processing Toolbox

The Extract by attribute dialog opens.

To extract the features with that condition in QGIS, set the following:

  1. Input layer: Make sure hokkaido-latest.osm - points is selected.
  2. Selection attribute: Select other_tags.
  3. Operator: Select contains.
  4. Value: Enter "shop"=>"convenience".
  5. When the settings are done, click the Run button at the lower right of the dialog.
The Extract by attribute dialog
The Extract by attribute dialog

The processing takes some time, so wait. When it finishes, QGIS adds a layer at the lower left of the main window.

Only the convenience stores near Sapporo Station are extracted (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a Geospatial Information Authority of Japan (GSI) Tiles background)
Only the convenience stores near Sapporo Station are extracted (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a Geospatial Information Authority of Japan (GSI) Tiles background)
If you run the steps above as they are, QGIS creates the layer in memory. Even if you save the project file, a layer created in memory loses its data when you quit QGIS. It is best to save layers produced by long-running processing to a GeoPackage.

Method 2: Extract specific tags from the other_tags attribute

In the other_tags attribute of Geofabrik data, multiple OpenStreetMap tags are combined into a single QGIS attribute and stored in a format called “HStore”. However, QGIS cannot use its filter function on HStore attributes, so they are hard to handle directly.

This section therefore introduces a preprocessing step that extracts the tags you need for analysis or processing from the other_tags attribute and creates them as new attributes.

To handle OpenStreetMap tags easily, use Vector table → Explode HStore Field in the Processing Toolbox. This tool extracts specific tags from an attribute and converts them into ordinary QGIS attributes.

Click Vector table → Explode HStore Field in the Processing Toolbox
Click Vector table → Explode HStore Field in the Processing Toolbox

Take convenience stores as an example. In OpenStreetMap, they should carry the shop=convenience tag. If you convert the shop key inside other_tags into a shop attribute, you can use that attribute to narrow the data down to convenience stores.

  1. Input layer: Select hokkaido-latest.osm - points.
  2. HStore field: Select other_tags.
  3. Expected list of fields separated by a comma (fields to select): Enter shop this time. To convert multiple keys to attributes, separate them with commas. For example, to use the brand key as an attribute in addition to the shop key, enter shop,brand.
  4. When the settings are done, click the Run button at the lower right.
The Explode HStore Field dialog
The Explode HStore Field dialog

The processing takes some time, and when it finishes QGIS adds a new layer. This layer copies all the features of the input layer and adds attributes, so it appears on top of the points layer.

The points layer displayed for the Sapporo urban area (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)
The points layer displayed for the Sapporo urban area (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)

Open the attribute table to see that a shop attribute has been added.

The attribute table shows the newly created shop attribute
The attribute table shows the newly created shop attribute

To narrow the new layer down to convenience stores, run the Extract by attribute tool with the following settings.

  1. Input layer: Make sure the layer you just created (here, Exploded) is selected.
  2. Selection attribute: Select shop.
  3. Operator: Select =.
  4. Value: Enter convenience.
  5. When the settings are done, click the Run button.
The Extract by attribute dialog
The Extract by attribute dialog

Back in the main window, only the convenience stores remain.

Only the convenience stores near Sapporo Station are extracted (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)
Only the convenience stores near Sapporo Station are extracted (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)

This preprocessing also makes it easy to, for example, color the features by brand value.

Convenience stores in central Sapporo colored by brand value (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)
Convenience stores in central Sapporo colored by brand value (created by processing OpenStreetMap data (©OpenStreetMap contributors) over a GSI Tiles background)

Conclusion

This article explained how to get data from Geofabrik and add it to QGIS, and how to narrow the added data down to specific features.

OpenStreetMap is freely usable map data that covers buildings, roads, restaurants, and many other features. By applying the steps in this article, you can extract the feature data you need from OpenStreetMap and use it for analysis and map making. Put this wealth of data to work in your own analyses and maps.

Note that if you create and publish maps using OpenStreetMap data, you need to provide attribution, such as a copyright notice. For details, see the official OpenStreetMap page “Copyright and License”.

About the author
QGIS LAB Editorial Team
QGIS LAB Editorial Team

QGIS LAB is a comprehensive information hub for QGIS, the open-source GIS software. Under the concept of “Geospatial for Greater Good,” we share the knowledge and skills to open up the world through location data.