146
OSM Data Processing with PostgreSQL / PostGIS Jochen Topf jochentopf.com

OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

  • Upload
    others

  • View
    19

  • Download
    1

Embed Size (px)

Citation preview

Page 1: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

OSMData Processing

withPostgreSQL / PostGIS

Jochen Topfjochentopf.com

Page 2: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostgreSQL

OpenStreetMap

Page 3: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Studley Tool Chest | CC-BY-SA | https://www.flickr.com/photos/publicresourceorg/493813720

Page 4: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

From help.openstreetmap.org

Page 5: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 6: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

What we will talk about...

● Background: Relational Databases, Geodata

● Converting OSM Data

● Use Cases

● Tools

● Tips & Tricks, Odds & Ends

Page 7: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

▌▌ ►

Page 8: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Background:

Relational Databases

Page 9: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Databases

Databases store and manipulate data.

There are many different ways to organize data...

Page 10: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

All data is organized in Tables.

Page 11: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 12: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 13: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 14: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 15: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 16: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 17: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Data Types

Fields have a type:

TextInteger

NumericDate

...

Page 18: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

ID Name Place Age

1 Joe Sydney 34

2 Jenny New York 42

3 Jeremy Moskow 55

Table Members:

Page 19: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Structured Query Language

Page 20: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Data Access using SQL

SELECT Name FROM Members;

Name

Joe

Jenny

Jeremy

Page 21: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Data Access using SQL

SELECT Name, Age FROM Members;

Name Age

Jenny 42

Joe 34

Jeremy 55

Page 22: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Data Access using SQL

SELECT Name, Age FROM Members ORDER BY Age;

Name Age

Joe 34

Jenny 42

Jeremy 55

Page 23: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Data Access using SQL

SELECT Name, Age FROM Members WHERE Age > 40 ORDER BY Name;

Name Age

Jenny 42

Jeremy 55

Page 24: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

INSERT, UPDATE, DELETEINSERT INTO Members (Name, Place, Age)

VALUES (’Julia’, ’London’, 27);

UPDATE Members SET Place = ’Helsinki’ WHERE Id = 2;

DELETE FROM Members WHERE Name = ’Jeremy’;

Page 25: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

INSERT, UPDATE, DELETEINSERT INTO Members (Name, Place, Age)

VALUES (’Julia’, ’London’, 27);

UPDATE Members SET Place = ’Helsinki’ WHERE Id = 2;

DELETE FROM Members WHERE Name = ’Jeremy’;

Page 26: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

INSERT, UPDATE, DELETEINSERT INTO Members (Name, Place, Age)

VALUES (’Julia’, ’London’, 27);

UPDATE Members SET Place = ’Helsinki’ WHERE Id = 2;

DELETE FROM Members WHERE Name = ’Jeremy’;

Page 27: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Advanced SQL: Aggregate Functions

SELECT Avg(Age) FROM Members; Avg

43

Page 28: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Advanced SQL: JOIN

Page 29: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

The Magic

You define a structure (“schema“).

You add data.

You ask for data back.

The database software does everything else.

Page 30: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Not so magic...

Performance can depend on structure

You still need to know a bit…

Page 31: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Indexes

Indexes allow faster accessfor some queries

Tradeoff: Indexes need space and need to be updated vs. faster queries

Page 32: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

PostgreSQL

MySQL, MariaDB

SQLite

...

Page 33: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Relational Databases

PostgreSQL

MySQL, MariaDB

SQLite

...

Page 34: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostgreSQL

Open Source

lots of features

good documentation, books, etc.

popular, great eco-system, well-supported

powerful plugin system

Page 35: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Background:

Geodata

Page 36: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Simple Feature Model

Point LineString Polygon

MultiPoint MultiLineString MultiPolygon

Page 37: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

We want to store thisin a database

Page 38: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

A Naive Approach...

ID Name X Y

1 Joe 151.22 -33.85

2 Jenny -74.01 40.70

3 Jeremy 37.61 55.95

Page 39: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

A Better Approach...

TextInteger

NumericDate

Geometry...

Page 40: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

A Better Approach...

ID Name Geom

1 Joe POINT(151.22 -33.85)

2 Jenny POINT(-74.01 40.70)

3 Jeremy POINT(37.61 55.95)

Page 41: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: Plugin

CREATE EXTENSION postgis;

Page 42: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: Datatypes

GEOMETRY

GEOMETRY (POINT)GEOMETRY (LINESTRING)

GEOMETRY (POLYGON)

(also: GEOGRAPHY)

Page 43: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Source: Wikipedia

Page 44: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Source: Wikipedia

Page 45: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Source: Wikipedia

Page 46: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Source: Wikipedia

Page 47: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Source: Wikipedia

Page 48: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: CRS

PostGIS knowns > 5000 Coordinate Systems (CRS/SRS)

Each Geometry associated with SRID.

Allows Transformations

Mix and match data sources

Page 49: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Most important CRSes:

WGS84 – EPSG:4326

Web Mercator – EPSG:3857

Page 50: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: Datatypes

GEOMETRY (POINT, 4326)GEOMETRY (LINESTRING, 4326)

GEOMETRY (POLYGON, 4326)

Page 51: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Coordinates

Always first X axis, then Y axis(as in mathematics).

so: longitude first, then latitude.

Page 52: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Well Known Text (WKT)

POINT(4 3)

LINESTRING(12 4, 3 2, 7, 9)

POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))

MULTIPOINT / -LINESTRING / -POLYGON

Page 53: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: Indexes

Normal indexes are good for1-dimensional data

Spatial indexes are good for2/3-dimensional data

(R -tree)

Page 54: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: Operations

Huge number of

operations on spatial data

Page 55: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: ST_Contains

Image:CC-BY-NC-SA www.h2gis.org

Page 56: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: ST_Union

Image:CC-BY-NC-SA www.h2gis.org

Page 57: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: ST_Intersection

Image:CC-BY-NC-SA www.h2gis.org

Page 58: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: ST_Buffer

Image:CC-BY-NC-SA www.h2gis.org

Page 59: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

PostGIS: ST_ShortestLine

Image:CC-BY-NC-SA www.h2gis.org

Page 60: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

ConvertingOSM Data

Page 61: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

OSM Data Model

Page 62: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Mismatch

OSMData Model

Relational /Simple Feature

Data Model

Page 63: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Mismatch

OSMData Model

Relational /Simple Feature

Data Model

Conversion

Page 64: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 65: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 66: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Selection

What data do we actually need?

nodes, ways, relations?

user id, timestamp, version, …?

which tags?

Page 67: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Data Types

tags in OSM:key → value (both text)

Map to:text, integer, boolean, enums, ...

Page 68: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Tags → Attributes

tags in OSM are flexible, table columns are fixed

Highway Name Oneway

primary Main St false

residential Elm St true

trunk true

Page 69: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Tags → Attributes

tags in OSM are flexible, table columns are fixed

Highway Name Oneway

primary Main St false

residential Elm St true

trunk true

Page 70: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Tags → Attributes

tags in OSM are flexible, table columns are fixed

Highway Name Oneway

primary Main St false

residential Elm St true

trunk true

Page 71: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: hstore and JSON

Place Name

city de: München, en: Munich

city de: Aachen, fr: Aix-la-Chapelle

village de: Lübben, hsb: Lubin

Page 72: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Tables

split data into tables…

few tables vs. many tables

by geometry type and/or by subject type

Page 73: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Split by Geometry Type

Tables:

nodeswaysareas

Page 74: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Split by Geometry/Feature Class

Tables:

highwaysrailways

riverspowerlines

...

restaurantsbus_stopsaddressesplaces...

lakesforests

countriesbuildings

...

Page 75: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Handling lists

nodes in ways

members in relations

tags in nodes, ways, or relations

Page 76: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Way Nodes

WayId

NodeId

SeqNo

WayId

Version

UserId

...

Ways WayNodes Nodes

NodeId

Version

UserId

...

Page 77: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Way Nodes

