Skip to content

kraina-ai/quackosm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

73 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


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.

GitHub Checks GitHub Workflow Status - DEV GitHub Workflow Status - PROD pre-commit.ci status CodeFactor Grade Codecov Package version Supported Python versions PyPI - Downloads

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 DuckDB1 with its Spatial2 extension.
  • Saves files in the GeoParquet3 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 Typer4.

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.10.2): For all DuckDB operations on PBF files

  • pyarrow (>=13.0.0): For parquet files wrangling

  • pyarrow-ops: For easy removal of duplicated features in parquet files

  • geoarrow-pyarrow (>=0.1.2): For GeoParquet IO operations

  • geopandas: For returning GeoDataFrames and reading Geo files

  • shapely (>=2.0): For parsing WKT and GeoJSON strings and fixing geometries

  • typeguard: For internal validation of types

  • psutil: For automatic scaling of parameters based on available resources

  • pooch: For downloading *.osm.pbf files

  • tqdm: For showing progress bars

  • requests: For iterating OSM PBF files services

  • beautifulsoup4: For parsing HTML files and scraping required information

Optional:

  • typer[all] (>=0.9.0) (click, colorama, rich, shellingham): For CLI

  • osmnx: For geocoding of strings in CLI

  • h3 (>=4.0.0b1): For reading H3 strings in CLI

  • h3ronpy (>=0.18.0): For transforming H3 indexes into geometries

  • s2: For transforming S2 indexes into geometries

  • python-geohash: For transforming GeoHash indexes into geometries

Usage

If you already have downloaded the PBF file πŸ“πŸ—ΊοΈ

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/32] Reading nodes β€’ 0:00:00
β ‹ [   2/32] Filtering nodes - intersection β€’ 0:00:00
β ™ [   3/32] Filtering nodes - tags β€’ 0:00:00
β ‹ [   4/32] Calculating distinct filtered nodes ids β€’ 0:00:00
β ™ [   5/32] Reading ways β€’ 0:00:00
β ‹ [   6/32] Unnesting ways β€’ 0:00:00
β Ή [   7/32] Filtering ways - valid refs β€’ 0:00:00
β ‹ [   8/32] Filtering ways - intersection β€’ 0:00:00
β ™ [   9/32] Filtering ways - tags β€’ 0:00:00
β ‹ [  10/32] Calculating distinct filtered ways ids β€’ 0:00:00
β ‹ [  11/32] Reading relations β€’ 0:00:00
β ‹ [  12/32] Unnesting relations β€’ 0:00:00
β Ή [  13/32] Filtering relations - valid refs β€’ 0:00:00
β ‹ [  14/32] Filtering relations - intersection β€’ 0:00:00
β ™ [  15/32] Filtering relations - tags β€’ 0:00:00
β ‹ [  16/32] Calculating distinct filtered relations ids β€’ 0:00:00
β ™ [  17/32] Loading required ways - by relations β€’ 0:00:00
β ‹ [  18/32] Calculating distinct required ways ids β€’ 0:00:00
β ™ [  19/32] Saving filtered nodes with geometries β€’ 0:00:00
β ‹ [20.1/32] Grouping filtered ways - assigning groups β€’ 0:00:00
β § [20.2/32] Grouping filtered ways - joining with nodes β€’ 0:00:00
β ‹ [20.3/32] Grouping filtered ways - partitioning by group β€’ 0:00:00
  [  21/32] Saving filtered ways with linestrings 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1 β€’ 0:00:00 < 0:00:00 β€’
β ‹ [22.1/32] Grouping required ways - assigning groups β€’ 0:00:00
β § [22.2/32] Grouping required ways - joining with nodes β€’ 0:00:00
β ‹ [22.3/32] Grouping required ways - partitioning by group β€’ 0:00:00
  [  23/32] Saving required ways with linestrings 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1 β€’ 0:00:00 < 0:00:00 β€’
β Ή [  24/32] Saving filtered ways with geometries β€’ 0:00:00
β Έ [  25/32] Saving valid relations parts β€’ 0:00:00
β ‹ [26.1/32] Saving relations inner parts - valid geometries β€’ 0:00:00
β ‹ [26.2/32] Saving relations inner parts - invalid geometries β€’ 0:00:00
β ‹ [27.1/32] Saving relations outer parts - valid geometries β€’ 0:00:00
β ‹ [27.2/32] Saving relations outer parts - invalid geometries β€’ 0:00:00
β ‹ [  28/32] Saving relations outer parts with holes β€’ 0:00:00
β ‹ [  29/32] Saving relations outer parts without holes β€’ 0:00:00
β ™ [  30/32] Saving filtered relations with geometries β€’ 0:00:00
β Ή [31.1/32] Saving valid features β€’ 0:00:00
β ‹ [  32/32] Saving final geoparquet file β€’ 0:00:00
Finished operation in 0:00:06
files/monaco_nofilter_noclip_compact.geoparquet

