QuackOSM Basic Usage¶
QuackOSM exposes some basic functions in the main Python module. Full documentation for them is available here.
This notebook will show how to use the library in a few simple scenarios.
To learn more about CLI
usage, see this page. The help
page for the CLI is available here.
To learn more details about PbfFileReader
class, see this page, or documentation page.
In [1]:
Copied!
import quackosm as qosm
import quackosm as qosm
Reading existing PBF file to GeoDataFrame¶
In [2]:
Copied!
qosm.convert_pbf_to_geodataframe("https://download.geofabrik.de/europe/monaco-latest.osm.pbf")
qosm.convert_pbf_to_geodataframe("https://download.geofabrik.de/europe/monaco-latest.osm.pbf")
Out[2]:
tags | geometry | |
---|---|---|
feature_id | ||
node/1097207537 | {'leaf_type': 'broadleaved', 'natural': 'tree'} | POINT (7.42768 43.74229) |
node/1097207541 | {'leaf_type': 'broadleaved', 'natural': 'tree'} | POINT (7.42782 43.74251) |
node/1097219229 | {'natural': 'tree'} | POINT (7.41853 43.73709) |
node/1097219236 | {'natural': 'tree'} | POINT (7.41905 43.7369) |
node/1097219254 | {'natural': 'tree'} | POINT (7.41305 43.73264) |
... | ... | ... |
relation/16261416 | {'natural': 'water', 'type': 'multipolygon', '... | POLYGON ((7.41783 43.73139, 7.41788 43.73139, ... |
relation/18306231 | {'natural': 'water', 'type': 'multipolygon'} | POLYGON ((7.42716 43.7401, 7.42714 43.74011, 7... |
relation/18406453 | {'name': 'Belvedere Gerard Brianti', 'place': ... | POLYGON ((7.43221 43.74259, 7.43232 43.7425, 7... |
relation/18406454 | {'name': 'Place Princesse Gabriela', 'place': ... | POLYGON ((7.43224 43.74119, 7.43219 43.74106, ... |
relation/11484092 | {'building': 'hotel', 'type': 'multipolygon'} | POLYGON ((7.4179 43.72483, 7.41783 43.72486, 7... |
9290 rows × 2 columns
Transforming existing PBF file to GeoParquet¶
In [3]:
Copied!
qosm.convert_pbf_to_parquet("https://download.geofabrik.de/europe/monaco-latest.osm.pbf")
qosm.convert_pbf_to_parquet("https://download.geofabrik.de/europe/monaco-latest.osm.pbf")
Out[3]:
PosixPath('files/monaco-latest_nofilter_noclip_compact.parquet')
In [4]:
Copied!
qosm.convert_osm_extract_to_geodataframe("Vatican City")
qosm.convert_osm_extract_to_geodataframe("Vatican City")
Out[4]:
tags | geometry | |
---|---|---|
feature_id | ||
node/1398913876 | {'barrier': 'lift_gate', 'foot': 'yes'} | POINT (12.4457 41.9042) |
node/1398913892 | {'barrier': 'gate'} | POINT (12.44479 41.9044) |
node/1402282620 | {'addr:housenumber': '15', 'addr:street': 'Vic... | POINT (12.45357 41.89592) |
node/1405099351 | {'historic': 'wayside_shrine', 'name': 'Ave Ma... | POINT (12.45243 41.89687) |
node/1410071558 | {'bench': 'no', 'bin': 'no', 'bus': 'yes', 'hi... | POINT (12.45143 41.89973) |
... | ... | ... |
relation/11839271 | {'building': 'yes', 'type': 'multipolygon'} | POLYGON ((12.44939 41.89759, 12.44953 41.89757... |
relation/13399080 | {'highway': 'pedestrian', 'lit': 'yes', 'name'... | POLYGON ((12.45775 41.90339, 12.45776 41.90354... |
relation/15843461 | {'artwork_type': 'architecture', 'surface': 'p... | POLYGON ((12.45807 41.90213, 12.45806 41.90207... |
relation/15846626 | {'building': 'apartments', 'type': 'multipolyg... | POLYGON ((12.45946 41.90431, 12.4594 41.9039, ... |
relation/1535934 | {'building': 'yes', 'type': 'multipolygon'} | POLYGON ((12.45942 41.89792, 12.45948 41.89793... |
8452 rows × 2 columns
Find an OSM PBF extract file using text and transform it to a GeoParquet¶
In [5]:
Copied!
qosm.convert_osm_extract_to_parquet("Vatican City")
qosm.convert_osm_extract_to_parquet("Vatican City")
Out[5]:
PosixPath('files/osmfr_europe_vatican_city_nofilter_noclip_compact.parquet')
Get OSM data for a given geometry as a GeoDataFrame¶
In [6]:
Copied!
area = qosm.geocode_to_geometry('Shibuya, Tokyo')
qosm.convert_geometry_to_geodataframe(area)
area = qosm.geocode_to_geometry('Shibuya, Tokyo')
qosm.convert_geometry_to_geodataframe(area)
Out[6]:
tags | geometry | |
---|---|---|
feature_id | ||
node/307397021 | {'highway': 'traffic_signals', 'name': '神宮前一丁目... | POINT (139.70805 35.67204) |
node/307410334 | {'highway': 'traffic_signals', 'name': '原宿幼稚園前... | POINT (139.71371 35.67055) |
node/307410347 | {'amenity': 'restaurant', 'name': 'えさき', 'name... | POINT (139.71317 35.6717) |
node/307423644 | {'crossing': 'traffic_signals', 'crossing:mark... | POINT (139.70711 35.6745) |
node/308925199 | {'highway': 'motorway_junction', 'name': '西新宿J... | POINT (139.68239 35.68896) |
... | ... | ... |
way/970382833 | {'highway': 'steps'} | LINESTRING (139.69427 35.65535, 139.69435 35.6... |
way/970382834 | {'footway': 'sidewalk', 'highway': 'footway'} | LINESTRING (139.69435 35.6554, 139.69432 35.65... |
way/971255555 | {'natural': 'wood'} | POLYGON ((139.70295 35.66808, 139.70298 35.668... |
way/971255556 | {'natural': 'wood'} | POLYGON ((139.70377 35.66747, 139.70307 35.666... |
way/971255557 | {'building': 'yes'} | POLYGON ((139.70394 35.667, 139.70384 35.6669,... |
50042 rows × 2 columns
Save OSM data for a given geometry as a GeoParquet¶
In [7]:
Copied!
qosm.convert_geometry_to_parquet(area)
qosm.convert_geometry_to_parquet(area)
Out[7]:
PosixPath('files/3c3cbc5081381e8da561a9c97617ef045ae66728f2e5db2dd06a865e5af608cd_nofilter_compact.parquet')
More advanced examples¶
Filter out data based on geometry from existing PBF file¶
In [8]:
Copied!
area = qosm.geocode_to_geometry("Monaco-Ville, Monaco")
gdf = qosm.convert_pbf_to_geodataframe(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", geometry_filter=area
)
gdf
area = qosm.geocode_to_geometry("Monaco-Ville, Monaco")
gdf = qosm.convert_pbf_to_geodataframe(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", geometry_filter=area
)
gdf
Out[8]:
tags | geometry | |
---|---|---|
feature_id | ||
node/25181806 | {'crossing': 'uncontrolled', 'crossing_ref': '... | POINT (7.42238 43.72994) |
node/25181829 | {'crossing': 'uncontrolled', 'crossing_ref': '... | POINT (7.4228 43.72985) |
node/25191432 | {'access': 'yes', 'addr:country': 'MC', 'ameni... | POINT (7.42711 43.73128) |
node/25193663 | {'crossing': 'marked', 'crossing:markings': 'y... | POINT (7.4201 43.73228) |
node/256411950 | {'crossing': 'uncontrolled', 'crossing_ref': '... | POINT (7.42223 43.73004) |
... | ... | ... |
node/6458602915 | {'amenity': 'recycling', 'location': 'undergro... | POINT (7.4182 43.73186) |
node/6474611451 | {'noexit': 'yes'} | POINT (7.41876 43.73122) |
node/6474714941 | {'entrance': 'yes', 'level': '0'} | POINT (7.42225 43.73014) |
node/6474714954 | {'entrance': 'yes', 'level': '1'} | POINT (7.42225 43.73015) |
node/6480060630 | {'barrier': 'kerb', 'kerb': 'flush'} | POINT (7.42201 43.73204) |
923 rows × 2 columns
Plot downloaded data
In [9]:
Copied!
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import geopandas as gpd
fig = plt.figure(figsize=(10, 10))
ax = fig.subplots()
gdf.plot(ax=ax, markersize=1, zorder=1, alpha=0.4)
gdf.boundary.plot(ax=ax, markersize=0, zorder=1, alpha=0.8)
gpd.GeoSeries([area], crs=4326).plot(
ax=ax,
color=(0, 0, 0, 0),
zorder=2,
hatch="///",
edgecolor="orange",
linewidth=1.5,
)
blue_patch = mpatches.Patch(color="C0", alpha=0.8, label="OSM features")
orange_patch = mpatches.Patch(
facecolor=(0, 0, 0, 0), edgecolor="orange", hatch="///", linewidth=1.5, label="Geometry filter"
)
ax.legend(handles=[blue_patch, orange_patch], loc="lower right")
plt.show()
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import geopandas as gpd
fig = plt.figure(figsize=(10, 10))
ax = fig.subplots()
gdf.plot(ax=ax, markersize=1, zorder=1, alpha=0.4)
gdf.boundary.plot(ax=ax, markersize=0, zorder=1, alpha=0.8)
gpd.GeoSeries([area], crs=4326).plot(
ax=ax,
color=(0, 0, 0, 0),
zorder=2,
hatch="///",
edgecolor="orange",
linewidth=1.5,
)
blue_patch = mpatches.Patch(color="C0", alpha=0.8, label="OSM features")
orange_patch = mpatches.Patch(
facecolor=(0, 0, 0, 0), edgecolor="orange", hatch="///", linewidth=1.5, label="Geometry filter"
)
ax.legend(handles=[blue_patch, orange_patch], loc="lower right")
plt.show()
In [10]:
Copied!
area = qosm.geocode_to_geometry("Barcelona")
gdf = qosm.convert_geometry_to_geodataframe(
area, tags_filter={"amenity": "bicycle_rental"}
)
gdf
area = qosm.geocode_to_geometry("Barcelona")
gdf = qosm.convert_geometry_to_geodataframe(
area, tags_filter={"amenity": "bicycle_rental"}
)
gdf
Out[10]:
amenity | geometry | |
---|---|---|
feature_id | ||
node/1938341162 | bicycle_rental | POINT (2.19831 41.42222) |
node/1948991828 | bicycle_rental | POINT (2.19181 41.38931) |
node/1948992533 | bicycle_rental | POINT (2.19233 41.38856) |
node/1948993201 | bicycle_rental | POINT (2.1907 41.39066) |
node/2827630926 | bicycle_rental | POINT (2.14812 41.37413) |
... | ... | ... |
node/5060672224 | bicycle_rental | POINT (2.17343 41.37527) |
node/5731954321 | bicycle_rental | POINT (2.15308 41.37759) |
node/9851051918 | bicycle_rental | POINT (2.14222 41.38291) |
node/11290488468 | bicycle_rental | POINT (2.18526 41.38572) |
node/11290681161 | bicycle_rental | POINT (2.17881 41.37747) |
514 rows × 2 columns
Show downloaded data on a map
In [11]:
Copied!
m = gdf.explore(color="orangered", tiles="CartoDB positron")
gpd.GeoSeries([area], crs=4326).boundary.explore(m=m)
m = gdf.explore(color="orangered", tiles="CartoDB positron")
gpd.GeoSeries([area], crs=4326).boundary.explore(m=m)
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Save the result GeoParquet with WKT geometry¶
In [12]:
Copied!
qosm.convert_pbf_to_parquet(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", save_as_wkt=True
)
qosm.convert_pbf_to_parquet(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", save_as_wkt=True
)
Out[12]:
PosixPath('files/monaco-latest_nofilter_noclip_compact_wkt.parquet')
Specify result file path¶
In [13]:
Copied!
qosm.convert_geometry_to_parquet(
area, result_file_path='barcelona_osm_output.parquet'
)
qosm.convert_geometry_to_parquet(
area, result_file_path='barcelona_osm_output.parquet'
)
Out[13]:
PosixPath('barcelona_osm_output.parquet')
Force recalculation of the result¶
By default, running the same command twice will result in reusing the saved GeoParquet file. You can force QuackOSM to recalculate the data.
In [14]:
Copied!
qosm.convert_pbf_to_parquet(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", ignore_cache=True
)
qosm.convert_pbf_to_parquet(
"https://download.geofabrik.de/europe/monaco-latest.osm.pbf", ignore_cache=True
)
Finished operation in 0:00:04
Out[14]:
PosixPath('files/monaco-latest_nofilter_noclip_compact.parquet')