WayId

Version

UserId

NodeIds

Ways Nodes

NodeId

Version

UserId

...Array of Ids

Page 78: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Relation Members

similar to way nodes

but

array of tuple (type, id, role)

Page 79: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: GeometryNodes → Points

Ways → LineStrings / Polygons

Multipolygon relations → Polygons

Route relations → MultiLineStrings

...

Page 80: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Geometry

Generalized geometries

For lower zoom levels / small scales

Selection – Merging – Simplification

Page 81: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Where?

Conversion can happen

1. before import in code

2. after import in the DB

Page 82: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 83: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 84: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Where?

Conversion can happen

1. before import in code

2. after import in the DB

Page 85: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Where?

Conversion can happen

1. before import in code

2. after import in the DB

fast

flexible

Page 86: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Assemble Lines

Take Locations from Nodes

Assemble them into LineStrings

Page 87: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Node locations

Where to store node locations?

1. in the database

2. in specialized index

Page 88: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Node Location Store

Page 89: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Conversion: Polygons

Assemble (Multi)Polygons

from Ways/Relations

Page 90: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

One-offImport

lots of trade-offs

not all software/schemas support updates

Import +Updates

Page 91: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Complete data needed for updates

Page 92: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Complete data needed for updates

Page 93: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Complete data needed for updates

Two kinds of data:1. The data you need for you application

2. The data needed to allow updating

Page 94: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Complete data needed for updates

Two kinds of data:1. The data you need for you application

2. The data needed to allow updating

Where? Database? External Storage?

Page 95: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Snapshot vs. History

most use cases only needcurrent OSM data

some needhistory of OSM data

Page 96: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Snapshot vs. History

most use cases only needcurrent OSM data

some needhistory of OSM data

Much more effort needed !

Page 97: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Use Cases

Page 98: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 99: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 100: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Use Cases

API DB

Rendering

Geocoding

Routing

Analytics

Page 101: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

API DB

Schema used in the main OSM database

PostgreSQL - No PostGIS !

Normal access via HTTP API

You can run your own

Page 102: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

API DBNeeds all (also historical) data

Multiple writers, transactions

Allow bounding-box download

Allow read/write access

Create full dumps and replication diffs

Page 103: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Rendering

Turning data into maps

Render into bitmap, vector tiles, etc.

Page 104: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

RenderingGet all data for an area quickly

Multiple layers

Create generalized geometries

One writer, multiple reader

Regular updates

Page 105: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Geocoding

“Search“

Geocoding – Address to Location

Reverse Geocoding – Location to Address

Page 106: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Geocoding

Build address hierarchy

Quick “fuzzy“ search

One writer, multiple reader

Regular updates

Page 107: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Routing

Using PostgreSQL plugin PgRouting

Flexible, but slow

Page 108: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Routing

Build network of streets

Calculate weights

Find route through network

Page 109: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Analytics

Statistics

Comparing data

Conflating data

Many diverse needs

Page 110: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Example 1: Wind Power

Find a place that …

… has lots of steady winds… is near existing high voltage lines

… is far from residential areas

Page 111: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Example 2: Public Transport

How far is the nearest public transport stop?

How many people live where the neareststop is more than x meters away?

Where should a new bus route go?

Page 112: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Example 3: OSM Contributors

Who are the most active OSM contributors?

What kinds of things do they map?

Where do they do their mapping?

Page 113: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Analytics

Flexible data model

Use of many geometric operations

Batch processing in multiple steps

Page 114: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Tools

Page 115: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

psql

Page 116: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

psql

Editor

Page 117: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

pgadmin

Page 118: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 119: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and
Page 120: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Osmosis

https://wiki.osm.org/wiki/Osmosis

Use case: API DB, AnalyticsUpdates: Yes

Schema: SeveralStatus: Not being maintained

Page 121: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Osmosis Schemas

API DB (version 0.6)

PostGIS Snapshot Schema (uses hstore)

PostGIS Simple Schema (no hstore)

(API DB MySQL <0.6)

Page 122: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

osm2pgsql

