Skip to content

Instantly share code, notes, and snippets.

# ENTRY:
if __name__ == "__main__":
download_all()
# MASTER:
def download_all():
for region in PARAM['regions']:
try:
logging.info("Downloading region '{}'...".format(region))
download(region)
logging.info("Download OK!")
except urllib.error.URLError:
logging.exception("Download failed (no connection or wrong URL?).")
# SLAVES:
def download(region):
bbox = PARAM['regions'][region]
url = PARAM['API-URL'].format(bbox="{0},{1},{2},{3}".format(*bbox))
out = OFILE['OSM'].format(region=region)
meta = {
'Region': region,
# AUXILIARY:
# https://stackoverflow.com/questions/34491808/how-to-get-the-current-scripts-code-in-python
THIS = inspect.getsource(inspect.getmodule(inspect.currentframe()))
# Set logger verbosity and output format
logging.basicConfig(
level=logging.DEBUG,
format="%(levelname)-8s [%(asctime)s] : %(message)s",
datefmt="%Y%m%d %H:%M:%S %Z"
# PARAMETERS:
PARAM = {
# Bounding box to download [left, bottom, right, top]
# https://wiki.openstreetmap.org/wiki/API_v0.6
'regions': {
'kaohsiung_small': [120.2206, 22.4827, 120.4308, 22.7578],
'kaohsiung_large': [119.9377, 22.1645, 120.8084, 23.3347],
},
for fn in OFILE.values():
os.makedirs(os.path.dirname(fn).format(), exist_ok=True)
# OUTPUT FILES:
OFILE = {
# Put the downloaded *.osm files here
'OSM': "OUTPUT/maps/UV/{region}.osm",
# Record meta-info here
'OSM-meta': "OUTPUT/maps/{region}_meta.txt",
}
# INPUT FILES:
pass
# IMPORTS:
import os
import json
import inspect
import logging
import datetime as dt
import urllib.request, urllib.error
# RA, 2019-01-15
# Download an OpenStreetMap file based on a lat-lon bounding box
# License: CC0 -- "No rights reserved"