Home

Awesome

Tegola OSM

This repo houses instructions and configuration files to aid with standing up an OpenStreetMap export and Natural Earth dataset into a PostGIS enabled database that uses tegola for creating and serving vector tiles.

Dependencies

If you want to use these scripts you will need the following:

Basic overview

The scripts in this repo prepare 2 databases with data from 3 sources:

  1. OpenStreetMap - highly detailed data for roads, rail, buildings, rivers, lakes, and more.
  2. Natural Earth - data for country borders, state lines, land, major roads, and more.
  3. OpenStreetMap Land Polygons - high detail polygons for landmass.

To import all this data into your databases the scripts use Imposm3 for OpenStreetMap data (1) and Gdal for both NaturalEarth and OSM land polygon data (2+3).

Once the data has been imported it is ready to serve with Tegola.

Repo config files

The following files allow you to configure what data is imported into your databases and how the data is served by Tegola:

Getting started

Step 1. Prepare databases

Create two databases called osm and natural_earth and enable the extensions postgis and hstore on both of them.

createdb -E utf8 -O my_pg_user osm
psql -d osm -c "CREATE EXTENSION postgis;"
psql -d osm -c "CREATE EXTENSION hstore;"

createdb -E utf8 -O my_pg_user natural_earth
psql -d natural_earth -c "CREATE EXTENSION postgis;"
psql -d natural_earth -c "CREATE EXTENSION hstore;"

The steps below will import OSM (1) and OSM land (3) data into "osm" and Natural Earth (2) data into "natural_earth".

Step 2. Download your desired OSM dataset in PBF format

Since processing map data can be time-consuming it's best to start with a city rather than the whole planet. You can download OSM data for individual cities at Geofabrik. For this guide, we will use London.

In this repo's root directory run the following:

Step 3. Import the OSM export into PostGIS using Imposm3

You will use Imposm to map the data from the OSM dataset (1) into your osm database. Imposm requires you to do this in 2 steps.

The osm database now has all the OSM data (1) ready for use.

Step 4. Import the OSM Land and Natural Earth dataset

Now you need to add the OSM land polygon data (3) to the osm database. Update the following lines in the osm_land.sh script with your database details e.g.

DB_NAME="osm"
DB_HOST="localhost"
DB_PORT="5432"
DB_USER="your_pg_user"
DB_PW="your_password"

The Natural Earth data will be imported into the natural_earth database you created earlier. Update the same lines in the natural_earth.sh script with the relevant details. E.g.

DB_NAME="natural_earth"
DB_HOST="localhost"
DB_PORT="5432"
DB_USER="your_pg_user"
DB_PW="your_password"

Then run each file: ./natural_earth.sh && ./osm_land.sh.

This will download the natural earth and osm land datasets and insert them into PostGIS under your natural_earth and osm databases respectively.

Note: For debugging options and more advanced ways to complete this step see "Alternative ways to import the OSM Land and Natural Earth dataset" below.

Step 5. Install SQL helper functions

Execute postgis_helpers.sql against your OSM database. Currently, this contains a single utility function for converting building heights from strings to numbers which is important if you want to extrude buildings for the 3d effect.

Step 6. Setup SQL indexes

Execute postgis_index.sql against your OSM database.

Step 7. Launch Tegola

Open your browser to localhost and the port you configured Tegola to run on (i.e. localhost:8080) to see the built-in viewer.

Alternative ways to import the OSM Land and Natural Earth dataset

Step 4 took a simple approach to configure the osm_land.sh and natural_earths.sh scripts by simply having you hard code them with your DB credentials. However, there are two other options this step can be accomplished by which might suit your needs better in production environments.

Option 2: Create a dbcredentials.sh file

Create a dbcredentials.sh file which will be shared with the osm_land script. This option is ideal for when the natural_earth and osm databases will reside on the same database server, and will use the same credentials. Ensure that the following variables are defined in your file:

DB_HOST="mydbhost"
DB_PORT="myport"
DB_USER="myuser"
DB_PW="mypassword"

Once you have configured the dbcredentials.sh file, run the scripts as above:

Option 3:

Create separate configuration files in the same pattern as the above dbcredentials.sh file and pass the path to the config file using the -c option. This is ideal if you have two different servers for the databases. Ensure the file you create follows this format:

