Skip to content

Instantly share code, notes, and snippets.

@joker234
Last active November 15, 2019 23:14
Show Gist options
  • Save joker234/60fb3dbafc0fe7f24058e4f6619d4cf4 to your computer and use it in GitHub Desktop.
Save joker234/60fb3dbafc0fe7f24058e4f6619d4cf4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"\n",
"from collections import Counter"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_overpass_json(key_tags):\n",
" url = 'https://overpass-api.de/api/interpreter'\n",
" query = '[out:json];'\n",
" for key, tag in key_tags:\n",
" query += f'nwr[\"{key}\"=\"{tag}\"][\"amenity\"];'\n",
" query += 'out tags qt;'\n",
" r = requests.get(url, params={'data': query})\n",
" return r.json()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"j = get_overpass_json([('currency:XBT', 'yes')])\n",
"\n",
"# get all amenity tags\n",
"amenities = [e['tags']['amenity'] for e in j['elements']]\n",
"\n",
"# count all elements\n",
"counter = Counter(amenities)\n",
"\n",
"# sort by second key in reversed order\n",
"sorted_counter = sorted(counter.items(), key=lambda x: x[1], reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" atm 174\n",
" bank 90\n",
" bar 16\n",
" cafe 14\n",
" restaurant 12\n",
" pharmacy 11\n",
" bureau_de_change 10\n",
" fast_food 5\n",
" fuel 3\n",
" dentist 3\n",
" ice_cream 2\n",
" pub 2\n",
" driving_school 2\n",
" payment_centre 1\n",
" vending_machine 1\n",
" community_centre 1\n"
]
}
],
"source": [
"# print one column with 20 characters and the second with 4 characters\n",
"for entry in sorted_counter:\n",
" print(f\"{entry[0]: >20} {entry[1]: >4}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-10-25T23:00:00Z\n",
" amenity=atm 172.0\n",
" remainder 111.0\n",
" amenity=bank 91.0\n",
" amenity=bar 16.0\n",
" amenity=cafe 14.0\n",
" amenity=restaurant 11.0\n",
" amenity=pharmacy 11.0\n",
" amenity=bureau_de_change 10.0\n",
" amenity=fast_food 5.0\n",
" amenity=fuel 3.0\n",
" amenity=dentist 3.0\n",
" amenity=pub 2.0\n",
" amenity=ice_cream 2.0\n",
" amenity=vending_machine 1.0\n",
" amenity=community_centre 1.0\n",
" amenity=driving_school 1.0\n",
" amenity=payment_centre 1.0\n"
]
}
],
"source": [
"# alternative method using ohsome API\n",
"# get json with result inside, no processing needed, just printing\n",
"j = requests.get(\"https://api.ohsome.org/v0.9/elements/count/groupBy/tag?keys=currency:XBT&values=yes&types=node,way,relation&bboxes=-180,-90,180,90&groupByKey=amenity\").json()\n",
"\n",
"# print timestamp of most recent data available at ohsome API\n",
"print(j['groupByResult'][0]['result'][0]['timestamp'])\n",
"\n",
"# create list of results from json\n",
"counter = [(e['groupByObject'], e['result'][0]['value']) for e in j['groupByResult']]\n",
"\n",
"# sort results\n",
"sorted_counter = sorted(counter, key=lambda x: x[1], reverse=True)\n",
"\n",
"# print results\n",
"for entry in sorted_counter:\n",
" print(f\"{entry[0]: >30} {entry[1]: >6}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@camelCaseNick
Copy link

You might consider the following shorter overpass query:

[out:json];
nwr["currency:XBT"="yes"];
out tags qt;

@joker234
Copy link
Author

You might consider the following shorter overpass query:

[out:json];
nwr["currency:XBT"="yes"];
out tags qt;

thx, will fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment