From e78aa8db231f431a155e8fbfb44d7b98077cc311 Mon Sep 17 00:00:00 2001 From: Mike C Date: Tue, 12 Jan 2016 16:17:06 -0500 Subject: [PATCH] Added test map list, implemented map processing including combining into single output map file --- lists/test.txt | 2 ++ process_maps.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 lists/test.txt create mode 100755 process_maps.py diff --git a/lists/test.txt b/lists/test.txt new file mode 100644 index 0000000..a47220f --- /dev/null +++ b/lists/test.txt @@ -0,0 +1,2 @@ +http://download.geofabrik.de/north-america/canada/prince-edward-island-latest.osm.pbf +http://download.geofabrik.de/north-america/us/delaware-latest.osm.bz2 diff --git a/process_maps.py b/process_maps.py new file mode 100755 index 0000000..def9ac4 --- /dev/null +++ b/process_maps.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import subprocess, sys, os, pprint, datetime, argparse, time + +base_path = os.path.dirname(os.path.realpath(__file__)) + +FNULL = open(os.devnull, 'w') + +wget_cmd = '/usr/bin/wget' +bunzip2_cmd = '/usr/bin/bunzip2' + +if __name__ == '__main__': + current_timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M') + + parser = argparse.ArgumentParser() + parser.add_argument('--map-list', action='store') + #TODO: Add argument to pass path to osmosis + #TODO: Add argument to pass path to output dir + args = parser.parse_args() + + map_list = [] + with open(args.map_list, 'r') as maps: + for line in maps: + map_list.append(line.strip()) + + print('Creating working directories') + if not os.path.exists('out'): + os.makedirs('out') + os.chdir('out') + if not os.path.exists(current_timestamp): + os.makedirs(current_timestamp) + os.chdir(current_timestamp) + if not os.path.exists('dl'): + os.makedirs('dl') + + print('Downloading maps') + for line in map_list: + print(' ', end='') + print(line) + subprocess.run([wget_cmd, '-P', 'dl', line.strip()], stdout=FNULL, stderr=subprocess.STDOUT) + print(' Sleeping 1m to prevent throttle/blocking') + time.sleep(300) # Seconds + + print('Decompressing maps (if necessary)') + for dirpath, dirnames, filenames in os.walk('dl'): + for file in filenames: + if file.endswith('bz2'): + print(' ', end='') + print(file) + subprocess.run([bunzip2_cmd, os.path.join(dirpath, file)]) + + print('Processing maps using osmosis') + files_to_process = [] + for dirpath, dirnames, filenames in os.walk('dl'): + for file in filenames: + print(' Found map: ', end='') + print(os.path.join(dirpath, file)) + files_to_process.append(os.path.join(dirpath, file)) + osmosis_cmd = [os.path.join(base_path, 'bin', 'osmosis', 'bin', 'osmosis')] + for file in files_to_process: + if file.endswith('osm'): + osmosis_cmd.extend(['--rx', 'file=' + file]) + elif file.endswith('pbf'): + osmosis_cmd.extend(['--rb', 'file=' + file]) + for x in range(0, len(files_to_process) - 1): + osmosis_cmd.append('--merge') + osmosis_cmd.extend(['--mapfile-writer', 'file=output.map']) + subprocess.run(osmosis_cmd) +