Used in “standard“ OSM rendering toolchain

https://wiki.osm.org/wiki/Osm2pgsql

Use case: RenderingUpdates: Yes

Schema: Few tables (hstore optional)Status: Maintained

Page 123: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Imposm3

Alternative rendering toolchain

https://imposm.org

Use case: RenderingUpdates: Yes

Schema: Many tablesStatus: Actively maintained

Page 124: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

NominatimStandard OSM search/geocoding

https://nominatim.org

Use case: (Reverse) GeocodingSchema: Optimized for geocoding

Status: Actively maintained

Uses osm2pgsql (with special plugin)

Page 125: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Osmium

https://osmcode.org/osmium-tool/

Use case: Analytics, (Rendering)

Updates: NoSchema: Simple

Status: Actively maintainedSimple to run for ad-hoc use

Page 126: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

osm-postgresql-experiments

Experimental, very flexible data import

https://github.com/osmcode/osm-postgresql-experiments

Use case: Rendering, AnalyticsUpdates: (Yes)

Schema: FlexibleStatus: Experimental

Page 127: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

osm2pgrouting

Importer für PgRouting

https://github.com/pgrouting/osm2pgrouting

Use case: RoutingUpdates: No

Schema: PgRoutingStatus: Maintained

Page 128: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Tips & Tricks

Odds & Ends

Page 129: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Quantity Structure

How much disk space do I need?

How much memory do I need?

How long will an import take?

Page 130: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Quantity Structure

How much disk space do I need?

How much memory do I need?

How long will an import take?

hundreds of Gbytesfor full planet

Page 131: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Quantity Structure

How much disk space do I need?

How much memory do I need?

How long will an import take?

hundreds of Gbytesfor full planet

More!

Page 132: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Quantity Structure

How much disk space do I need?

How much memory do I need?

How long will an import take?

hundreds of Gbytesfor full planet

many hoursif not days for planet

More!

Page 133: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Start small...

Do not try to import the whole planet at first!

Start small(e.g. with data for a city)

andwork your way up

Page 134: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Minimize data

1. Filter data outside DB if you can

2. Import data into DB

Page 135: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Importing Data

1. load data

2. create indexes

3. ANALYZE

Page 136: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Performance Tuning

You will need to tune your PostgreSQL!

Settings in postgresql.conf:shared_buffers, work_mem, maintenance_work_mem, fsync,

synchronous_commit, checkpoint_timeout, checkpoint_completion_target, ...

Page 137: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Indexes

Learn how indexes work andwhen they are used

Also for spatial indexes!

Use EXPLAIN command

Page 138: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

The COPY command

COPY instead of INSERT

more efficient, use it if possible

Page 139: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Learning Curve

PostgreSQL / PostGISis an incredible powerful tool

“Magic“ working of PostgreSQLcan be surprising

Configure the logs and look at them

Page 140: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Spatial operations are extra magic!

Again: Start small

Learning Curve

Page 141: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Always growing

Database will grow over time(not only because of more OSM data)

VACUUM

Still grow more

Page 142: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Other SQL Databases

MySql, MariaDB

Oracle Spatial

Sqlite (Library, not Server)

Page 143: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Other SQL Databases

MySql, MariaDB

Oracle Spatial

Sqlite (Library, not Server)

geodata support lacking

Page 144: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Other SQL Databases

MySql, MariaDB

Oracle Spatial

Sqlite (Library, not Server)

geodata support lacking

proprietary

Page 145: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

Other SQL Databases

MySql, MariaDB

Oracle Spatial

Sqlite (Library, not Server)

geodata support lacking

proprietary

not as powerful, problems with huge datasets,but can be useful for some applications

Page 146: OSM Data Processing with PostgreSQL / PostGIS · 2019-10-07 · PostGIS knowns > 5000 Coordinate Systems (CRS/SRS) Each Geometry associated with SRID. Allows Transformations Mix and

THE END

Jochen Topfjochentopf.com

[email protected]

https://www.floss-shop.de/de/floss-merchandise/stofftiere/40/postgresql-elefant