OSM Online Loader¶
In [1]:
Copied!
from srai.loaders.osm_loaders.filters.popular import get_popular_tags
from srai.loaders.osm_loaders.filters import GEOFABRIK_LAYERS, HEX2VEC_FILTER
from srai.loaders.osm_loaders import OSMOnlineLoader
from srai.regionalizers import geocode_to_region_gdf
from srai.plotting.folium_wrapper import plot_regions
from functional import seq
from srai.loaders.osm_loaders.filters.popular import get_popular_tags
from srai.loaders.osm_loaders.filters import GEOFABRIK_LAYERS, HEX2VEC_FILTER
from srai.loaders.osm_loaders import OSMOnlineLoader
from srai.regionalizers import geocode_to_region_gdf
from srai.plotting.folium_wrapper import plot_regions
from functional import seq
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: 1152. Key/value pairs: 11766'
In [3]:
Copied!
seq(all_popular_tags.items()).take(10).dict()
seq(all_popular_tags.items()).take(10).dict()
Out[3]:
{'4wd_only': ['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'], 'FIXME': ['Shape Bing outline.'], 'FMMP_modified': ['no'], 'FMMP_reviewed': ['no'], 'GNS:dsg_code': ['PPL', 'WAD'], 'GNS:dsg_name': ['populated place', 'wadi'], 'GNS:dsg_string': ['populated place']}
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'], 'building': ['house', 'residential', 'yes'], 'highway': ['footway', 'path', 'residential', 'service', 'track', 'unclassified'], 'natural': ['tree', 'water', 'wood'], 'oneway': ['yes'], 'power': ['pole', 'tower'], 'service': ['driveway'], 'source': ['BAG', 'Bing'], '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
Downloading leisure: park: 100%|██████████| 1/1 [00:00<00:00, 2.78it/s]
Out[7]:
geometry | leisure | |
---|---|---|
feature_id | ||
relation/1348101 | POLYGON ((17.07422 51.08476, 17.07431 51.08479... | park |
relation/3654662 | MULTIPOLYGON (((17.05660 51.11998, 17.05674 51... | park |
relation/4552866 | POLYGON ((16.87549 51.13633, 16.87538 51.13640... | park |
relation/6629819 | POLYGON ((16.97217 51.08334, 16.97213 51.08324... | park |
relation/6727464 | POLYGON ((16.97677 51.09440, 16.97681 51.09440... | park |
... | ... | ... |
way/1165220485 | POLYGON ((16.93995 51.09223, 16.94014 51.09223... | park |
way/1184167518 | POLYGON ((17.02414 51.11386, 17.02409 51.11356... | park |
way/1198428666 | POLYGON ((16.97079 51.09587, 16.97055 51.09580... | park |
way/1210337884 | POLYGON ((16.90013 51.15538, 16.90036 51.15571... | park |
way/1225784763 | POLYGON ((16.99172 51.12721, 16.99175 51.12723... | park |
288 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
Downloading leisure: stadium : 100%|██████████| 8/8 [00:02<00:00, 3.68it/s] Grouping features: 100%|██████████| 2/2 [00:00<00:00, 32.44it/s]
Out[9]:
geometry | tourism | sport | |
---|---|---|---|
feature_id | |||
node/216330105 | POINT (2.17484 41.38580) | amenity=bar | NaN |
node/432592965 | POINT (2.17077 41.38035) | amenity=cafe | NaN |
node/499122827 | POINT (2.17440 41.43041) | amenity=bar | NaN |
node/499122828 | POINT (2.17371 41.43198) | amenity=bar | NaN |
node/499122829 | POINT (2.17252 41.43052) | amenity=cafe | NaN |
... | ... | ... | ... |
way/1223158058 | POLYGON ((2.15459 41.36165, 2.15524 41.36164, ... | NaN | leisure=pitch |
way/1223750308 | POLYGON ((2.19186 41.38141, 2.19213 41.38147, ... | NaN | leisure=pitch |
way/1226173443 | POLYGON ((2.15653 41.37767, 2.15636 41.37781, ... | NaN | leisure=pitch |
way/1229654044 | POLYGON ((2.16821 41.43381, 2.16840 41.43373, ... | NaN | leisure=pitch |
way/1233295819 | POLYGON ((2.17401 41.37558, 2.17409 41.37556, ... | NaN | leisure=pitch |
4012 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