-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpov_convert.py
63 lines (52 loc) · 2.44 KB
/
tpov_convert.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Built-in modules
import os, sys, argparse, subprocess
from tpov_functions import *
# File formats which osmconvert can read
osmconvert_formats = (".osm", ".osc", ".osc.gz", ".osh", ".o5m", ".o5c", ".pbf")
def main (args):
map_file, filter_file = args.map, args.filter
try:
with open (filter_file) as f:
pass
except FileNotFoundError:
raise FileNotFoundError (f"Filter file {filter_file} not found")
if os.path.splitext (map_file) [1] not in osmconvert_formats:
raise ValueError (f"Invalid file format. Supported formats: {', '.join (osmconvert_formats)}")
elif os.path.splitext (map_file) [1] not in (".osm", ".o5m"): # Convert to .o5m format
print (f"Converting {map_file} to .o5m format...")
o5m_file = os.path.splitext (map_file) [0] + ".temp.o5m" # Temporary file
sys.stdout.flush () # Force print to display before running subprocess
sys.stderr.flush ()
osmconvert = subprocess.run (["osmconvert", map_file, "-o=" + o5m_file], stdout = sys.stdout, stderr = sys.stderr)
osmconvert.check_returncode ()
else:
o5m_file = map_file
print (f"Filtering {o5m_file} with {filter_file}...")
output_file = os.path.splitext (map_file) [0] + ".filtered.o5m"
sys.stdout.flush ()
sys.stderr.flush ()
osmfilter = subprocess.run (["osmfilter", o5m_file, "--parameter-file=" + filter_file, "-o=" + output_file], stdout = sys.stdout, stderr = sys.stderr)
osmfilter.check_returncode ()
print (f"Saved filtered .o5m file as {output_file}")
if o5m_file != map_file:
os.remove (o5m_file)
print (f"Deleted temporary file {o5m_file}")
parser = argparse.ArgumentParser (
description = "Convert and filter OSM map files for use with tpov_match.py",
formatter_class = argparse.RawDescriptionHelpFormatter,
epilog = f"""\
This program and the tpov suite require the tools osmconvert and osmfilter.
Currently supported input file formats:
{', '.join (osmconvert_formats)}
The filter file contains a list of osmfilter commands.
For more information, see https://wiki.openstreetmap.org/wiki/Osmfilter
A default filter file is provided at "tpov_filter.txt".
"""
)
parser.add_argument ("map", help = "The OSM map file to convert")
parser.add_argument ("filter", help = "The filter file to apply")
def script (args):
import shlex
main (parser.parse_args (shlex.split (args)))
if __name__ == "__main__":
main (parser.parse_args ())