DB_NAME="mydb"
DB_HOST="mydbhost"
DB_PORT="myport"
DB_USER="myuser"
DB_PW="mypassword"

Once you have configured the files, run the scripts with the -c flag and provide the path to the credentials file, ie:

Advanced Usage

Both scripts support a -v flag for debugging. natural_earth.sh also supports a -d flag, which will drop the existing natural earth database prior to import if set. Since the osm_land.sh imports into the osm database which is shared with other data, it lacks this functionality. Instead, only the relevant tables are dropped.

Data Layers

To view these data layers in a map and query the features for a better understanding of each data layer, use the Tegola-OSM Inspector. The data layers described here are in the "Tegola-OSM" database as laid out in the tegola.toml (i.e., not the Natural Earth database that is specified in tegola-natural-earth.toml).

sourceDescription
neNatural Earth data, version 4
osmOpenStreetMap data, current
osm landOpenStreetMap-derived land polygons from openstreetmapdata.com, currentness depends on last pull

Note: All layers also have the data fields: layer id and geometry. An empty where column means that all features are retained.

populated_places

points

zoomsourcetable/layerdata fieldswhere
0-2nene_110m_populated_placesscalerank, labelrank, name, min_zoom, featurecla, rank_max
3-4nene_50m_populated_placesscalerank, labelrank, name, min_zoom, featurecla, rank_max
5-20nene_10m_populated_placesscalerank, labelrank, name, min_zoom, featurecla, rank_max

country_lines

zoomsourcetable/layerdata fieldswhere
0-2nene_110m_admin_0_boundary_lines_landfeaturecla, name, min_zoom
3-4nene_50m_admin_0_boundary_lines_landfeaturecla, name, min_zoom
5-10nene_10m_admin_0_boundary_lines_landfeaturecla, name, min_zoom

country_lines_disputed

lines

zoomsourcetable/layerdata fieldswhere
3-4nene_50m_ne_50m_admin_0_boundary_lines_disputed_areasfeaturecla, name, min_zoom
5-10nene_10m_ne_50m_admin_0_boundary_lines_disputed_areasfeaturecla, name, min_zoom

country_label_points

zoomsourcetable/layerdata fieldswhere
3-20nene_10m_admin_0_label_pointssr_subunit, scalerank

country_polygons

zoomsourcetable/layerdata fieldswhere
0-2nene_110m_admin_0_countriesfeaturecla, name, name_long, abbrev, adm0_a3, min_zoom, min_label, max_label
3-4nene_50m_admin_0_countriesfeaturecla, name, name_long, abbrev, adm0_a3, min_zoom, min_label, max_label
5-10nene_10m_admin_0_countriesfeaturecla, name, name_long, abbrev, adm0_a3, min_zoom, min_label, max_label

state_lines

zoomsourcetable/layerdata fieldswhere
0-2nene_110m_admin_1_states_provinces_linesfeaturecla, name, adm0_name, min_zoom
3-4nene_50m_admin_1_states_provinces_linesfeaturecla, name, adm0_name, min_zoom
5-10nene_10m_admin_1_states_provinces_linesfeaturecla, name, adm0_name, min_zoom

land

polygons

zoomsourcetable/layerdata fieldswhere
0-2nene_110m_landfeaturecla, min_zoom
3-4nene_50m_landfeaturecla, min_zoom
5-7nene_10m_landfeaturecla, min_zoom
8-20osm landland_polygons

admin_lines

zoomsourcetable/layerdata fieldswhere
8-12osmadmin_boundaries_8-12admin_level, name, typeadmin_level IN (1,2,3,4,5,6,7,8)
13-20osmadmin_boundaries_13-20admin_level, name, typeadmin_level IN (1,2,3,4,5,6,7,8,9,10)

state_label_points

zoomsourcetable/layerdata fieldswhere
3-20nene_10m_admin_1_label_pointsname, scalerank

landuse_areas

Nature reserves, military land, forest, leisure, wood, etc. polygons

zoomsourcetable/layerdata fieldswhere
3-5osmlanduse_areas_gen0name, class, type, areatype IN ('forest','wood','nature reserve', 'nature_reserve', 'military') AND area > 1000000000
6-9osmlanduse_areas_gen0_6name, class, type, areatype IN ('forest','wood','nature reserve', 'nature_reserve', 'military') AND area > 100000000
10-12osmlanduse_areas_gen1name, class, type, area
13-20osmlanduse_areasname, class, type, area

water_areas

polygons

zoomsourcetable/layerdata fieldswhere
3-5osmwater_areas_gen0name, class, type, areatype IN ('water', 'pond', 'basin', 'canal', 'mill_pond', 'riverbank') AND area > 1000000000
6-9osmwater_areas_gen0_6name, class, type, areatype IN ('water', 'pond', 'basin', 'canal', 'mill_pond', 'riverbank') AND area > 100000000
10-12osmwater_areas_gen1name, class, type, areatype IN ('water', 'pond', 'basin', 'canal', 'mill_pond', 'riverbank') AND area > 1000
13-20osmwater_areasname, class, type, areatype IN ('water', 'pond', 'basin', 'canal', 'mill_pond', 'riverbank', 'dock')

water_lines

zoomsourcetable/layerdata fieldswhere
8-12osmwater_lines_gen0name, typetype IN ('river', 'canal')
13-14osmwater_lines_gen1name, typetype IN ('river', 'canal', 'stream', 'ditch', 'drain', 'dam')
15-20osmwater_linesname, typetype IN ('river', 'canal', 'stream', 'ditch', 'drain', 'dam')

transport_lines

Roads, airport runways, ferry routes, paths, etc.

zoomsourcetable/layerdata fieldswhere
3-4nene_10m_roads_3name, min_zoom, min_label, type, labelmin_zoom < 5 AND type <> 'Ferry Route'
5-6nene_10m_roads_5name, min_zoom, min_label, type, labelmin_zoom <= 7 AND type <> 'Ferry Route'
7-8osmtransport_lines_gen0type, tunnel, bridge, reftype IN ('motorway','trunk','motorway_link','trunk_link','primary') AND tunnel = 0 AND bridge = 0
9-10osmtransport_lines_gen1ref, class, typetype IN ('motorway', 'trunk', 'primary', 'primary_link', 'secondary', 'motorway_link', 'trunk_link')
11-12osmtransport_lines_11-12name, ref, class, type, tunnel, bridge, access, servicetype IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'rail', 'taxiway', 'runway', 'apron')
13osmtransport_lines_13name, ref, class, type, tunnel, bridge, access, servicetype IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'rail', 'residential', 'taxiway', 'runway', 'apron')
14-20osmtransport_lines_14-20name, ref, class, type, tunnel, bridge, access, service

transport_areas

Airports, etc. polygons

zoomsourcetable/layerdata fieldswhere
12-20osmtransport_areasname, class, type

transport_points

Airports, helipads, etc.

zoomsourcetable/layerdata fieldswhere
14-20osmtransport_pointsname, class, type

amenity_areas

Fire stations, banks, embassies, government, police stations, schools, universities, etc. polygons

zoomsourcetable/layerdata fieldswhere
14-20osmamenity_areasname, type

amenity_points

Fire stations, banks, embassies, government, police stations, schools, universities, etc.

zoomsourcetable/layerdata fieldswhere
14-20osmamenity_pointsname, type

other_points

Man made, historic, military, barriers, power towers, etc.

zoomsourcetable/layerdata fieldswhere
14-20osmother_pointsname, class, type

other_lines

Man made, historic, military, barriers, power lines, etc.

zoomsourcetable/layerdata fieldswhere
14-20osmother_linesname, class, type

other_areas

polygons Man made, historic, military, power, barriers, piers, etc.

zoomsourcetable/layerdata fieldswhere
6-8osmother_areas_filtername, class, typearea > 1000000
9-20osmother_areasname, class, type

buildings

polygons

zoomsourcetable/layerdata fieldswhere
14-20osmbuildingsname, height, type

How long does it take to import the entire planet?

If you run this import, please send in a PR to report your import machine specs and how long it takes.

@peldhose: 11.30 hours on a Google cloud server with 8 vCPU, 30GB RAM and 1TB storage (400GB used)
@SahAssar 15.43 hours on a Dell XPS 13 9380 i7-8565U 16GB RAM and 1TB SSD (375GB used by postgres after import)