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 0x7f61fcb476b0>)
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:19
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 │ 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/107499296 │ POLYGON ((-21.7761242 64.1464504, -21.7757389 64.1464324, -21.7757639 64.1463304, -21.7758411 64.146334, -21.7758569 64.1462695, -21.7760925 64.1462805, -21.7760762 64.1463471, -21.7761487 64.1463505, -21.7761242 64.1464504)) │ residential │ │ way/107499263 │ POLYGON ((-21.7764388 64.1460403, -21.7760522 64.1460214, -21.7760782 64.1459203, -21.7761569 64.1459241, -21.7761739 64.1458582, -21.7764086 64.1458697, -21.7763911 64.1459377, -21.7764643 64.1459413, -21.7764388 64.1460403)) │ residential │ │ way/107499211 │ POLYGON ((-21.777345 64.1460877, -21.7769605 64.146069, -21.7769871 64.1459652, -21.7770648 64.145969, -21.7770811 64.1459053, -21.7773149 64.1459167, -21.7772981 64.1459821, -21.7773712 64.1459856, -21.777345 64.1460877)) │ residential │ │ way/107499191 │ POLYGON ((-21.7757411 64.1455954, -21.7756575 64.1457061, -21.7757288 64.1457164, -21.7757386 64.1457035, -21.7757862 64.1457103, -21.7758601 64.1456125, -21.7757411 64.1455954)) │ residential │ │ way/107499218 │ POLYGON ((-21.7755203 64.1458898, -21.7754103 64.145874, -21.7753376 64.1459708, -21.7753777 64.1459765, -21.7753691 64.145988, -21.7754389 64.145998, -21.7755203 64.1458898)) │ residential │ │ way/107499226 │ POLYGON ((-21.7755203 64.1458898, -21.7754389 64.145998, -21.7755101 64.1460082, -21.7755187 64.1459968, -21.7755643 64.1460033, -21.7756371 64.1459065, -21.7755203 64.1458898)) │ residential │ │ way/107499289 │ POLYGON ((-21.7754103 64.145874, -21.7753035 64.1458588, -21.7752224 64.1459667, -21.7752884 64.1459761, -21.7752968 64.145965, -21.7753376 64.1459708, -21.7754103 64.145874)) │ residential │ │ way/107499224 │ POLYGON ((-21.7753035 64.1458588, -21.7751945 64.1458432, -21.7751209 64.1459411, -21.7751606 64.1459468, -21.7751531 64.1459568, -21.7752224 64.1459667, -21.7753035 64.1458588)) │ residential │ │ way/107499300 │ POLYGON ((-21.7757411 64.1455954, -21.7756335 64.14558, -21.7755599 64.1456775, -21.7755993 64.1456831, -21.7755893 64.1456964, -21.7756575 64.1457061, -21.7757411 64.1455954)) │ residential │ │ way/107499268 │ POLYGON ((-21.7756335 64.14558, -21.775525 64.1455644, -21.7754422 64.1456742, -21.7755097 64.1456838, -21.7755189 64.1456716, -21.7755599 64.1456775, -21.7756335 64.14558)) │ residential │ ├────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────┤ │ ? rows (>9999 rows, 20 shown) 3 columns │ └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Count all buildings¶
features_relation.count("feature_id")
┌───────────────────┐ │ count(feature_id) │ │ int64 │ ├───────────────────┤ │ 25389 │ └───────────────────┘
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:19
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... |
25389 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:19
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 │ varchar │ ├────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────┤ │ 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/441801965 │ LINESTRING (22.055825 57.9111269, 22.0560882 57.9113742, 22.0563349 57.9115746) │ 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.0380646 58.0024242, 22.0376184 58.0022958, 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.0255445 57.9975239, 22.0235683 57.9963199, 22.019945 57.9941124, 22.0189449 57.993394, 22.0185402 57.9927809, 22.0183619 57.9919954, 22.0185665 57.9912965, 22.0193382 57.9896141, 22.0197497 57.9886068, 22.0203264 57.9873089, 22.0207424 57.986355, 22.0209716 57.9857848, 22.0210919 57.9854853, 22.0213131 57.9849392, 22.0215284 57.9844135, 22.0216545 57.9839407, 22.0216902 57.9836398, 22.0217102 57.9833145, 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.0189558 57.9802367, 22.0181816 57.9797606, 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.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.1191505 57.9500351, 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.1212168 57.9543797, 22.1213226 57.954477, 22.1219175 57.9548877, 22.1220815 57.9550373, 22.122546 57.9558138, 22.1239156 57.957798, 22.1241603 57.9582011, 22.1248062 57.9591621, 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.1036122 57.9631158, 22.1012661 57.9639724, 22.0999695 57.9646673, 22.0888238 57.970641, 22.0884304 57.9708519, 22.08696 57.9716403, 22.0843338 57.9730484, 22.0816274 57.9742212) │ tertiary │ │ way/1018220953 │ LINESTRING (22.0608019 57.978236, 22.0535175 57.9800692) │ tertiary │ │ way/1018220951 │ LINESTRING (22.0622446 57.9779054, 22.0608019 57.978236) │ tertiary │ │ way/4937123 │ LINESTRING (22.0792412 57.975065, 22.0777345 57.9755978, 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.063527 57.9777223, 22.0632252 57.9777654, 22.0622446 57.9779054) │ tertiary │ │ way/1018220950 │ LINESTRING (22.0816274 57.9742212, 22.0792412 57.975065) │ tertiary │ │ · │ · │ · │ │ · │ · │ · │ │ · │ · │ · │ │ way/422210769 │ LINESTRING (24.7941555 59.005847, 24.7941019 59.0058244, 24.7940639 59.0057979) │ secondary │ │ way/137450752 │ LINESTRING (24.793926 59.0052965, 24.794009 59.0053538, 24.7940514 59.0054406, 24.7940929 59.0054882, 24.7941604 59.0055442, 24.7942963 59.0055926) │ secondary │ │ way/24283556 │ LINESTRING (24.7940367 59.0056978, 24.7939615 59.0055107, 24.7939112 59.0053687, 24.793926 59.0052965) │ secondary │ │ way/1121711341 │ LINESTRING (24.793926 59.0052965, 24.7937509 59.0048593, 24.7937118 59.004755, 24.7937039 59.004732) │ secondary │ │ way/137450748 │ LINESTRING (24.7942963 59.0055926, 24.7946264 59.0055354, 24.7948047 59.0054786, 24.7952746 59.0053057, 24.7953772 59.0052945) │ secondary │ │ way/137450740 │ LINESTRING (24.7953772 59.0052945, 24.7953503 59.0053397, 24.7951008 59.0054704, 24.7949417 59.0055511, 24.794669 59.0056903, 24.7945617 59.0057451) │ secondary │ │ way/224656086 │ LINESTRING (24.8187068 59.0062444, 24.8186917 59.006181, 24.8186404 59.006122, 24.8185699 59.0060723, 24.8179337 59.0057561, 24.8167134 59.0051357, 24.8157016 59.0046101, 24.8149428 59.00422, 24.814723 59.0041263, 24.8146952 59.0041145, 24.8139183 59.0039408) │ tertiary │ │ way/74942831 │ LINESTRING (24.8164268 59.010221, 24.8163867 59.0101075, 24.8149537 59.0086967, 24.8148876 59.0085948, 24.8148724 59.0085076, 24.8149002 59.0084158, 24.8149591 59.0083483, 24.8154505 59.0079398, 24.8158201 59.0076721, 24.8175669 59.0068603, 24.8181975 59.0065688, 24.8186109 59.0063777, 24.8186614 59.0063367, 24.818702 59.0062935, 24.8187068 59.0062444) │ tertiary │ │ way/1029298115 │ LINESTRING (24.81101 59.0157065, 24.8115118 59.0158097, 24.8119494 59.0158681, 24.8126192 59.0159224, 24.8135312 59.0159954, 24.8156413 59.0162671, 24.8168785 59.0163744, 24.8174198 59.0164129, 24.8193333 59.0164973, 24.8221157 59.0166603, 24.8225463 59.0167026, 24.8229212 59.0167741, 24.8232737 59.0168768, 24.8235504 59.0169741, 24.824236 59.0172386, 24.8249743 59.017494, 24.8269854 59.0180681, 24.8282045 59.0183873, 24.829199 59.0186082, 24.8302959 59.0188188, 24.831214 59.018924, 24.8326716 59.0190095, 24.8338176 59.0190259, 24.835508 59.0190133, 24.8360077 59.0190424, 24.8366971 59.0191944, 24.8368133 59.0192216, 24.8368643 59.0193725, 24.8371249 59.0197292) │ tertiary │ │ way/1029298113 │ LINESTRING (24.8380005 59.0177754, 24.8380807 59.0179842, 24.8380916 59.0181184, 24.838059 59.0182733, 24.8378428 59.0184711, 24.8373644 59.0188802, 24.836921 59.0191268, 24.8368133 59.0192216) │ tertiary │ ├────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────┤ │ ? rows (>9999 rows, 20 shown) 3 columns │ └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Count loaded roads¶
features_relation.count("feature_id")
┌───────────────────┐ │ count(feature_id) │ │ int64 │ ├───────────────────┤ │ 26221 │ └───────────────────┘
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
29512.95952574051
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:06
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 │ ├─────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ 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 │ {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.4770544 47.0510716, 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) │ │ · │ · │ · │ │ · │ · │ · │ │ · │ · │ · │ │ node/3530177691 │ {natural=tree} │ POINT (9.5276682 47.122004) │ │ way/1057550359 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=48b, 'addr:postcode'=9495, 'addr:street'=Fürst-Johann-Strasse, building=semidetached_house} │ POLYGON ((9.5277694 47.1219968, 9.5277661 47.1219385, 9.5278357 47.1219367, 9.5278353 47.1219281, 9.5279088 47.1219262, 9.5279129 47.1220002, 9.5278493 47.1220018, 9.5278489 47.1219948, 9.5277694 47.1219968)) │ │ way/346498763 │ {building=garage} │ POLYGON ((9.5277189 47.1221587, 9.5278218 47.1221562, 9.5278186 47.1220952, 9.5277157 47.1220977, 9.5277189 47.1221587)) │ │ node/3530177748 │ {natural=tree} │ POINT (9.5278318 47.1221465) │ │ node/3530177757 │ {natural=tree} │ POINT (9.5278991 47.1221684) │ │ way/346498795 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=3, 'addr:postcode'=9495, 'addr:street'=Wingarten, building=apartments} │ POLYGON ((9.5277974 47.1224017, 9.528004 47.1223854, 9.5279872 47.1222868, 9.5277806 47.1223031, 9.5277974 47.1224017)) │ │ node/3530177750 │ {natural=tree} │ POINT (9.5279603 47.1221496) │ │ way/1057550360 │ {'addr:city'=Triesen, 'addr:country'=LI, 'addr:housenumber'=48a, 'addr:postcode'=9495, 'addr:street'=Fürst-Johann-Strasse, building=semidetached_house} │ POLYGON ((9.5280553 47.121989, 9.5280517 47.1219309, 9.5279813 47.1219329, 9.5279808 47.1219244, 9.5279088 47.1219262, 9.5279129 47.1220002, 9.5279771 47.1219984, 9.5279767 47.1219913, 9.5280553 47.121989)) │ │ node/3530177339 │ {natural=tree} │ POINT (9.5280215 47.1210139) │ │ node/3530177286 │ {natural=tree} │ POINT (9.5279052 47.1208204) │ ├─────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ ? 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) |
56436 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: >