-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyextract.py
197 lines (154 loc) · 6.64 KB
/
pyextract.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'''
Created on Dec 31, 2020
@author: stevo
'''
import os
import json
import argparse
import timeit
from utils.map_bb import num2MapBB, conversion_type
from utils.file_and_folder import ensure_dir
from utils.glog import logger_init
from logging import INFO, DEBUG
from utils.proc import ExecuteCmdExt
def write_configfile(filename, outdir, maplist):
data = {}
data['extracts'] = []
data['directory'] = outdir
for x, y, z, smap in maplist:
data['extracts'].append({'output': '{}-{}-{}.osm'.format(x, y, z),
'output_format': 'osm',
'bbox': smap.getbbox()})
with open(filename, 'w') as outfile:
json.dump(data, outfile, indent=4)
if __name__ == '__main__':
start = timeit.default_timer()
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-i',
dest='inputfilename',
type=argparse.FileType('r', encoding='UTF-8'),
default="./sampledata/in/seamarks_planet.osm",
help='name of osm file')
parser.add_argument('-o',
dest='outdir',
default="./workingdir/",
help='output directory')
parser.add_argument('-y_min',
dest='y_min_z9',
type=int,
default=161,
help='x position of tile (zoomlevel 9)')
parser.add_argument('-y_max',
dest='y_max_z9',
type=int,
default=161,
help='x position of tile (zoomlevel 9)')
parser.add_argument('-x_min',
dest='x_min_z9',
type=int,
default=274,
help='x position of tile (zoomlevel 9)')
parser.add_argument('-x_max',
dest='x_max_z9',
type=int,
default=274,
help='x position of tile (zoomlevel 9)')
parser.add_argument('-x',
dest='x',
type=int,
default=-1,
help='x position of tile (zoomlevel 9)')
parser.add_argument('-y',
dest='y',
type=int,
default=-1,
help='y position of tile (zoomlevel 9)')
parser.add_argument('-g', '--generate', action='store_true')
parser.add_argument('-l',
dest='logfilename',
default="./log/pyextract.log",
help='logfile')
args = parser.parse_args()
log = logger_init("pyextractr",
args.logfilename,
level_file=DEBUG,
level_console=INFO)
in_filename = args.inputfilename.name
z = 9 # zoomlevel
if args.x is not -1 and args.y is not -1:
x_min_z9 = args.x
x_max_z9 = args.x
y_min_z9 = args.y
y_max_z9 = args.y
else:
x_min_z9 = args.x_min_z9
x_max_z9 = args.x_max_z9
y_min_z9 = args.y_min_z9
y_max_z9 = args.y_max_z9
print("generate config file for command \"osmium extract\": {}".format(in_filename))
print("input file: {}".format(in_filename))
print("output dir: {}".format(args.outdir))
print("x_min={}, x_max={}, y_min={}, y_max={}, z={}".format(x_min_z9, x_max_z9, y_min_z9, y_max_z9,z))
# stage 1 / generate config file with level 9
maplist = list()
for y_z9 in range(y_min_z9, y_max_z9+1):
for x_z9 in range(x_min_z9, x_max_z9+1):
maplist.append([x_z9, y_z9, z, num2MapBB(x_z9, y_z9, z, conversion_type.extendet)])
confdir = args.outdir + "cfg/"
ensure_dir(confdir)
outfile = confdir + "osmium_extract_z9.cfg"
if os.path.exists(outfile):
os.remove(outfile)
osmdir = args.outdir + "osm-extracts/"
ensure_dir(osmdir)
write_configfile(outfile, osmdir, maplist)
convert_cmd = "osmium extract --overwrite --config {} {}".format(outfile, in_filename)
if(args.generate is True):
outstr, ret = ExecuteCmdExt(convert_cmd)
if ret != 0:
log.info("generation failed outfile: {}".format("outfile"))
log.error(outstr)
else:
log.info("generation passed outfile: {}".format("outfile"))
else:
print(convert_cmd)
# stage 2 / generate config file with level 10-12
for y_z9 in range(y_min_z9, y_max_z9+1):
for x_z9 in range(x_min_z9, x_max_z9+1):
in_filename = osmdir + "{}-{}-{}.osm".format(x_z9, y_z9, z)
filesize = os.stat(in_filename).st_size
if filesize < 100:
log.info("generation skipped for in_filename {}".format(in_filename))
continue
maplist = list()
for xadj_z10 in [0, 1]:
for yadj_z10 in [0, 1]:
x_z10 = x_z9 * 2 + xadj_z10
y_z10 = y_z9 * 2 + yadj_z10
maplist.append([x_z10, y_z10, 10, num2MapBB(x_z10, y_z10, 10, conversion_type.extendet)])
for xadj_z11 in [0, 1, 2, 3]:
for yadj_z11 in [0, 1, 2, 3]:
x_z11 = x_z9 * 4 + xadj_z11
y_z11 = y_z9 * 4 + yadj_z11
maplist.append([x_z11, y_z11, 11, num2MapBB(x_z11, y_z11, 11, conversion_type.extendet)])
for xadj_z12 in [0, 1, 2, 3, 4, 5, 6, 7]:
for yadj_z12 in [0, 1, 2, 3, 4, 5, 6, 7]:
x_z12 = x_z9 * 8 + xadj_z12
y_z12 = y_z9 * 8 + yadj_z12
maplist.append([x_z12, y_z12, 12, num2MapBB(x_z12, y_z12, 12, conversion_type.extendet)])
outfile = confdir + "osmium_extract_x{}-y{}_z9.cfg".format(x_z9, y_z9)
if os.path.exists(outfile):
os.remove(outfile)
write_configfile(outfile, osmdir, maplist)
convert_cmd = "osmium extract --overwrite --config {} {}".format(outfile, in_filename)
if(args.generate is True):
outstr, ret = ExecuteCmdExt(convert_cmd)
if ret != 0:
log.info("generation failed / outfile: {}".format(outfile))
log.error(outstr)
else:
log.info("generation passed / outfile: {}".format(outfile))
else:
print(convert_cmd)
stop = timeit.default_timer()
print('Time: ', stop - start)