Skip to main content

Example: Building Map

Map with buildings of a section of Brasília

You don’t always have to load all the OSM data into the database to create something useful (or beautiful). In this example we take the data from a city and only load the building footprints into the database. The whole process only takes a few seconds and we can show a pretty map.

As example city we are using Brasília here, the capital of Brazil. As I write this, it isn’t fully mapped yet, some buildings are missing. But the distinctive shape of this planned city shows up nevertheless.

Getting and Preparing the Data

First, we need some data. The smallest extract available from the Geofabrik Download server containing the city is the “centro-oeste” region. Lets download that:


A quick search on the OSM web site find the Distrito Federal as a good boundary of the area of interest. If we scroll down to the end of the box on the left side of that page we find a link “Download XML”. We get the URL from that and add “/full” to the end. Downloading that gives use the relation containing the boundary including all ways and nodes needed:

wget -O federal-district.osm

We can now use Osmium to create an extract containing only the data inside the Distrito Federal from the “centro-oeste” region:

osmium extract -p federal-district.osm -o brasilia.osm.pbf centro-oeste-latest.osm.pbf

The result is the file brasilia.osm.pbf containing all OSM data for the federal district.

Running Osm2pgsql

Now we import this data into the database with osm2pgsql. First we need a style file for the flex output that only imports the buildings. We need a single table with a polygon geometry. Because we don’t need any tags in the database, we don’t import them.

Download Lua config
local buildings = osm2pgsql.define_area_table('buildings', {
    -- Define an autoincrementing id column, QGIS likes a unique id on the table
    { column = 'id', type = 'serial', create_only = true },
    -- The 'multi=false' setting tells osm2pgsql to split up MultiPolygons into
    -- several Polygon geometries.
    { column = 'geom', type = 'polygon', multi = false },

function osm2pgsql.process_way(object)
    if object.is_closed and object.tags.building then
            geom = { create = 'area' }

function osm2pgsql.process_relation(object)
    if object.tags.type == 'multipolygon' and object.tags.building then
            geom = { create = 'area' }

Now we load the data:

osm2pgsql -d mbuildings -O flex -S buildings.lua brasilia.osm.pbf

And create an index on the buildings table:

psql mbuildings -c "CREATE UNIQUE INDEX ON buildings (id);"


There are many ways to visualize the data now. In this case I loaded the data into QGIS which has excellent support for PostgreSQL/PostGIS layers. After a few clicks loading the database table and changing the style, I got this:

Building map of Brasília