PBF File Reader¶
PBFFileReader can really quickly parse full OSM extract in the form of *.osm.pbf file.
It uses DuckDB with spatial extension to convert pbf files into geoparquet files without GDAL dependency.
Reader can filter objects by geometry and by OSM tags with option to split tags into columns or keep it as a single dictionary.
Caching strategy is implemented to reduce computations, but it can be overriden using ignore_cache parameter.
Download all buildings in Reykjavík, Iceland¶
Filtering the data with geometry and by tags, with tags in exploded form
import urllib.request
from quackosm import PbfFileReader, geocode_to_geometry
iceland_pbf_url = "https://download.geofabrik.de/europe/iceland-latest.osm.pbf"
iceland_pbf_file = "iceland.osm.pbf"
urllib.request.urlretrieve(iceland_pbf_url, iceland_pbf_file)
('iceland.osm.pbf', <http.client.HTTPMessage at 0x7f705c05afc0>)
reykjavik_geometry = geocode_to_geometry("Reykjavík, IS")
reykjavik_geometry
To filter out buildings, we will utilize format used also in the osmnx library: a dictionary with keys representing tag keys and values that could be a bool, string or a list of string.
By default, QuackOSM will return only the tags that are present in the passed filter.
In this example we will select all the buildings using { "building": True } filter and only building tag values will be present in the result.
reader = PbfFileReader(
geometry_filter=reykjavik_geometry, tags_filter={"building": True}
)
reykjavik_buildings_gpq = reader.convert_pbf_to_parquet("iceland.osm.pbf")
reykjavik_buildings_gpq
Finished operation in 0:00:15
PosixPath('files/iceland_ae99e3d9_4e8a6f65_exploded_sorted.parquet')
Read those features using DuckDB¶
import duckdb
connection = duckdb.connect()
connection.load_extension("parquet")
connection.load_extension("spatial")
features_relation = connection.read_parquet(str(reykjavik_buildings_gpq))
features_relation
┌────────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────┐
│ feature_id │ geometry │ building │
│ varchar │ geometry('ogc:crs84') │ varchar │
├────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────┤
│ way/61934038 │ POLYGON ((-21.8437438 64.1084884, -21.8437503 64.1082447, -21.8437548 64.1080761, -21.844211 64.1080784, -21.8442 64.1084907, -21.8437438 64.1084884)) │ yes │
│ way/61934018 │ POLYGON ((-21.8431866 64.1079564, -21.8431225 64.1079561, -21.843123 64.1079396, -21.8431273 64.1077785, -21.8431962 64.1077788, -21.843197 64.1077511, -21.843708 64.1077537, -21.8437072 64.1077839, -21.843764 64.1077841, -21.8437592 64.1079616, -21.8436976 64.1079613, -21.8436962 64.1080122, -21.8431852 64.1080096, -21.8431866 64.1079564)) │ apartments │
│ way/61933932 │ POLYGON ((-21.8434848 64.1070313, -21.8434268 64.1070543, -21.843394 64.1069969, -21.8432922 64.1070146, -21.843207 64.1070404, -21.8433233 64.1070904, -21.8432325 64.1070969, -21.8432383 64.1071307, -21.843187 64.1071575, -21.8432326 64.1071819, -21.8432242 64.1072204, -21.843299 64.1072354, -21.8433326 64.1072691, -21.8433761 64.107268, -21.843322 64.1073354, -21.8433933 64.1073462, -21.8434693 64.107349, -21.8435612 64.1073458, -21.8436249 64.1073376, -21.8435791 64.1072684, -21.8436211 64.1072707, -21.8436537 64.1072371, -21.8437317 64.1072233, -21.8437179 64.1071874, -21.8437759 64.1071601, -21.8437236 64.1071316, -21.8437386 64.1070967, -21.8436633 64.107085, -21.8436326 64.1070471, -21.8435497 64.1070556, -21.8434848 64.1070313)) │ church │
│ way/61934167 │ POLYGON ((-21.8443402 64.108232, -21.8443443 64.1080791, -21.8443535 64.1077336, -21.8443541 64.1077086, -21.8448455 64.1077111, -21.8448425 64.1078236, -21.8449022 64.1078239, -21.844893 64.1081703, -21.8447929 64.1081698, -21.8447912 64.1082342, -21.8443402 64.108232)) │ yes │
│ way/1346502222 │ POLYGON ((-21.8523431 64.1054147, -21.8520855 64.1051045, -21.8482221 64.1057163, -21.8484909 64.1060401, -21.8491877 64.1059297, -21.8491363 64.1058678, -21.8513297 64.1055207, -21.8513697 64.1055689, -21.8523431 64.1054147)) │ construction │
│ way/849242393 │ POLYGON ((-21.8477126 64.1052314, -21.8477865 64.1053217, -21.8480253 64.1052844, -21.848183 64.105477, -21.8479411 64.1055148, -21.8480191 64.10561, -21.8484656 64.1055403, -21.848156 64.1051621, -21.8477126 64.1052314)) │ yes │
│ way/204102373 │ POLYGON ((-21.8451786 64.1055617, -21.8456047 64.1056787, -21.84601 64.10579, -21.8459173 64.1058544, -21.8459857 64.1058727, -21.846112 64.1057827, -21.8461856 64.1058025, -21.846246 64.1057594, -21.8461874 64.1057437, -21.8463012 64.1056626, -21.8462285 64.1056428, -21.8461376 64.1057066, -21.8457298 64.1055958, -21.8457048 64.1055488, -21.8455401 64.1055041, -21.8454232 64.105516, -21.8452938 64.1054809, -21.8451786 64.1055617)) │ yes │
│ way/95141704 │ POLYGON ((-21.846527 64.1057239, -21.8463012 64.1056626, -21.8462285 64.1056428, -21.8460204 64.1055863, -21.8461907 64.1054668, -21.8466973 64.1056044, -21.846527 64.1057239)) │ residential │
│ way/95141705 │ POLYGON ((-21.846062 64.1060629, -21.8455547 64.1059271, -21.8457279 64.1058037, -21.8459173 64.1058544, -21.8459857 64.1058727, -21.8462352 64.1059395, -21.846062 64.1060629)) │ residential │
│ way/923188606 │ POLYGON ((-21.8471423 64.1058071, -21.847081 64.1058173, -21.8470434 64.1057739, -21.8471047 64.1057638, -21.8471423 64.1058071)) │ yes │
│ · │ · │ · │
│ · │ · │ · │
│ · │ · │ · │
│ way/283269196 │ POLYGON ((-21.773429 64.1482725, -21.7733649 64.1483421, -21.7732448 64.1483211, -21.7733089 64.1482514, -21.773429 64.1482725)) │ residential │
│ way/107501486 │ POLYGON ((-21.7731315 64.1483633, -21.7730259 64.1483624, -21.7730281 64.1483141, -21.7729758 64.1483137, -21.7729752 64.1483278, -21.7728957 64.1483271, -21.7728963 64.148313, -21.7728119 64.1483123, -21.7728165 64.1482084, -21.7731383 64.1482111, -21.7731315 64.1483633)) │ residential │
│ way/283269193 │ POLYGON ((-21.7740773 64.1482704, -21.7740476 64.1483026, -21.7739302 64.1482821, -21.7737365 64.1482482, -21.7737662 64.1482159, -21.773887 64.1482371, -21.7740773 64.1482704)) │ residential │
│ way/283269194 │ POLYGON ((-21.7739302 64.1482821, -21.7738993 64.1483157, -21.7737772 64.1482943, -21.7735857 64.1482608, -21.7736166 64.1482272, -21.7737365 64.1482482, -21.7739302 64.1482821)) │ residential │
│ way/283269195 │ POLYGON ((-21.7737772 64.1482943, -21.7737463 64.148328, -21.7736238 64.1483065, -21.773429 64.1482725, -21.7734599 64.1482388, -21.7735857 64.1482608, -21.7737772 64.1482943)) │ residential │
│ way/107501495 │ POLYGON ((-21.7736238 64.1483065, -21.7735929 64.1483401, -21.7734732 64.1483192, -21.77344 64.1483552, -21.7733649 64.1483421, -21.773429 64.1482725, -21.7736238 64.1483065)) │ residential │
│ way/107499216 │ POLYGON ((-21.7751584 64.1481514, -21.7748842 64.148111, -21.7748919 64.1481011, -21.7747868 64.1480857, -21.7748585 64.1479932, -21.7749851 64.1480118, -21.774953 64.1480533, -21.774986 64.1480582, -21.7750245 64.1480085, -21.7752418 64.1480405, -21.7752182 64.148071, -21.7752447 64.1480749, -21.7752019 64.1481301, -21.7751776 64.1481265, -21.7751584 64.1481514)) │ residential │
│ way/283269192 │ POLYGON ((-21.773887 64.1482371, -21.7739188 64.1482025, -21.7741702 64.1482465, -21.7741383 64.148281, -21.7740773 64.1482704, -21.773887 64.1482371)) │ residential │
│ way/107499188 │ POLYGON ((-21.7746576 64.1480871, -21.7742467 64.1480259, -21.7743296 64.14792, -21.7744309 64.1479347, -21.774448 64.1479635, -21.7746077 64.1479868, -21.7746506 64.1479684, -21.7747351 64.1479888, -21.7746576 64.1480871)) │ residential │
│ way/107499175 │ POLYGON ((-21.7747816 64.1478107, -21.7745081 64.1477712, -21.7745985 64.1476521, -21.7748729 64.1476917, -21.7748549 64.1477154, -21.7749338 64.1477268, -21.7748769 64.1478018, -21.7747971 64.1477903, -21.7747816 64.1478107)) │ residential │
└────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────┘
? rows (>9999 rows, 20 shown) 3 columns
Count all buildings¶
features_relation.count("feature_id")
┌───────────────────┐ │ count(feature_id) │ │ int64 │ ├───────────────────┤ │ 25464 │ └───────────────────┘
Keeping all the tags while filtering the data¶
To keep all of the tags present in the source data, we can use keep_all_tags parameter. That way we will still return only buildings, but with all of the tags attached.
By default, all of those tags will be kept in a single column as a dict.
reader.convert_pbf_to_geodataframe("iceland.osm.pbf", keep_all_tags=True)
Finished operation in 0:00:14
| tags | geometry | |
|---|---|---|
| feature_id | ||
| way/61934038 | {'addr:housenumber': '10', 'addr:postcode': '1... | POLYGON ((-21.84374 64.10849, -21.84375 64.108... |
| way/61934018 | {'addr:city': 'Reykjavík', 'addr:housenumber':... | POLYGON ((-21.84319 64.10796, -21.84312 64.107... |
| way/61933932 | {'addr:housenumber': '5', 'addr:postcode': '10... | POLYGON ((-21.84348 64.10703, -21.84343 64.107... |
| way/61934167 | {'addr:housenumber': '8', 'addr:postcode': '10... | POLYGON ((-21.84434 64.10823, -21.84434 64.108... |
| way/1346502222 | {'addr:city': 'Reykjavík', 'addr:housenumber':... | POLYGON ((-21.85234 64.10541, -21.85209 64.105... |
| ... | ... | ... |
| way/222355490 | {'addr:housenumber': '6', 'addr:postcode': '11... | POLYGON ((-21.69511 64.11597, -21.69516 64.115... |
| way/365759197 | {'building': 'industrial'} | POLYGON ((-21.70156 64.1055, -21.70186 64.1051... |
| relation/13506468 | {'addr:city': 'Reykjavík', 'addr:housenumber':... | POLYGON ((-21.69638 64.09985, -21.69636 64.099... |
| way/992249961 | {'building': 'yes'} | POLYGON ((-21.6986 64.09975, -21.69855 64.0997... |
| way/570226126 | {'building': 'yes'} | POLYGON ((-21.70041 64.08481, -21.70032 64.084... |
25464 rows × 2 columns
Download main roads for Estonia¶
Filtering the data only by tags, with tags in exploded form
highways_filter = {
"highway": [
"motorway",
"trunk",
"primary",
"secondary",
"tertiary",
]
}
estonia_pbf_url = "http://download.geofabrik.de/europe/estonia-latest.osm.pbf"
estonia_pbf_file = "estonia.osm.pbf"
urllib.request.urlretrieve(estonia_pbf_url, estonia_pbf_file)
reader = PbfFileReader(geometry_filter=None, tags_filter=highways_filter)
estonia_features_gpq = reader.convert_pbf_to_parquet(estonia_pbf_file)
estonia_features_gpq
Finished operation in 0:00:18
PosixPath('files/estonia_87e7b741_noclip_exploded_sorted.parquet')
features_relation = connection.read_parquet(str(estonia_features_gpq))
features_relation
┌────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────┐
│ feature_id │ geometry │ highway │
│ varchar │ geometry('ogc:crs84') │ varchar │
├────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────┤
│ way/441801965 │ LINESTRING (22.055825 57.9111269, 22.0560882 57.9113742, 22.0563349 57.9115746) │ tertiary │
│ way/1018216517 │ LINESTRING (22.0563349 57.9115746, 22.0565881 57.911771, 22.0567573 57.9119305, 22.0568909 57.912108, 22.0569823 57.9122961, 22.057069 57.9125329, 22.0571383 57.9127075) │ tertiary │
│ way/37439072 │ LINESTRING (22.0571383 57.9127075, 22.0571903 57.9127901, 22.0573036 57.9128767, 22.0573909 57.9129283, 22.0575834 57.9129985, 22.0576021 57.9130034, 22.0579829 57.9131031, 22.0581652 57.9131651, 22.0583127 57.913228, 22.0584328 57.9132951, 22.0585628 57.9133852, 22.0586936 57.9134988, 22.058829 57.9136461, 22.0594326 57.9143824, 22.0596083 57.9145685, 22.0597846 57.9147444, 22.060021 57.9149467, 22.0606738 57.9154739, 22.0613254 57.9160024, 22.06168 57.9162791, 22.0619472 57.9164677, 22.0621751 57.9166234, 22.0626801 57.9169592, 22.0630516 57.9171963, 22.0632575 57.9173115, 22.0634442 57.9174006, 22.0636074 57.9174747, 22.0637146 57.917547, 22.0638036 57.9176209, 22.0640832 57.9178738) │ tertiary │
│ way/1018216516 │ LINESTRING (22.0640832 57.9178738, 22.0642168 57.9179896, 22.0642584 57.918064, 22.0642581 57.9181382) │ tertiary │
│ way/232265919 │ LINESTRING (22.0409996 58.0039353, 22.0388314 58.002661, 22.03839 58.0025194, 22.0382184 58.0024692, 22.0380646 58.0024242, 22.0377136 58.0023198, 22.0370582 58.0021423, 22.0363056 58.0019396, 22.0344895 58.0014445, 22.0334132 58.0011464, 22.031798 58.0007169, 22.0312669 58.0005554, 22.0308003 58.0003958, 22.0299982 58.0000671, 22.0293673 57.9997562, 22.0286268 57.9993298, 22.0281676 57.9990479, 22.0281132 57.9990145, 22.0277882 57.998815, 22.0277412 57.9987861, 22.0256947 57.9975296, 22.0236413 57.9962663, 22.0217588 57.9951115, 22.0201022 57.9941243, 22.0194638 57.9937308, 22.0191012 57.9934553, 22.0189646 57.9932839, 22.0187496 57.9930071, 22.0185795 57.9927065, 22.018502 57.9924928, 22.0184525 57.9923238, 22.0184234 57.9921683, 22.0184128 57.9920286, 22.0184338 57.9918346, 22.0184889 57.9915953, 22.018589 57.9913278, 22.0192997 57.989674, 22.0197497 57.9886068, 22.0203113 57.9873026, 22.0207256 57.9863505, 22.0209716 57.9857848, 22.0210919 57.9854853, 22.0213358 57.9849278, 22.0215521 57.9843872, 22.0215806 57.9842631, 22.0216545 57.9839407, 22.0216902 57.9836398, 22.0217025 57.9834018, 22.0216823 57.9832162, 22.0215934 57.982903, 22.021419 57.9825155, 22.0211337 57.9819858, 22.0208339 57.9816209, 22.0205326 57.9813067, 22.0198611 57.9808164, 22.0189424 57.9802405, 22.018484 57.9799604, 22.0184485 57.9799387, 22.0180477 57.9796968, 22.0169012 57.9789891, 22.0144007 57.9774724, 22.0126276 57.9764101, 22.0114743 57.975706, 22.0107859 57.9752858, 22.0071663 57.9731103, 22.0061964 57.9724639, 22.0052121 57.9717478, 22.0039716 57.970712, 22.0029867 57.9697428, 22.00269 57.9694162, 22.0019228 57.968535, 22.0013785 57.9677904, 22.0003182 57.9662021, 21.9992996 57.9646586, 21.9972166 57.9615807, 21.9971443 57.9614702, 21.9967701 57.9608983, 21.996616 57.9604841, 21.9964811 57.9599436, 21.9964489 57.9593717, 21.9965829 57.9584759, 21.9967887 57.9575496, 21.9971487 57.9556953, 21.9974206 57.9543359, 21.997672 57.9530652, 21.9976949 57.9529393, 21.9977519 57.9526592, 21.9977619 57.9526102, 21.997783 57.9525061, 21.997791 57.9524667, 21.997965 57.9516116, 21.9980774 57.9511991, 21.9982022 57.9509179, 21.9983111 57.9507133, 21.9986802 57.9501513, 21.9990458 57.9496884, 21.9995387 57.9492148, 22.0000886 57.9487813, 22.0002683 57.9486511, 22.0004491 57.9485276, 22.0008378 57.9482893, 22.0016256 57.9478062, 22.0039493 57.946387, 22.0067603 57.9446678, 22.0080223 57.9438992, 22.0081881 57.9437989, 22.0106623 57.9422877, 22.0117012 57.9416498, 22.0123896 57.941114, 22.0128118 57.9407148, 22.0133024 57.9401891, 22.0134488 57.9399937, 22.0140052 57.9391042, 22.0145747 57.9381741, 22.0149037 57.9376662, 22.0152858 57.9371623, 22.0164393 57.9358291, 22.0173204 57.9348554, 22.0181691 57.9339626, 22.0184151 57.9338105, 22.0188136 57.9336245, 22.0251493 57.9310279, 22.0348016 57.9271172, 22.0375506 57.926033, 22.0445412 57.9237122, 22.0468228 57.9229301, 22.0473732 57.9227291, 22.0476643 57.9225905, 22.0482686 57.9222757, 22.0491394 57.9217129, 22.0506239 57.9207264, 22.0512772 57.9202941, 22.051932 57.91989, 22.0524278 57.9196645, 22.052747 57.919569, 22.0530308 57.9195005, 22.0534041 57.9194237, 22.0540667 57.9193325, 22.0574948 57.9189141, 22.0596265 57.9186637, 22.0618148 57.9183929, 22.0636563 57.9181602, 22.0639902 57.918131, 22.0642581 57.9181382, 22.0644605 57.9181695, 22.0647002 57.9182452, 22.0649041 57.9183419, 22.0662254 57.9190668, 22.0667118 57.9193563, 22.067059 57.9195838, 22.0673697 57.9198141, 22.0676331 57.9200253, 22.0679073 57.9202593, 22.0681937 57.9205324, 22.0687993 57.9210981, 22.069128 57.9214281, 22.0704011 57.9228595, 22.0710745 57.9235712, 22.0717436 57.9242615, 22.0723043 57.9247631, 22.0728413 57.9251791, 22.0733455 57.9255487, 22.0736327 57.9257744, 22.0738081 57.9258917, 22.0740418 57.926021, 22.0743881 57.9261976, 22.074814 57.9263984, 22.0753661 57.9266451, 22.0757039 57.9267793, 22.0761404 57.9269525, 22.0769401 57.927235, 22.0789279 57.9278726, 22.0807069 57.9283837, 22.082883 57.928978, 22.0838908 57.9292754, 22.0847777 57.9295613, 22.0853585 57.929765, 22.0865895 57.9302597, 22.0874274 57.9306336, 22.0920971 57.9330818, 22.0952889 57.9347543, 22.097774 57.9359314, 22.1000961 57.9370645, 22.1030526 57.9387145, 22.1039535 57.9391836, 22.1049281 57.9396676, 22.1061235 57.9402157, 22.1068671 57.9405925, 22.107055 57.9407084, 22.1071949 57.9408593, 22.1076188 57.9415401, 22.1078304 57.941831, 22.1081511 57.9421122, 22.1086647 57.9424304, 22.1093062 57.9427187, 22.1114706 57.9434859, 22.1122651 57.9438023, 22.1152236 57.9453406, 22.1155635 57.9455581, 22.1158115 57.945766, 22.1160467 57.9460158, 22.1187927 57.9495541, 22.1191165 57.9499892, 22.1195952 57.9505917, 22.1198863 57.9508971, 22.12105 57.9520537, 22.121246 57.9523306, 22.1213243 57.9524814, 22.1213918 57.9528157, 22.1213298 57.9532003, 22.1212331 57.9537161, 22.1211679 57.954094, 22.1211802 57.9542294, 22.1211967 57.9543243, 22.1212517 57.9544167, 22.1213011 57.9544855, 22.1214316 57.9545775, 22.1216311 57.9547049, 22.1219175 57.9548877, 22.1220815 57.9550373, 22.1221647 57.955143, 22.122546 57.9558138, 22.1227025 57.9560634, 22.1239156 57.957798, 22.1241603 57.9582011, 22.1246046 57.9588622, 22.1249054 57.9593072, 22.1257003 57.9601034, 22.12679 57.9611765, 22.1273795 57.9620948, 22.1280326 57.9631733, 22.1283243 57.9638429, 22.128603 57.9645727, 22.128952 57.965223, 22.1319977 57.9684531, 22.1334987 57.9698068, 22.1346931 57.9711443, 22.1349226 57.9715106) │ tertiary │
│ way/1018220952 │ LINESTRING (22.1068474 57.9623483, 22.1065086 57.962407, 22.1059957 57.9625149, 22.1036583 57.9631311, 22.1029824 57.9633444, 22.1020228 57.9636793, 22.1016093 57.9638515, 22.1009866 57.964135, 22.0999176 57.9646581, 22.0910779 57.969399, 22.0888238 57.970641, 22.0884304 57.9708519, 22.08696 57.9716403, 22.0842819 57.973056, 22.0838175 57.9733065, 22.0826777 57.9738046, 22.0815755 57.9742472) │ tertiary │
│ way/1018220953 │ LINESTRING (22.0608019 57.978236, 22.0539617 57.9799574, 22.0538724 57.9799799, 22.0535175 57.9800692) │ tertiary │
│ way/1018220951 │ LINESTRING (22.0622446 57.9779054, 22.0617261 57.9780081, 22.0608019 57.978236) │ tertiary │
│ way/4937123 │ LINESTRING (22.0793018 57.9750696, 22.0783812 57.9753754, 22.0777085 57.9756406, 22.0765658 57.9761355, 22.0755268 57.9765237, 22.0749313 57.9767099, 22.0742608 57.9768686, 22.0735309 57.97699, 22.0709656 57.9773805, 22.0701591 57.9774945, 22.0693971 57.9775859, 22.0690844 57.9776128, 22.0687346 57.9776384, 22.0684389 57.9776509, 22.0677736 57.977668, 22.0641958 57.9777281, 22.0640831 57.97773, 22.0637818 57.9777413, 22.0636651 57.9777457, 22.0632177 57.9777826, 22.0627804 57.9778269, 22.0622446 57.9779054) │ tertiary │
│ way/1018220950 │ LINESTRING (22.0815755 57.9742472, 22.0793018 57.9750696) │ tertiary │
│ · │ · │ · │
│ · │ · │ · │
│ · │ · │ · │
│ way/1352078531 │ LINESTRING (24.5683358 59.3194304, 24.5681957 59.3196543, 24.5680015 59.3199642, 24.5679016 59.3200938) │ tertiary │
│ way/1352078530 │ LINESTRING (24.5679016 59.3200938, 24.5677615 59.3202526, 24.5674239 59.3206353, 24.5671119 59.3209469) │ tertiary │
│ way/1352078529 │ LINESTRING (24.5671119 59.3209469, 24.567059 59.321004) │ tertiary │
│ way/413676968 │ LINESTRING (24.5857528 59.3206673, 24.5842648 59.3196376, 24.5819301 59.3180115, 24.5812145 59.3175157) │ trunk │
│ way/413676967 │ LINESTRING (24.5801448 59.3164507, 24.5818218 59.317614) │ trunk │
│ way/413676973 │ LINESTRING (24.5812145 59.3175157, 24.5789449 59.3159342) │ trunk │
│ way/1331983448 │ LINESTRING (24.5792405 59.3158173, 24.5801448 59.3164507) │ trunk │
│ way/352240971 │ LINESTRING (24.6168163 59.3184729, 24.6146983 59.3169316, 24.6140618 59.3164603, 24.6137219 59.3162238, 24.6118778 59.3150404, 24.6114347 59.314764, 24.6105461 59.3142488, 24.6101305 59.3140082, 24.6096758 59.3137599, 24.6095568 59.3136949, 24.6093984 59.313612) │ tertiary │
│ way/1320429869 │ LINESTRING (24.6103622 59.3132723, 24.6073298 59.3129358) │ trunk │
│ way/1321770601 │ LINESTRING (24.607368 59.3128169, 24.6104001 59.3131573) │ trunk │
└────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────┘
? rows (>9999 rows, 20 shown) 3 columns
Count loaded roads¶
features_relation.count("feature_id")
┌───────────────────┐ │ count(feature_id) │ │ int64 │ ├───────────────────┤ │ 29933 │ └───────────────────┘
length_in_meters = (
features_relation.project(
"ST_Length(ST_Transform(geometry, 'EPSG:4326', 'EPSG:3301')) AS road_length"
)
.sum("road_length")
.fetchone()[0]
)
length_in_km = length_in_meters / 1000
length_in_km
29744.572179755753
Plot the roads using GeoPandas¶
With fast loading of geoparquet files using geoarrow.pyarrow library.
import geoarrow.pyarrow as ga
from geoarrow.pyarrow import io
from quackosm._constants import GEOMETRY_COLUMN
parquet_table = io.read_geoparquet_table(estonia_features_gpq)
ga.to_geopandas(parquet_table.column(GEOMETRY_COLUMN)).plot()
<Axes: >
Download all data for Liechtenstein¶
Without filtering, with tags in a compact form
liechtenstein_pbf_url = "https://download.geofabrik.de/europe/liechtenstein-latest.osm.pbf"
liechtenstein_pbf_file = "liechtenstein.osm.pbf"
urllib.request.urlretrieve(liechtenstein_pbf_url, liechtenstein_pbf_file)
# Here explode_tags is set to False explicitly,
# but it would set automatically when not filtering the data
reader = PbfFileReader(geometry_filter=None, tags_filter=None)
liechtenstein_features_gpq = reader.convert_pbf_to_parquet(
liechtenstein_pbf_file, explode_tags=False
)
liechtenstein_features_gpq
Finished operation in 0:00:05
PosixPath('files/liechtenstein_nofilter_noclip_compact_sorted.parquet')
features_relation = connection.read_parquet(str(liechtenstein_features_gpq))
features_relation
┌─────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ feature_id │ tags │ geometry │
│ varchar │ map(varchar, varchar) │ geometry('ogc:crs84') │
├─────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ node/540308211 │ {power=tower} │ POINT (9.5372399 46.9862797) │
│ node/1338490716 │ {power=tower} │ POINT (9.5342839 46.9887559) │
│ node/540308206 │ {power=tower} │ POINT (9.5313353 46.9912103) │
│ node/540308180 │ {power=tower} │ POINT (9.5285455 46.9936492) │
│ node/540308170 │ {power=tower} │ POINT (9.5257615 46.9960867) │
│ node/540308159 │ {line_attachment=anchor, power=tower} │ POINT (9.5231821 46.998349) │
│ way/160527579 │ {highway=path, surface=gravel} │ LINESTRING (9.4763856 47.0521369, 9.4764306 47.0519574, 9.476491 47.0518671, 9.4766172 47.0517149, 9.476662 47.0516592, 9.4766745 47.0515936, 9.4767745 47.0514237, 9.4768773 47.0512946, 9.4769524 47.0511758, 9.477043 47.0510712, 9.4770946 47.050993, 9.4772689 47.0507445, 9.4773317 47.0505664, 9.4774119 47.0503482, 9.4774255 47.0502526, 9.4774889 47.0501908, 9.4775274 47.0500558, 9.477582 47.0499795, 9.4776337 47.0498801, 9.4776686 47.0498161, 9.4778721 47.0497533) │
│ way/122990817 │ {admin_level=4, boundary=administrative} │ LINESTRING (9.4772452 47.049055, 9.4757865 47.0517524) │
│ node/1725816418 │ {barrier=bollard} │ POINT (9.4763856 47.0521369) │
│ way/1214145501 │ {'cycleway:both'=no, highway=unclassified, lane_markings=no, name=Rheindamm, smoothness=excellent, surface=asphalt, width=4} │ LINESTRING (9.476461 47.0524842, 9.4766379 47.0521002) │
│ · │ · │ · │
│ · │ · │ · │
│ · │ · │ · │
│ way/515588779 │ {highway=service} │ LINESTRING (9.5275345 47.1230629, 9.5270525 47.1230291) │
│ way/346498871 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=4, 'addr:postcode'=9495, 'addr:street'=Meierhofstrasse, building=yes} │ POLYGON ((9.5271413 47.1227414, 9.5271488 47.1226949, 9.527001 47.1226838, 9.5269815 47.1228048, 9.5271295 47.1228158, 9.5271387 47.1227589, 9.5271554 47.1227601, 9.5271582 47.1227426, 9.5271413 47.1227414)) │
│ way/346498805 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=8, 'addr:postcode'=9495, 'addr:street'=Wingarten, building=yes} │ POLYGON ((9.5268989 47.1226295, 9.5270815 47.1226153, 9.5270648 47.1225157, 9.5268822 47.1225298, 9.5268989 47.1226295)) │
│ node/3530177944 │ {natural=tree} │ POINT (9.5269526 47.1226761) │
│ way/346498879 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=2, 'addr:postcode'=9495, 'addr:street'=Meierhofstrasse, building=yes} │ POLYGON ((9.5271086 47.1229914, 9.5271146 47.1229346, 9.5271332 47.1229355, 9.5271355 47.1229136, 9.5271167 47.1229127, 9.5271216 47.122866, 9.5269768 47.1228589, 9.5269634 47.1229842, 9.5271086 47.1229914)) │
│ node/3530178094 │ {natural=tree} │ POINT (9.5269953 47.1232545) │
│ node/3530178099 │ {natural=tree} │ POINT (9.5269856 47.1233264) │
│ way/515592656 │ {'addr:city'=Vaduz, 'addr:country'=LI, 'addr:housenumber'=72, 'addr:postcode'=9490, 'addr:street'=Meierhofstrasse, building=yes} │ POLYGON ((9.5271514 47.1232735, 9.5270355 47.1232746, 9.5270364 47.1231985, 9.5271091 47.1231989, 9.5271098 47.123143, 9.527082 47.1231428, 9.5270826 47.1230886, 9.5272122 47.1230892, 9.5272122 47.123199, 9.5271514 47.123199, 9.5271514 47.1232735)) │
│ node/3530178108 │ {natural=tree} │ POINT (9.5270369 47.1234397) │
│ way/140678738 │ {'cycleway:both'=no, highway=secondary, lanes=2, lit=yes, maxspeed=50, name=Meierhofstrasse, oneway=no, sidewalk=separate, 'sidewalk:both:surface'=asphalt, smoothness=good, surface=asphalt} │ LINESTRING (9.5285313 47.1224637, 9.5285126 47.1224933, 9.5284882 47.1225319, 9.5283407 47.1227629, 9.5282293 47.1229407, 9.5281782 47.1230207, 9.5281465 47.1230718, 9.527923 47.1234242, 9.5278254 47.1235809, 9.5277232 47.123737, 9.5275485 47.1240148, 9.5274903 47.1241074, 9.527208 47.1245508, 9.5270177 47.1248501, 9.5265507 47.1255862, 9.5263964 47.125826, 9.5262035 47.126106, 9.5260664 47.1262807) │
└─────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
? rows (>9999 rows, 20 shown) 3 columns
Return data as GeoDataFrame¶
PbfFileReader can also return the data in the GeoDataFrame form.
Here the caching strategy will be utilized - file won't be transformed again.
features_gdf = reader.convert_pbf_to_geodataframe(liechtenstein_pbf_file)
features_gdf
| tags | geometry | |
|---|---|---|
| feature_id | ||
| node/540308211 | {'power': 'tower'} | POINT (9.53724 46.98628) |
| node/1338490716 | {'power': 'tower'} | POINT (9.53428 46.98876) |
| node/540308206 | {'power': 'tower'} | POINT (9.53134 46.99121) |
| node/540308180 | {'power': 'tower'} | POINT (9.52855 46.99365) |
| node/540308170 | {'power': 'tower'} | POINT (9.52576 46.99609) |
| ... | ... | ... |
| way/439890023 | {'natural': 'cliff'} | LINESTRING (9.60433 47.06468, 9.60382 47.06464... |
| way/1195535494 | {'natural': 'grassland'} | POLYGON ((9.60181 47.06237, 9.60136 47.06238, ... |
| node/540308216 | {'line_attachment': 'suspension', 'power': 'to... | POINT (9.54003 46.98396) |
| node/1110282277 | {'power': 'tower'} | POINT (9.5405 46.98211) |
| node/2186283697 | {'power': 'tower'} | POINT (9.54143 46.97844) |
61350 rows × 2 columns
Plot the forests using GeoPandas¶
Filter all polygons and features with landuse=forest.
features_gdf[
features_gdf.geom_type.isin(("Polygon", "MultiPolygon"))
& features_gdf.tags.apply(lambda x: "landuse" in x and x["landuse"] == "forest")
].plot(color="green")
<Axes: >