An open-source tool for reading OpenStreetMap PBF files using DuckDB
Project description
Generated using DALLยทE 3 model with this prompt: A logo for a python library with White background, high quality, 8k. Cute duck and globe with cartography elements. Library for reading OpenStreetMap data using DuckDB.
QuackOSM
An open-source tool for reading OpenStreetMap PBF files using DuckDB.
What is QuackOSM ๐ฆ?
- Scalable reader for OpenStreetMap ProtoBuffer (
pbf
) files. - Is based on top of
DuckDB
[^1] with itsSpatial
[^2] extension. - Saves files in the
GeoParquet
[^3] file format for easier integration with modern cloud stacks. - Utilizes multithreading unlike GDAL that works in a single thread only.
- Can filter data based on geometry without the need for
ogr2ogr
clipping before operation. - Can filter data based on OSM tags.
- Utilizes caching to reduce repeatable computations.
- Can be used as Python module as well as a beautiful CLI based on
Typer
[^4].
[^1]: DuckDB Website [^2]: DuckDB Spatial extension repository [^3]: GeoParquet data format [^4]: Typer docs
Installing
As pure Python module
pip install quackosm
With beautiful CLI
pip install quackosm[cli]
Required Python version?
QuackOSM supports Python >= 3.9
Dependencies
Required:
- duckdb (>=0.9.2)
- pyarrow (>=13.0.0)
- geoarrow-pyarrow (>=0.1.1)
- geopandas
- shapely (>=2.0)
- typeguard
Optional:
- typer[all] (click, colorama, rich, shellingham)
Usage
Load data as a GeoDataFrame
>>> import quackosm as qosm
>>> qosm.get_features_gdf(monaco_pbf_path)
tags geometry
feature_id
node/10005045289 {'shop': 'bakery'} POINT (7.42245 43.73105)
node/10020887517 {'leisure': 'swimming_pool', ... POINT (7.41316 43.73384)
node/10021298117 {'leisure': 'swimming_pool', ... POINT (7.42777 43.74277)
node/10021298717 {'leisure': 'swimming_pool', ... POINT (7.42630 43.74097)
node/10025656383 {'ferry': 'yes', 'name': 'Qua... POINT (7.42550 43.73690)
... ... ...
way/990669427 {'amenity': 'shelter', 'shelt... POLYGON ((7.41461 43.7338...
way/990669428 {'highway': 'secondary', 'jun... LINESTRING (7.41366 43.73...
way/990669429 {'highway': 'secondary', 'jun... LINESTRING (7.41376 43.73...
way/990848785 {'addr:city': 'Monaco', 'addr... POLYGON ((7.41426 43.7339...
way/993121275 {'building': 'yes', 'name': ... POLYGON ((7.43214 43.7481...
[7906 rows x 2 columns]
Just convert PBF to GeoParquet
>>> import quackosm as qosm
>>> gpq_path = qosm.convert_pbf_to_gpq(monaco_pbf_path)
>>> gpq_path.as_posix()
'files/monaco_nofilter_noclip_compact.geoparquet'
Inspect the file with duckdb
>>> import duckdb
>>> duckdb.load_extension('spatial')
>>> duckdb.read_parquet(str(gpq_path)).project(
... "* REPLACE (ST_GeomFromWKB(geometry) AS geometry)"
... ).order("feature_id")
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ feature_id โ tags โ geometry โ
โ varchar โ map(varchar, varchโฆ โ geometry โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ node/10005045289 โ {shop=bakery} โ POINT (7.4224498 43.7310532) โ
โ node/10020887517 โ {leisure=swimming_โฆ โ POINT (7.4131561 43.7338391) โ
โ node/10021298117 โ {leisure=swimming_โฆ โ POINT (7.4277743 43.7427669) โ
โ node/10021298717 โ {leisure=swimming_โฆ โ POINT (7.4263029 43.7409734) โ
โ node/10025656383 โ {ferry=yes, name=Qโฆ โ POINT (7.4254971 43.7369002) โ
โ node/10025656390 โ {amenity=restauranโฆ โ POINT (7.4269287 43.7368818) โ
โ node/10025656391 โ {name=Capitainerieโฆ โ POINT (7.4272127 43.7359593) โ
โ node/10025656392 โ {name=Direction deโฆ โ POINT (7.4270392 43.7365262) โ
โ node/10025656393 โ {name=IQOS, openinโฆ โ POINT (7.4275175 43.7373195) โ
โ node/10025656394 โ {artist_name=Anna โฆ โ POINT (7.4293446 43.737448) โ
โ ยท โ ยท โ ยท โ
โ ยท โ ยท โ ยท โ
โ ยท โ ยท โ ยท โ
โ way/986864693 โ {natural=bare_rock} โ POLYGON ((7.4340482 43.745598, 7.4340263 4โฆ โ
โ way/986864694 โ {barrier=wall} โ LINESTRING (7.4327547 43.7445382, 7.432808โฆ โ
โ way/986864695 โ {natural=bare_rock} โ POLYGON ((7.4332994 43.7449315, 7.4332912 โฆ โ
โ way/986864696 โ {barrier=wall} โ LINESTRING (7.4356006 43.7464325, 7.435574โฆ โ
โ way/986864697 โ {natural=bare_rock} โ POLYGON ((7.4362767 43.74697, 7.4362983 43โฆ โ
โ way/990669427 โ {amenity=shelter, โฆ โ POLYGON ((7.4146087 43.733883, 7.4146192 4โฆ โ
โ way/990669428 โ {highway=secondaryโฆ โ LINESTRING (7.4136598 43.7334433, 7.413640โฆ โ
โ way/990669429 โ {highway=secondaryโฆ โ LINESTRING (7.4137621 43.7334251, 7.413746โฆ โ
โ way/990848785 โ {addr:city=Monaco,โฆ โ POLYGON ((7.4142551 43.7339622, 7.4143113 โฆ โ
โ way/993121275 โ {building=yes, namโฆ โ POLYGON ((7.4321416 43.7481309, 7.4321638 โฆ โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 7906 rows (20 shown) 3 columns โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Use as CLI
$ quackosm monaco.osm.pbf
โ [ 1/33] Reading nodes โข 0:00:00
โ [ 2/33] Filtering nodes - intersection โข 0:00:00
โ [ 3/33] Filtering nodes - tags โข 0:00:00
โ [ 4/33] Calculating distinct filtered nodes ids โข 0:00:00
โ ธ [ 5/33] Reading ways โข 0:00:00
โ [ 6/33] Unnesting ways โข 0:00:00
โ [ 7/33] Filtering ways - valid refs โข 0:00:00
โ [ 8/33] Filtering ways - intersection โข 0:00:00
โ [ 9/33] Filtering ways - tags โข 0:00:00
โ [ 10/33] Calculating distinct filtered ways ids โข 0:00:00
โ [ 11/33] Reading relations โข 0:00:00
โ [ 12/33] Unnesting relations โข 0:00:00
โ [ 13/33] Filtering relations - valid refs โข 0:00:00
โ [ 14/33] Filtering relations - intersection โข 0:00:00
โ [ 15/33] Filtering relations - tags โข 0:00:00
โ [ 16/33] Calculating distinct filtered relations ids โข 0:00:00
โ [ 17/33] Loading required ways - by relations โข 0:00:00
โ [ 18/33] Calculating distinct required ways ids โข 0:00:00
โ [ 19/33] Saving filtered nodes with geometries โข 0:00:00
โ ธ [ 20/33] Saving required nodes with structs โข 0:00:00
โ ผ [ 21/33] Grouping filtered ways โข 0:00:00
[ 22/33] Saving filtered ways with linestrings 100% โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1/1 โข 0:00:00 < 0:00:00 โข
โ [ 23/33] Grouping required ways โข 0:00:00
[ 24/33] Saving required ways with linestrings 100% โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1/1 โข 0:00:00 < 0:00:00 โข
โ ด [ 25/33] Saving filtered ways with geometries โข 0:00:00
โ น [ 26/33] Saving valid relations parts โข 0:00:00
โ [27.1/33] Saving relations inner parts - valid geometries โข 0:00:00
โ [27.2/33] Saving relations inner parts - invalid geometries โข 0:00:00
โ [28.1/33] Saving relations outer parts - valid geometries โข 0:00:00
โ [28.2/33] Saving relations outer parts - invalid geometries โข 0:00:00
โ [ 29/33] Saving relations outer parts with holes โข 0:00:00
โ [ 30/33] Saving relations outer parts without holes โข 0:00:00
โ [ 31/33] Saving filtered relations with geometries โข 0:00:00
โ ธ [32.1/33] Saving valid features โข 0:00:00
โ [ 33/33] Saving final geoparquet file โข 0:00:00
files/monaco_nofilter_noclip_compact.geoparquet
CLI Help output:
You can find full API + more examples in the docs.
How does it work?
Basic logic
QuackOSM utilizes ST_ReadOSM
function from DuckDB
's Spatial
extension to read raw data from the PBF file:
- Nodes with coordinates and tags;
- Ways with nodes refs and tags;
- Relations with nodes and ways refs, ref roles and tags.
Library contains a logic to construct geometries (points, linestrings, polygons) from those raw features.
- Read nodes from the PBF file, save them to the parquet file.
- (Optional) Filter nodes based on geometry filter
- (Optional) Filter nodes based on tags filter
- Read ways from the PBF file, save them to the parquet file.
- Select all nodes refs and join them with previously read nodes.
- (Optional) Filter ways based on geometry filter - join intersecting nodes
- (Optional) Filter ways based on tags filter
- Read relations from the PBF file, save them to the parquet file.
- Select all ways refs and join them with previously read ways.
- (Optional) Filter relations based on geometry filter - join intersecting ways
- (Optional) Filter relations based on tags filter
- Select ways required by filtered relations
- Select nodes required by filtered and required ways
- Save filtered nodes with point geometries
- Group ways with nodes geometries and contruct linestrings
- Save filtered ways with linestrings and polygon geometries (depending on tags values)
- Divide relation parts into inner and outer polygons
- Group relation parts into full (multi)polygons and save them
- Fix invalid geometries
- Return final GeoParquet file
Memory usage
DuckDB queries requiring JOIN
, GROUP
and ORDER BY
operations are very memory intensive. Because of that, some steps are divided into chunks (groups) with a set number of rows per chunk.
QuackOSM has been roughly tuned to different workloads. The rows_per_bucket
variable is set based on an available memory in the system:
Memory | Rows per group |
---|---|
< 8 GB | 100 000 |
8 - 16 GB | 500 000 |
16 - 24 GB | 1 000 000 |
> 24 GB | 5 000 000 |
WSL usage: sometimes code can break since DuckDB is trying to use all available memory, that can be occupied by Windows.
Disk usage
The algorithm depends on saving intermediate .parquet
files between queries.
As a rule of thumb, when parsing a full file without filtering, you should have at least 10x more free space on disk than the base file size (100MB pbf file -> 1GB free space to parse it).
Below you can see the chart of disk usage during operation. Generated on a machine with Intel i7-4790 CPU with 32 GB of RAM. Red dotted line represents the size of the base file.
Monaco
PBF file size: 525 KB
Estonia
PBF file size: 100 MB
Poland
PBF file size: 1.7 GB
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file quackosm-0.3.2.tar.gz
.
File metadata
- Download URL: quackosm-0.3.2.tar.gz
- Upload date:
- Size: 674.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.11.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada7f08cfc7dffbfc6cc933952daa24d6d3e9367ec26913f6e93e78efcf2a325 |
|
MD5 | 699511d2bc657c15fc87c5cf2087da79 |
|
BLAKE2b-256 | 0944e4fd9e95f10aa869f77ab810c93e231510d13fc0b9436c0c6998efc3c335 |
Provenance
File details
Details for the file quackosm-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: quackosm-0.3.2-py3-none-any.whl
- Upload date:
- Size: 40.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.11.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bf15f309a292acac6241dd4fe9dc4b768b1a7cd7117baa07c19c95a22901b34 |
|
MD5 | 8cb16a0ef844f94849c14841ca34579f |
|
BLAKE2b-256 | 394501e8422d1126f52a4e452ff0f50e8e5ad1e6944cd44cd94b3d35192fb07d |