Let the QuackOSM automatically download the required OSM PBF files for you πŸ”ŽπŸŒ

Load data as a GeoDataFrame

>>> import quackosm as qosm
>>> import osmnx as ox
>>> geometry = ox.geocode_to_gdf("Vatican City").unary_union
>>> qosm.get_features_gdf_from_geometry(geometry)
                                              tags                      geometry
feature_id
node/10253371713   {'crossing': 'uncontrolled',...     POINT (12.45603 41.90454)
node/10253371714               {'highway': 'stop'}     POINT (12.45705 41.90400)
node/10253371715               {'highway': 'stop'}     POINT (12.45242 41.90164)
node/10253371720     {'artwork_type': 'statue',...     POINT (12.45147 41.90484)
node/10253371738               {'natural': 'tree'}     POINT (12.45595 41.90609)
...                                            ...                           ...
way/983015528     {'barrier': 'hedge', 'height'...  POLYGON ((12.45027 41.901...
way/983015529     {'barrier': 'hedge', 'height'...  POLYGON ((12.45028 41.901...
way/983015530     {'barrier': 'hedge', 'height'...  POLYGON ((12.45023 41.901...
way/998561138     {'barrier': 'bollard', 'bicyc...  LINESTRING (12.45821 41.9...
way/998561139     {'barrier': 'bollard', 'bicyc...  LINESTRING (12.45828 41.9...

[3286 rows x 2 columns]

Just convert geometry to GeoParquet

>>> import quackosm as qosm
>>> from shapely import from_wkt
>>> geometry = from_wkt(
...     "POLYGON ((14.4861 35.9107, 14.4861 35.8811, 14.5331 35.8811, 14.5331 35.9107, 14.4861 35.9107))"
... )
>>> gpq_path = qosm.convert_geometry_to_gpq(geometry)
>>> gpq_path.as_posix()
'files/4b2967088a8fe31cdc15401e29bff9b7b882565cd8143e90443f39f2dc5fe6de_nofilter_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/10001388317 β”‚ {amenity=bench, ba…  β”‚ POINT (14.5093988 35.8936881)                β”‚
β”‚ node/10001388417 β”‚ {amenity=bench, ba…  β”‚ POINT (14.5094635 35.8937135)                β”‚
β”‚ node/10001388517 β”‚ {amenity=bench, ba…  β”‚ POINT (14.5095215 35.8937305)                β”‚
β”‚ node/10018287160 β”‚ {opening_hours=Mo-…  β”‚ POINT (14.5184916 35.8915925)                β”‚
β”‚ node/10018287161 β”‚ {defensive_works=b…  β”‚ POINT (14.5190093 35.8909471)                β”‚
β”‚ node/10018287162 β”‚ {defensive_works=h…  β”‚ POINT (14.5250094 35.8883199)                β”‚
β”‚ node/10018742746 β”‚ {defibrillator:loc…  β”‚ POINT (14.5094082 35.8965151)                β”‚
β”‚ node/10018742747 β”‚ {amenity=bank, nam…  β”‚ POINT (14.51329 35.8991614)                  β”‚
β”‚ node/10032244899 β”‚ {amenity=restauran…  β”‚ POINT (14.4946298 35.8986226)                β”‚
β”‚ node/10034853491 β”‚ {amenity=pharmacy}   β”‚ POINT (14.4945884 35.9012758)                β”‚
β”‚       Β·          β”‚         Β·            β”‚               Β·                              β”‚
β”‚       Β·          β”‚         Β·            β”‚               Β·                              β”‚
β”‚       Β·          β”‚         Β·            β”‚               Β·                              β”‚
β”‚ way/884730763    β”‚ {highway=footway, …  β”‚ LINESTRING (14.5218277 35.8896022, 14.5218…  β”‚
β”‚ way/884730764    β”‚ {bridge=yes, highw…  β”‚ LINESTRING (14.5218054 35.8896015, 14.5218…  β”‚
β”‚ way/884730765    β”‚ {highway=footway, …  β”‚ LINESTRING (14.5204069 35.889924, 14.52044…  β”‚
β”‚ way/884730766    β”‚ {handrail=yes, hig…  β”‚ LINESTRING (14.5204375 35.8898663, 14.5204…  β”‚
β”‚ way/884730767    β”‚ {access=yes, handr…  β”‚ LINESTRING (14.5196113 35.8906142, 14.5196…  β”‚
β”‚ way/884730768    β”‚ {highway=steps, la…  β”‚ LINESTRING (14.5197226 35.890676, 14.51972…  β”‚
β”‚ way/884730769    β”‚ {access=yes, handr…  β”‚ LINESTRING (14.5197184 35.8906707, 14.5197…  β”‚
β”‚ way/884738591    β”‚ {highway=pedestria…  β”‚ LINESTRING (14.5204163 35.8897296, 14.5204…  β”‚
β”‚ way/884744870    β”‚ {highway=residenti…  β”‚ LINESTRING (14.5218931 35.8864046, 14.5221…  β”‚
β”‚ way/884744871    β”‚ {access=yes, handr…  β”‚ LINESTRING (14.5221083 35.8864287, 14.5221…  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ? rows (>9999 rows, 20 shown)                                                3 columns β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Use as CLI

$ quackosm --geom-filter-geocode "Shibuya, Tokyo"
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 45.7M/45.7M [00:00<00:00, 259GB/s]
β ¦ [   1/32] Reading nodes β€’ 0:00:05
β ‹ [   2/32] Filtering nodes - intersection β€’ 0:00:00
β ¦ [   3/32] Filtering nodes - tags β€’ 0:00:00
β ‹ [   4/32] Calculating distinct filtered nodes ids β€’ 0:00:00
⠏ [   5/32] Reading ways β€’ 0:00:04
β ¦ [   6/32] Unnesting ways β€’ 0:00:02
β ΄ [   7/32] Filtering ways - valid refs β€’ 0:00:02
β ™ [   8/32] Filtering ways - intersection β€’ 0:00:01
β Ή [   9/32] Filtering ways - tags β€’ 0:00:00
β ‹ [  10/32] Calculating distinct filtered ways ids β€’ 0:00:00
β ΄ [  11/32] Reading relations β€’ 0:00:00
β ΄ [  12/32] Unnesting relations β€’ 0:00:00
β Ή [  13/32] Filtering relations - valid refs β€’ 0:00:00
β ™ [  14/32] Filtering relations - intersection β€’ 0:00:00
β ™ [  15/32] Filtering relations - tags β€’ 0:00:00
β ‹ [  16/32] Calculating distinct filtered relations ids β€’ 0:00:00
β ™ [  17/32] Loading required ways - by relations β€’ 0:00:00
β ‹ [  18/32] Calculating distinct required ways ids β€’ 0:00:00
β ‹ [  19/32] Saving filtered nodes with geometries β€’ 0:00:00
β ‹ [20.1/32] Grouping filtered ways - assigning groups β€’ 0:00:00
β ΄ [20.2/32] Grouping filtered ways - joining with nodes β€’ 0:00:03
β Ή [20.3/32] Grouping filtered ways - partitioning by group β€’ 0:00:00
  [  21/32] Saving filtered ways with linestrings 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1 β€’ 0:00:01 < 0:00:00 β€’
β ‹ [22.1/32] Grouping required ways - assigning groups β€’ 0:00:00
β ΄ [22.2/32] Grouping required ways - joining with nodes β€’ 0:00:03
β ‹ [22.3/32] Grouping required ways - partitioning by group β€’ 0:00:00
  [  23/32] Saving required ways with linestrings 100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1 β€’ 0:00:00 < 0:00:00 β€’
β ‹ [  24/32] Saving filtered ways with geometries β€’ 0:00:01
β Έ [  25/32] Saving valid relations parts β€’ 0:00:00
β ‹ [26.1/32] Saving relations inner parts - valid geometries β€’ 0:00:00
β ‹ [26.2/32] Saving relations inner parts - invalid geometries β€’ 0:00:00
β ™ [27.1/32] Saving relations outer parts - valid geometries β€’ 0:00:00
β ‹ [27.2/32] Saving relations outer parts - invalid geometries β€’ 0:00:00
β ‹ [  28/32] Saving relations outer parts with holes β€’ 0:00:00
β ‹ [  29/32] Saving relations outer parts without holes β€’ 0:00:00
β ™ [  30/32] Saving filtered relations with geometries β€’ 0:00:00
β Έ [31.1/32] Saving valid features β€’ 0:00:00
β ‹ [  32/32] Saving final geoparquet file β€’ 0:00:00
Finished operation in 0:00:39
files/9ae2b160eb7556991148f5a2693aaf4b38bbb225c3700a6bfe9e5e54f48b987e_nofilter_compact.geoparquet

CLI Help output (QuackOSM -h): 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.

  1. Read nodes from the PBF file, save them to the parquet file.
    1. (Optional) Filter nodes based on geometry filter
    2. (Optional) Filter nodes based on tags filter
  2. Read ways from the PBF file, save them to the parquet file.
    1. Select all nodes refs and join them with previously read nodes.
    2. (Optional) Filter ways based on geometry filter - join intersecting nodes
    3. (Optional) Filter ways based on tags filter
  3. Read relations from the PBF file, save them to the parquet file.
    1. Select all ways refs and join them with previously read ways.
    2. (Optional) Filter relations based on geometry filter - join intersecting ways
    3. (Optional) Filter relations based on tags filter
  4. Select ways required by filtered relations
  5. Select nodes required by filtered and required ways
  6. Save filtered nodes with point geometries
  7. Group ways with nodes geometries and contruct linestrings
  8. Save filtered ways with linestrings and polygon geometries (depending on tags values)
  9. Divide relation parts into inner and outer polygons
  10. Group relation parts into full (multi)polygons and save them
  11. Fix invalid geometries
  12. Return final GeoParquet file

Geometry validation

You might ask a question: How do I know that these geometries are reconstructed correctly?

To answer this question, the QuackOSM has implemented dedicated tests that validate the results of GDAL geometries vs QuackOSM. This might come as a surprise, but since OSM geometries aren't always perfectly defined (especially relations), the QuackOSM can even fix geometries that are loaded with weird artifacts by GDAL.

You can inspect the comparison algorithm in the test_gdal_parity function from tests/base/test_pbf_file_reader.py file.

Caching

Library utilizes caching system to reduce repeatable computations.

By default, the library is saving results in the files directory created in the working directory. Result file name is generated based on the original *.osm.pbf file name.

Original file name to be converted: example.osm.pbf.

Default output without any filtering: example_nofilter_noclip_compact.geoparquet.

The nofilter part can be replaced by the hash of OSM tags provided for filtering. example_a9dd1c3c2e3d6a94354464e9a1a536ef44cca77eebbd882f48ca52799eb4ca91_noclip_exploded.geoparquet

The noclip part can be replaced by the hash of geometry used for filtering. example_nofilter_430020b6b1ba7bef8ea919b2fb4472dab2972c70a2abae253760a56c29f449c4_compact.geoparquet

The compact part can also take the form of exploded, it represents the form of OSM tags - either kept together in a single dictionary or split into columns.

When filtering by selecting individual features IDs, an additional hash based on those IDs is appended to the file. example_nofilter_noclip_compact_c740a1597e53ae8c5e98c5119eaa1893ddc177161afe8642addcbe54a6dc089d.geoparquet

When the keep_all_tags parameter is passed while filtering by OSM tags, and additional alltags component is added after the osm filter hash part. example_a9dd1c3c2e3d6a94354464e9a1a536ef44cca77eebbd882f48ca52799eb4ca91_alltags_noclip_compact.geoparquet

General schema of multiple segments that are concatenated together: pbf_file_name_(osm_filter_tags_hash_part/nofilter)(_alltags)_(clipping_geometry_hash_part/noclip)_(compact/exploded)(_filter_osm_ids_hash_part).geoparquet

If the WKT mode is turned on, then the result file will be saved with a parquet extension and a _wkt suffix.

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_group 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 AMD Ryzen 7 5800X CPU (16 threads, 3.8 GHz clock speed) and 24 GB of RAM.

Monaco

PBF file size: 525 KB

Geofabrik link

Monaco PBF file result

Estonia

PBF file size: 100 MB

Geofabrik link

Estonia PBF file result

Poland

PBF file size: 1.7 GB

Geofabrik link

Poland PBF file result

License

The library is distributed under Apache-2.0 License.

The free OpenStreetMap data, which is used for the development of QuackOSM, is licensed under the Open Data Commons Open Database License (ODbL) by the OpenStreetMap Foundation (OSMF).

Footnotes

  1. DuckDB Website ↩

  2. DuckDB Spatial extension repository ↩

  3. GeoParquet data format ↩

  4. Typer docs ↩