OSM Online Loader¶
In [1]:
Copied!
from functional import seq
from srai.loaders.osm_loaders import OSMOnlineLoader
from srai.loaders.osm_loaders.filters import GEOFABRIK_LAYERS, HEX2VEC_FILTER
from srai.loaders.osm_loaders.filters.popular import get_popular_tags
from srai.plotting.folium_wrapper import plot_regions
from srai.regionalizers import geocode_to_region_gdf
from functional import seq
from srai.loaders.osm_loaders import OSMOnlineLoader
from srai.loaders.osm_loaders.filters import GEOFABRIK_LAYERS, HEX2VEC_FILTER
from srai.loaders.osm_loaders.filters.popular import get_popular_tags
from srai.plotting.folium_wrapper import plot_regions
from srai.regionalizers import geocode_to_region_gdf
Filters¶
Filters are dictionaries used for specifying what type of objects one would like to download from OpenStreetMap.
There is currently one predefined filter (from Hex2Vec paper) and one way to download a filter - using popular tags from taginfo API.
They can also be defined manually in code.
Additionally, few predefined grouped filters are available (eg. BASE_OSM_GROUPS_FILTER
and GEOFABRIK_LAYERS
).
Grouped filters categorize base filters into groups.
Get popular tags from taginfo API¶
In [2]:
Copied!
all_popular_tags = get_popular_tags()
num_keys = len(all_popular_tags)
num_values = seq(all_popular_tags.values()).map(len).sum()
f"Unique keys: {num_keys}. Key/value pairs: {num_values}"
all_popular_tags = get_popular_tags()
num_keys = len(all_popular_tags)
num_values = seq(all_popular_tags.values()).map(len).sum()
f"Unique keys: {num_keys}. Key/value pairs: {num_values}"
Out[2]:
'Unique keys: 1245. Key/value pairs: 13066'
In [3]:
Copied!
seq(all_popular_tags.items()).take(10).dict()
seq(all_popular_tags.items()).take(10).dict()
Out[3]:
{'4wd_only': ['recommended', 'yes'], 'CLC:code': ['112', '211', '221', '231', '242', '243', '311', '312', '313'], 'CLC:explanation': ['See http://wiki.openstreetmap.org/wiki/Romania_CLC_Import.'], 'CLC:year': ['2006'], 'FMMP_modified': ['no'], 'FMMP_reviewed': ['no'], 'GNS:dsg_code': ['PPL', 'WAD'], 'GNS:dsg_name': ['populated place', 'wadi'], 'GNS:dsg_string': ['populated place'], 'GeoBaseNHN:DatasetName': ['02LG000', '02LH000']}
In [4]:
Copied!
frequent_in_wiki_only_tags = get_popular_tags(in_wiki_only=True, min_fraction=0.001)
frequent_in_wiki_only_tags
frequent_in_wiki_only_tags = get_popular_tags(in_wiki_only=True, min_fraction=0.001)
frequent_in_wiki_only_tags
Out[4]:
{'access': ['private'], 'addr:country': ['DE'], 'building': ['house', 'residential', 'yes'], 'highway': ['crossing', 'footway', 'path', 'residential', 'service', 'track', 'unclassified'], 'landuse': ['farmland'], 'natural': ['tree', 'water', 'wood'], 'oneway': ['yes'], 'power': ['pole', 'tower'], 'service': ['driveway'], 'source': ['BAG', 'Bing', 'NRCan-CanVec-10.0'], 'surface': ['asphalt', 'unpaved'], 'wall': ['no'], 'waterway': ['stream']}
Import a predefined filter¶
In [5]:
Copied!
hex_2_vec_keys = len(HEX2VEC_FILTER)
hex_2_vec_key_values = seq(HEX2VEC_FILTER.values()).map(len).sum()
f"Unique keys: {hex_2_vec_keys}. Key/value pairs: {hex_2_vec_key_values}"
hex_2_vec_keys = len(HEX2VEC_FILTER)
hex_2_vec_key_values = seq(HEX2VEC_FILTER.values()).map(len).sum()
f"Unique keys: {hex_2_vec_keys}. Key/value pairs: {hex_2_vec_key_values}"
Out[5]:
'Unique keys: 15. Key/value pairs: 725'
In [6]:
Copied!
geofabrik_layers_keys = len(GEOFABRIK_LAYERS)
geofabrik_layers_key_values = (
seq(GEOFABRIK_LAYERS.values()).flat_map(lambda filter: filter.items()).map(len).sum()
)
f"Unique groups: {geofabrik_layers_keys}. Key/value pairs: {geofabrik_layers_key_values}"
geofabrik_layers_keys = len(GEOFABRIK_LAYERS)
geofabrik_layers_key_values = (
seq(GEOFABRIK_LAYERS.values()).flat_map(lambda filter: filter.items()).map(len).sum()
)
f"Unique groups: {geofabrik_layers_keys}. Key/value pairs: {geofabrik_layers_key_values}"
Out[6]:
'Unique groups: 28. Key/value pairs: 116'
Using OSMOnlineLoader to download data for a specific area¶
Download all parks in Wrocław, Poland¶
In [7]:
Copied!
loader = OSMOnlineLoader()
parks_filter = {"leisure": "park"}
wroclaw_gdf = geocode_to_region_gdf("Wrocław, Poland")
parks_gdf = loader.load(wroclaw_gdf, parks_filter)
parks_gdf
loader = OSMOnlineLoader()
parks_filter = {"leisure": "park"}
wroclaw_gdf = geocode_to_region_gdf("Wrocław, Poland")
parks_gdf = loader.load(wroclaw_gdf, parks_filter)
parks_gdf
Out[7]:
geometry | leisure | |
---|---|---|
feature_id | ||
relation/1348101 | POLYGON ((17.07422 51.08476, 17.07431 51.08479... | park |
relation/3654662 | MULTIPOLYGON (((17.05674 51.12007, 17.05682 51... | park |
relation/4552866 | POLYGON ((16.87549 51.13633, 16.87538 51.1364,... | park |
relation/6629819 | POLYGON ((16.97217 51.08334, 16.97213 51.08324... | park |
relation/6727464 | POLYGON ((16.97677 51.0944, 16.97681 51.0944, ... | park |
... | ... | ... |
way/1381322925 | POLYGON ((17.0003 51.0982, 17.0006 51.09827, 1... | park |
way/1381322928 | POLYGON ((16.9983 51.09875, 16.99921 51.09897,... | park |
way/1381322931 | POLYGON ((16.9971 51.09847, 16.99715 51.09843,... | park |
way/1381875210 | POLYGON ((17.11168 51.10123, 17.11033 51.1013,... | park |
way/1381875215 | POLYGON ((17.10952 51.10267, 17.10955 51.10281... | park |
313 rows × 2 columns
In [8]:
Copied!
folium_map = plot_regions(wroclaw_gdf, colormap=["lightgray"], tiles_style="CartoDB positron")
parks_gdf.explore(m=folium_map, color="forestgreen")
folium_map = plot_regions(wroclaw_gdf, colormap=["lightgray"], tiles_style="CartoDB positron")
parks_gdf.explore(m=folium_map, color="forestgreen")
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Download hotels, bars, cafes, pubs and sport related objects in Barcelona¶
Uses grouped filters as an example.
In [9]:
Copied!
barcelona_gdf = geocode_to_region_gdf("Barcelona")
barcelona_filter = {
"tourism": {"building": "hotel", "amenity": ["bar", "cafe", "pub"]},
"sport": {"sport": "soccer", "leisure": ["pitch", "sports_centre", "stadium"]},
}
barcelona_objects_gdf = loader.load(barcelona_gdf, barcelona_filter)
barcelona_objects_gdf
barcelona_gdf = geocode_to_region_gdf("Barcelona")
barcelona_filter = {
"tourism": {"building": "hotel", "amenity": ["bar", "cafe", "pub"]},
"sport": {"sport": "soccer", "leisure": ["pitch", "sports_centre", "stadium"]},
}
barcelona_objects_gdf = loader.load(barcelona_gdf, barcelona_filter)
barcelona_objects_gdf
Out[9]:
geometry | tourism | sport | |
---|---|---|---|
feature_id | |||
node/216330105 | POINT (2.17484 41.3858) | amenity=bar | NaN |
node/432592965 | POINT (2.17077 41.38035) | amenity=cafe | NaN |
node/499122827 | POINT (2.17432 41.43045) | amenity=bar | NaN |
node/499122828 | POINT (2.17371 41.43198) | amenity=bar | NaN |
node/499122829 | POINT (2.17252 41.43052) | amenity=cafe | NaN |
... | ... | ... | ... |
way/1371344763 | POLYGON ((2.14242 41.36157, 2.14369 41.36165, ... | NaN | sport=soccer |
way/1371344764 | POLYGON ((2.14236 41.36242, 2.14362 41.36246, ... | NaN | sport=soccer |
way/1371900982 | POLYGON ((2.21076 41.41428, 2.21086 41.41435, ... | NaN | leisure=pitch |
way/1379439024 | POLYGON ((2.11682 41.39987, 2.11706 41.40013, ... | NaN | leisure=pitch |
way/1379439025 | POLYGON ((2.11707 41.40019, 2.11693 41.40026, ... | NaN | leisure=pitch |
4312 rows × 3 columns
Tourism group¶
In [10]:
Copied!
folium_map = plot_regions(barcelona_gdf, colormap=["lightgray"], tiles_style="CartoDB positron")
barcelona_objects_gdf.query("tourism.notna()").explore(
m=folium_map,
color="orangered",
marker_kwds=dict(radius=1),
)
folium_map = plot_regions(barcelona_gdf, colormap=["lightgray"], tiles_style="CartoDB positron")
barcelona_objects_gdf.query("tourism.notna()").explore(
m=folium_map,
color="orangered",
marker_kwds=dict(radius=1),
)
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook