-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathosm_maps_functions.py
792 lines (638 loc) · 33.1 KB
/
osm_maps_functions.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
"""
functions and object for managing OSM maps
"""
#!/usr/bin/python
# import official python packages
import glob
import multiprocessing
import os
import struct
import subprocess
import sys
import platform
import shutil
import logging
# import custom python packages
from wahoomc import file_directory_functions as fd_fct
from wahoomc.constants_functions import get_path_to_static_tile_json, translate_tags_to_keep
from wahoomc.constants import USER_WAHOO_MC
from wahoomc.constants import USER_OUTPUT_DIR
from wahoomc.constants import RESOURCES_DIR
from wahoomc.constants import LAND_POLYGONS_PATH
from wahoomc.downloader import Downloader
from wahoomc.geofabrik import Geofabrik
log = logging.getLogger('main-logger')
class TileNotFoundError(Exception):
"""Raised when no tile is found for x/y combination"""
def get_xy_coordinates_from_input(input_xy_coordinates):
"""
extract/split x/y combinations by given X/Y coordinates.
input should be "188/88" or for multiple values "188/88,100/10,109/99".
returns a list of x/y combinations as integers
"""
xy_combinations = []
# split by "," first for multiple x/y combinations, then by "/" for x and y value
for xy_coordinate in input_xy_coordinates.split(","):
splitted = xy_coordinate.split("/")
if len(splitted) == 2:
xy_combinations.append(
{"x": int(splitted[0]), "y": int(splitted[1])})
return xy_combinations
def get_tile_by_one_xy_combination_from_jsons(xy_combination):
"""
get tile from json files by given X/Y coordinate combination
"""
# go through all files in all folders of the "json" directory
file_path_jsons = os.path.join(RESOURCES_DIR, 'json')
for folder in fd_fct.get_folders_in_folder(file_path_jsons):
for file in fd_fct.get_filenames_of_jsons_in_folder(os.path.join(file_path_jsons, folder)):
# get content of json in folder
content = fd_fct.read_json_file(
os.path.join(file_path_jsons, folder, file + '.json'))
# check tiles values against input x/y combination
for tile in content:
if tile['x'] == xy_combination['x'] and tile['y'] == xy_combination['y']:
return tile
# if function is processed until here, there is no tile found for the x/y combination --> Exception
raise TileNotFoundError
def run_subprocess_and_log_output(cmd, error_message, cwd=""):
"""
run given cmd-subprocess and issue error message if wished
"""
if not cwd:
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as process:
for line in iter(process.stdout.readline, b''): # b'\n'-separated lines
try:
log.debug('subprocess:%r', line.decode("utf-8").strip())
except UnicodeDecodeError:
log.debug('subprocess:%r', line.decode("latin-1").strip())
else:
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) as process:
for line in iter(process.stdout.readline, b''): # b'\n'-separated lines
try:
log.debug('subprocess:%r', line.decode("utf-8").strip())
except UnicodeDecodeError:
log.debug('subprocess:%r', line.decode("latin-1").strip())
if error_message and process.wait() != 0: # 0 means success
log.error(error_message)
sys.exit()
class OsmMaps:
"""
This is a OSM data class
"""
osmosis_win_file_path = fd_fct.get_tooling_win_path(
['Osmosis', 'bin', 'osmosis.bat'])
# Number of workers for the Osmosis read binary fast function
workers = '1'
def __init__(self, o_input_data):
self.force_processing = ''
self.tiles = []
self.border_countries = {}
self.country_name = ''
self.o_input_data = o_input_data
self.o_downloader = Downloader(
o_input_data.max_days_old, o_input_data.force_download)
if 8 * struct.calcsize("P") == 32:
self.osmconvert_path = fd_fct.get_tooling_win_path(['osmconvert'])
else:
self.osmconvert_path = fd_fct.get_tooling_win_path(
['osmconvert64-0.8.8p'])
def process_input(self, calc_border_countries):
"""
Process input: get relevant tiles and if border countries should be calculated
The three primary inputs are giving by a separate value each and have separate processing:
1. country name
2. x/y combinations
"""
log.info('-' * 80)
if self.o_input_data.country and self.o_input_data.xy_coordinates:
log.error(
"! country and X/Y coordinates are given. Only one of both is allowed!")
sys.exit()
# option 1: input a country as parameter, e.g. germany
if self.o_input_data.country:
log.info('# Input country: %s.', self.o_input_data.country)
# option 1a: use Geofabrik-URL to calculate the relevant tiles
if self.o_input_data.geofabrik_tiles:
self.force_processing = self.o_downloader.check_and_download_geofabrik_if_needed()
o_geofabrik = Geofabrik(self.o_input_data.country)
self.tiles = o_geofabrik.get_tiles_of_country()
# option 1b: use static json files in the repo to calculate relevant tiles
else:
json_file_path = get_path_to_static_tile_json(
self.o_input_data.country)
self.tiles = fd_fct.read_json_file(json_file_path)
# country name is the input argument
self.country_name = self.o_input_data.country
# option 2: input a x/y combinations as parameter, e.g. 134/88 or 133/88,130/100
elif self.o_input_data.xy_coordinates:
log.info(
'# Input X/Y coordinates: %s.', self.o_input_data.xy_coordinates)
# option 2a: use Geofabrik-URL to get the relevant tiles
if self.o_input_data.geofabrik_tiles:
sys.exit("X/Y coordinated via Geofabrik not implemented now")
# option 2b: use static json files in the repo to get relevant tiles
else:
xy_coordinates = get_xy_coordinates_from_input(
self.o_input_data.xy_coordinates)
# loop through x/y combinations and find each tile in the json files
self.find_tiles_for_xy_combinations(xy_coordinates)
# calc border country when input X/Y coordinates
calc_border_countries = True
# Build list of countries needed
self.border_countries = {}
if calc_border_countries:
self.calc_border_countries(calc_border_countries)
else:
self.border_countries[self.country_name] = {}
def find_tiles_for_xy_combinations(self, xy_coordinates):
"""
loop through x/y combinations and find each tile in the json files
"""
for xy_comb in xy_coordinates:
try:
self.tiles.append(get_tile_by_one_xy_combination_from_jsons(
xy_comb))
# country name is the X/Y combinations separated by minus
# >1 x/y combinations are separated by underscore
if not self.country_name:
self.country_name = f'{xy_comb["x"]}-{xy_comb["y"]}'
else:
self.country_name = f'{self.country_name}_{xy_comb["x"]}-{xy_comb["y"]}'
except TileNotFoundError:
pass
def check_and_download_files(self):
"""
trigger check of land_polygons and OSM map files if not existing or are not up-to-date
"""
self.o_downloader.tiles_from_json = self.tiles
self.o_downloader.border_countries = self.border_countries
force_processing = self.o_downloader.check_and_download_files_if_needed()
# if download is needed or force_processing given via input --> force_processing = True
if force_processing or self.o_input_data.force_processing or self.force_processing:
self.force_processing = True
else:
self.force_processing = False
def calc_border_countries(self, calc_border_countries):
"""
calculate relevant border countries for the given tiles
"""
log.info('-' * 80)
if calc_border_countries:
log.info('# Determine involved/border countries')
# Build list of countries needed
for tile in self.tiles:
for country in tile['countries']:
if country not in self.border_countries:
self.border_countries[country] = {}
# log.info('+ Count of involved countries: %s',
# len(self.border_countries))
for country in self.border_countries:
log.info('+ Involved country: %s', country)
if calc_border_countries and len(self.border_countries) > 1:
log.info('+ Border countries will be processed')
def filter_tags_from_country_osm_pbf_files(self):
"""
Filter tags from country osm.pbf files
"""
log.info('-' * 80)
log.info('# Filter tags from country osm.pbf files')
# Windows
if platform.system() == "Windows":
for key, val in self.border_countries.items():
out_file_o5m = os.path.join(USER_OUTPUT_DIR,
f'outFile-{key}.o5m')
out_file_o5m_filtered = os.path.join(USER_OUTPUT_DIR,
f'outFileFiltered-{key}.o5m')
out_file_o5m_filtered_names = os.path.join(USER_OUTPUT_DIR,
f'outFileFiltered-{key}-Names.o5m')
if not os.path.isfile(out_file_o5m_filtered) or self.force_processing is True:
log.info('+ Converting map of %s to o5m format', key)
cmd = [self.osmconvert_path]
cmd.extend(['-v', '--hash-memory=2500', '--complete-ways',
'--complete-multipolygons', '--complete-boundaries',
'--drop-author', '--drop-version'])
cmd.append(val['map_file'])
cmd.append('-o='+out_file_o5m)
run_subprocess_and_log_output(
cmd, '! Error in OSMConvert with country: {key}')
log.info(
'+ Filtering unwanted map objects out of map of %s', key)
cmd = [fd_fct.get_tooling_win_path(['osmfilter'])]
cmd.append(out_file_o5m)
cmd.append(
'--keep="' + translate_tags_to_keep(sys_platform=platform.system()) + '"')
cmd.append('--keep-tags=all type= layer= "' +
translate_tags_to_keep(sys_platform=platform.system()) + '"')
cmd.append('-o=' + out_file_o5m_filtered)
run_subprocess_and_log_output(
cmd, '! Error in OSMFilter with country: {key}')
cmd = [fd_fct.get_tooling_win_path(['osmfilter'])]
cmd.append(out_file_o5m)
cmd.append(
'--keep="' + translate_tags_to_keep(
name_tags=True, sys_platform=platform.system()) + '"')
cmd.append('--keep-tags=all type= name= layer= "' +
translate_tags_to_keep(
name_tags=True, sys_platform=platform.system()) + '"')
cmd.append('-o=' + out_file_o5m_filtered_names)
run_subprocess_and_log_output(
cmd, '! Error in OSMFilter with country: {key}')
os.remove(out_file_o5m)
val['filtered_file'] = out_file_o5m_filtered
val['filtered_file_names'] = out_file_o5m_filtered_names
# Non-Windows
else:
for key, val in self.border_countries.items():
out_file_o5m_filtered = os.path.join(USER_OUTPUT_DIR,
f'filtered-{key}.o5m.pbf')
out_file_o5m_filtered_names = os.path.join(USER_OUTPUT_DIR,
f'outFileFiltered-{key}-Names.o5m.pbf')
if not os.path.isfile(out_file_o5m_filtered) or self.force_processing is True:
log.info('+ Create filtered country file for %s', key)
# https://docs.osmcode.org/osmium/latest/osmium-tags-filter.html
cmd = ['osmium', 'tags-filter', '--remove-tags']
cmd.append(val['map_file'])
cmd.extend(translate_tags_to_keep(
sys_platform=platform.system()))
cmd.extend(['-o', out_file_o5m_filtered])
cmd.append('--overwrite')
run_subprocess_and_log_output(
cmd, '! Error in Osmium with country: {key}')
cmd = ['osmium', 'tags-filter', '--remove-tags']
cmd.append(val['map_file'])
cmd.extend(translate_tags_to_keep(
name_tags=True, sys_platform=platform.system()))
cmd.extend(['-o', out_file_o5m_filtered_names])
cmd.append('--overwrite')
run_subprocess_and_log_output(
cmd, '! Error in Osmium with country: {key}')
val['filtered_file'] = out_file_o5m_filtered
val['filtered_file_names'] = out_file_o5m_filtered_names
log.info('+ Filter tags from country osm.pbf files: OK')
def generate_land(self):
"""
Generate land for all tiles
"""
log.info('-' * 80)
log.info('# Generate land')
tile_count = 1
for tile in self.tiles:
land_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'land.shp')
out_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'land')
# create land.dbf, land.prj, land.shp, land.shx
if not os.path.isfile(land_file) or self.force_processing is True:
log.info(
'+ Generate land %s of %s for Coordinates: %s,%s', tile_count, len(self.tiles), tile["x"], tile["y"])
cmd = ['ogr2ogr', '-overwrite', '-skipfailures']
# Try to prevent getting outside of the +/-180 and +/- 90 degrees borders. Normally the +/- 0.1 are there to prevent white lines at border borders.
if tile["x"] == 255 or tile["y"] == 255 or tile["x"] == 0 or tile["y"] == 0:
cmd.extend(['-spat', f'{tile["left"]:.6f}',
f'{tile["bottom"]:.6f}',
f'{tile["right"]:.6f}',
f'{tile["top"]:.6f}'])
else:
cmd.extend(['-spat', f'{tile["left"]-0.1:.6f}',
f'{tile["bottom"]-0.1:.6f}',
f'{tile["right"]+0.1:.6f}',
f'{tile["top"]+0.1:.6f}'])
cmd.append(land_file)
cmd.append(LAND_POLYGONS_PATH)
run_subprocess_and_log_output(
cmd, f'! Error generating land for tile: {tile["x"]},{tile["y"]}')
# create land1.osm
if not os.path.isfile(out_file+'1.osm') or self.force_processing is True:
# Windows
if platform.system() == "Windows":
cmd = ['python', os.path.join(RESOURCES_DIR,
'shape2osm.py'), '-l', out_file, land_file]
# Non-Windows
else:
cmd = ['python', os.path.join(RESOURCES_DIR,
'shape2osm.py'), '-l', out_file, land_file]
run_subprocess_and_log_output(
cmd, f'! Error creating land.osm for tile: {tile["x"]},{tile["y"]}')
tile_count += 1
log.info('+ Generate land: OK')
def generate_sea(self):
"""
Generate sea for all tiles
"""
log.info('-' * 80)
log.info('# Generate sea')
tile_count = 1
for tile in self.tiles:
out_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'sea.osm')
if not os.path.isfile(out_file) or self.force_processing is True:
log.info(
'+ Generate sea %s of %s for Coordinates: %s,%s', tile_count, len(self.tiles), tile["x"], tile["y"])
with open(os.path.join(RESOURCES_DIR, 'sea.osm'), encoding="utf-8") as sea_file:
sea_data = sea_file.read()
# Try to prevent getting outside of the +/-180 and +/- 90 degrees borders. Normally the +/- 0.1 are there to prevent white lines at tile borders
if tile["x"] == 255 or tile["y"] == 255 or tile["x"] == 0 or tile["y"] == 0:
sea_data = sea_data.replace(
'$LEFT', f'{tile["left"]:.6f}')
sea_data = sea_data.replace(
'$BOTTOM', f'{tile["bottom"]:.6f}')
sea_data = sea_data.replace(
'$RIGHT', f'{tile["right"]:.6f}')
sea_data = sea_data.replace(
'$TOP', f'{tile["top"]:.6f}')
else:
sea_data = sea_data.replace(
'$LEFT', f'{tile["left"]-0.1:.6f}')
sea_data = sea_data.replace(
'$BOTTOM', f'{tile["bottom"]-0.1:.6f}')
sea_data = sea_data.replace(
'$RIGHT', f'{tile["right"]+0.1:.6f}')
sea_data = sea_data.replace(
'$TOP', f'{tile["top"]+0.1:.6f}')
with open(out_file, mode='w', encoding="utf-8") as output_file:
output_file.write(sea_data)
tile_count += 1
log.info('+ Generate sea: OK')
def split_filtered_country_files_to_tiles(self):
"""
Split filtered country files to tiles
"""
log.info('-' * 80)
log.info('# Split filtered country files to tiles')
tile_count = 1
for tile in self.tiles:
for country, val in self.border_countries.items():
if country not in tile['countries']:
continue
log.info(
'+ Splitting tile %s of %s for Coordinates: %s,%s from map of %s', tile_count, len(self.tiles), tile["x"], tile["y"], country)
out_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', f'split-{country}.osm.pbf')
out_file_names = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', f'split-{country}-names.osm.pbf')
out_merged = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'merged.osm.pbf')
if not os.path.isfile(out_merged) or self.force_processing is True:
# Windows
if platform.system() == "Windows":
cmd = [self.osmconvert_path,
'-v', '--hash-memory=2500']
cmd.append('-b='+f'{tile["left"]}' + ',' + f'{tile["bottom"]}' +
',' + f'{tile["right"]}' + ',' + f'{tile["top"]}')
cmd.extend(
['--complete-ways', '--complete-multipolygons', '--complete-boundaries'])
cmd.append(val['filtered_file'])
cmd.append('-o='+out_file)
run_subprocess_and_log_output(
cmd, f'! Error in Osmosis with country: {country}. Win/out_file')
cmd = [self.osmconvert_path,
'-v', '--hash-memory=2500']
cmd.append('-b='+f'{tile["left"]}' + ',' + f'{tile["bottom"]}' +
',' + f'{tile["right"]}' + ',' + f'{tile["top"]}')
cmd.extend(
['--complete-ways', '--complete-multipolygons', '--complete-boundaries'])
cmd.append(val['filtered_file_names'])
cmd.append('-o='+out_file_names)
run_subprocess_and_log_output(
cmd, '! Error in Osmosis with country: {country}. Win/out_file_names')
# Non-Windows
else:
cmd = ['osmium', 'extract']
cmd.extend(
['-b', f'{tile["left"]},{tile["bottom"]},{tile["right"]},{tile["top"]}'])
cmd.append(val['filtered_file'])
cmd.extend(['-s', 'smart'])
cmd.extend(['-o', out_file])
cmd.extend(['--overwrite'])
run_subprocess_and_log_output(
cmd, '! Error in Osmosis with country: {country}. macOS/out_file')
cmd = ['osmium', 'extract']
cmd.extend(
['-b', f'{tile["left"]},{tile["bottom"]},{tile["right"]},{tile["top"]}'])
cmd.append(val['filtered_file_names'])
cmd.extend(['-s', 'smart'])
cmd.extend(['-o', out_file_names])
cmd.extend(['--overwrite'])
run_subprocess_and_log_output(
cmd, '! Error in Osmosis with country: {country}. macOS/out_file_names')
log.info(val['filtered_file'])
tile_count += 1
log.info('+ Split filtered country files to tiles: OK')
def merge_splitted_tiles_with_land_and_sea(self, process_border_countries):
"""
Merge splitted tiles with land an sea
"""
log.info('-' * 80)
log.info('# Merge splitted tiles with land an sea')
tile_count = 1
for tile in self.tiles: # pylint: disable=too-many-nested-blocks
log.info(
'+ Merging tiles for tile %s of %s for Coordinates: %s,%s', tile_count, len(self.tiles), tile["x"], tile["y"])
out_tile_dir = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}')
out_file = os.path.join(out_tile_dir, 'merged.osm.pbf')
land_files = glob.glob(os.path.join(out_tile_dir, 'land*.osm'))
if not os.path.isfile(out_file) or self.force_processing is True:
# sort land* osm files
self.sort_osm_files(tile)
# Windows
if platform.system() == "Windows":
cmd = [self.osmosis_win_file_path]
loop = 0
# loop through all countries of tile, if border-countries should be processed.
# if border-countries should not be processed, only process the "entered" country
for country in tile['countries']:
if process_border_countries or country in self.border_countries:
cmd.append('--rbf')
cmd.append(os.path.join(
out_tile_dir, f'split-{country}.osm.pbf'))
cmd.append('workers=' + self.workers)
if loop > 0:
cmd.append('--merge')
cmd.append('--rbf')
cmd.append(os.path.join(
out_tile_dir, f'split-{country}-names.osm.pbf'))
cmd.append('workers=' + self.workers)
cmd.append('--merge')
loop += 1
for land in land_files:
cmd.extend(
['--rx', 'file='+os.path.join(out_tile_dir, f'{land}'), '--s', '--m'])
cmd.extend(
['--rx', 'file='+os.path.join(out_tile_dir, 'sea.osm'), '--s', '--m'])
cmd.extend(['--tag-transform', 'file=' + os.path.join(RESOURCES_DIR,
'tunnel-transform.xml'), '--wb', out_file, 'omitmetadata=true'])
# Non-Windows
else:
cmd = ['osmium', 'merge', '--overwrite']
# loop through all countries of tile, if border-countries should be processed.
# if border-countries should not be processed, only process the "entered" country
for country in tile['countries']:
if process_border_countries or country in self.border_countries:
cmd.append(os.path.join(
out_tile_dir, f'split-{country}.osm.pbf'))
cmd.append(os.path.join(
out_tile_dir, f'split-{country}-names.osm.pbf'))
for land in land_files:
cmd.append(land)
cmd.append(os.path.join(out_tile_dir, 'sea.osm'))
cmd.extend(['-o', out_file])
run_subprocess_and_log_output(
cmd, f'! Error in Osmosis with tile: {tile["x"]},{tile["y"]}')
tile_count += 1
log.info('+ Merge splitted tiles with land an sea: OK')
def sort_osm_files(self, tile):
"""
sort land*.osm files to be in this order: nodes, then ways, then relations.
this is mandatory for osmium-merge since:
https://github.com/osmcode/osmium-tool/releases/tag/v1.13.2
"""
log.info('-' * 80)
log.info('# Sorting land* osm files')
# get all land* osm files
land_files = glob.glob(os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'land*.osm'))
# Windows
if platform.system() == "Windows":
for land in land_files:
cmd = [self.osmosis_win_file_path]
cmd.extend(['--read-xml', 'file='+os.path.join(land)])
cmd.append('--sort')
cmd.extend(['--write-xml', 'file='+os.path.join(land)])
# Non-Windows
else:
for land in land_files:
cmd = ['osmium', 'sort', '--overwrite']
cmd.append(land)
cmd.extend(['-o', land])
run_subprocess_and_log_output(
cmd, f'Error in Osmosis with sorting land* osm files of tile: {tile["x"]},{tile["y"]}')
log.info('+ Sorting land* osm files: OK')
def create_map_files(self, save_cruiser, tag_wahoo_xml):
"""
Creating .map files
"""
log.info('-' * 80)
log.info('# Creating .map files')
# Number of threads to use in the mapwriter plug-in
threads = str(multiprocessing.cpu_count() - 1)
if int(threads) < 1:
threads = 1
tile_count = 1
for tile in self.tiles:
log.info(
'+ Creating map file for tile %s of %s for Coordinates: %s,%s', tile_count, len(self.tiles), tile["x"], tile["y"])
out_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}.map')
if not os.path.isfile(out_file+'.lzma') or self.force_processing is True:
merged_file = os.path.join(USER_OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'merged.osm.pbf')
# Windows
if platform.system() == "Windows":
cmd = [self.osmosis_win_file_path, '--rbf', merged_file,
'workers=' + self.workers, '--mw', 'file='+out_file]
# Non-Windows
else:
cmd = ['osmosis', '--rb', merged_file,
'--mw', 'file='+out_file]
cmd.append(
f'bbox={tile["bottom"]:.6f},{tile["left"]:.6f},{tile["top"]:.6f},{tile["right"]:.6f}')
cmd.append('zoom-interval-conf=10,0,17')
cmd.append('threads=' + threads)
# add path to tag-wahoo xml file
try:
cmd.append(
f'tag-conf-file={fd_fct.get_tag_wahoo_xml_path(tag_wahoo_xml)}')
except fd_fct.TagWahooXmlNotFoundError:
log.error(
'The tag-wahoo xml file was not found: ˚%s˚. Does the file exist and is your input correct?', tag_wahoo_xml)
sys.exit()
run_subprocess_and_log_output(
cmd, f'Error in creating map file via Osmosis with tile: {tile["x"]},{tile["y"]}. mapwriter plugin installed?')
# Windows
if platform.system() == "Windows":
cmd = [fd_fct.get_tooling_win_path(['lzma']), 'e', out_file,
out_file+'.lzma', f'-mt{threads}', '-d27', '-fb273', '-eos']
# Non-Windows
else:
# force overwrite of output file and (de)compress links
cmd = ['lzma', out_file, '-f']
# --keep: do not delete source file
if save_cruiser:
cmd.append('--keep')
run_subprocess_and_log_output(
cmd, f'! Error creating map files for tile: {tile["x"]},{tile["y"]}')
# Create "tile present" file
with open(out_file + '.lzma.17', mode='wb') as tile_present_file:
tile_present_file.close()
tile_count += 1
log.info('+ Creating .map files: OK')
def make_and_zip_files(self, extension, zip_folder):
"""
make or make and zip .map or .map.lzma files
extension: '.map.lzma' for Wahoo tiles
extension: '.map' for Cruiser map files
"""
if extension == '.map.lzma':
folder_name = self.country_name
else:
folder_name = self.country_name + '-maps'
log.info('-' * 80)
log.info('# Create: %s files', extension)
log.info('+ Country: %s', self.country_name)
# Check for us/utah etc names
try:
res = self.country_name.index('/')
self.country_name = self.country_name[res+1:]
except ValueError:
pass
# copy the needed tiles to the country folder
log.info('+ Copying %s tiles to output folders', extension)
for tile in self.tiles:
src = os.path.join(f'{USER_OUTPUT_DIR}',
f'{tile["x"]}', f'{tile["y"]}') + extension
dst = os.path.join(
f'{USER_WAHOO_MC}', folder_name, f'{tile["x"]}', f'{tile["y"]}') + extension
self.copy_to_dst(extension, src, dst)
if extension == '.map.lzma':
src = src + '.17'
dst = dst + '.17'
self.copy_to_dst(extension, src, dst)
if zip_folder:
# Windows
if platform.system() == "Windows":
cmd = [fd_fct.get_tooling_win_path(['7za']), 'a', '-tzip']
cmd.extend(
[folder_name + '.zip', os.path.join(".", folder_name, "*")])
# Non-Windows
else:
cmd = ['zip', '-r']
cmd.extend(
[folder_name + '.zip', folder_name])
run_subprocess_and_log_output(
cmd, f'! Error zipping map files for folder: {folder_name}', cwd=USER_WAHOO_MC)
# Delete the country/region map folders after compression
try:
shutil.rmtree(os.path.join(
f'{USER_WAHOO_MC}', folder_name))
except OSError:
log.error(
'! Error, could not delete folder %s', os.path.join(USER_WAHOO_MC, folder_name))
log.info('+ Zip %s files: OK', extension)
log.info('+ Create %s files: OK', extension)
def copy_to_dst(self, extension, src, dst):
"""
Zip .map or .map.lzma files
postfix: '.map.lzma' for Wahoo tiles
postfix: '.map' for Cruiser map files
"""
outdir = os.path.dirname(dst)
# first create the to-directory if not already there
os.makedirs(outdir, exist_ok=True)
try:
shutil.copy2(src, dst)
except Exception as exception: # pylint: disable=broad-except
log.error(
'! Error copying %s files for country %s: %s', extension, self.country_name, exception)
sys.exit()