Skip to content

Instantly share code, notes, and snippets.

@matkoniecz
Last active June 29, 2018 09:44
Show Gist options
  • Save matkoniecz/04a224799bc0fc05b3fb218d75b74acc to your computer and use it in GitHub Desktop.
Save matkoniecz/04a224799bc0fc05b3fb218d75b74acc to your computer and use it in GitHub Desktop.
import urllib.request, urllib.error, urllib.parse
import time
def download_overpass_query(query, filepath):
query = urllib.parse.quote(query)
url = "http://overpass-api.de/api/interpreter?data=" + query
with open(filepath, 'w') as file:
downloaded = overpass_download(url)
print(downloaded)
file.write(downloaded)
def overpass_download(url):
while True:
try:
print("downloading " + url)
resource = urllib.request.urlopen(url)
return resource.read().decode('utf-8')
except urllib.error.HTTPError as e:
print(e.getcode())
if e.getcode() == 429 or e.getcode() == 503:
time.sleep(60)
continue
raise e
except urllib.error.URLError as e:
print(("URLError for url " + url))
print(e)
return
query = """
[out:json][timeout:25];
(
node["flood_mark"="yes"];
way["flood_mark"="yes"];
relation["flood_mark"="yes"];
);
out body;
>;
out skel qt;
"""
download_overpass_query(query, 'test_saving.geojson')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment