-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
36 lines (27 loc) · 1.02 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
import pandas as pd
import numpy as np
def get_osm_objects(country='CZ', key='natural', value='peak', extract_tags={'name':'??','ele':''}):
'''Get pandas dataframe with OSM objects of given type in a country.
:param country: 'CZ', 'DE', ...
:param key: e.g. 'natural' or 'amenity'
:param value: e.g. 'peak' or 'pub'
:param extract_tags: list of tags that will be extracted to the dataframe
:return: pandas dataframe
'''
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area["ISO3166-1"="%s"][admin_level=2];
(node["%s"="%s"](area);
);
out center;
""" % (country, key, value)
response = requests.get(overpass_url, params={'data': overpass_query})
data = response.json()
df = pd.DataFrame(data['elements'])
if 'tags' in df:
for tag, missing in extract_tags.items():
df[tag] = df['tags'].apply(lambda x: x.get(tag, missing))
df.drop('tags', inplace=True, axis=1)
return df