-
Notifications
You must be signed in to change notification settings - Fork 24
/
osm_maps_functions.py
670 lines (550 loc) · 28 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
"""
functions and object for managing OSM maps
"""
#!/usr/bin/python
# import official python packages
import glob
import multiprocessing
import os
import subprocess
import sys
import platform
import shutil
# import custom python packages
from common_python import file_directory_functions as fd_fct
from common_python import constants
from common_python import constants_functions as const_fct
from common_python.downloader import Downloader
from common_python.geofabrik import Geofabrik
class OsmMaps:
"""
This is a OSM data class
"""
osmosis_win_file_path = os.path.join(
fd_fct.TOOLING_WIN_DIR, 'Osmosis', 'bin', 'osmosis.bat')
def __init__(self, oInputData):
self.force_processing = ''
# Number of workers for the Osmosis read binary fast function
self.workers = '1'
self.tiles = []
self.border_countries = {}
self.country_name = ''
self.o_input_data = oInputData
self.o_downloader = Downloader(
oInputData.max_days_old, oInputData.force_download)
def process_input(self, input_argument, calc_border_countries):
"""
get relevant tiles for given input and calc border countries of these tiles
"""
# logging
print(f'+ Input country or json file: {input_argument}.')
# option 1: have a .json file as input parameter
if os.path.isfile(input_argument):
self.tiles = fd_fct.read_json_file(input_argument)
# country name is the last part of the input filename
self.country_name = os.path.split(input_argument)[1][:-5]
# option 2: input a country as parameter, e.g. germany
else:
# option 2a: 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(input_argument)
self.tiles = o_geofabrik.get_tiles_of_country()
# option 2b: use static json files in the repo to calculate relevant tiles
else:
json_file_path = os.path.join(fd_fct.COMMON_DIR, 'json',
const_fct.get_region_of_country(input_argument), input_argument + '.json')
self.tiles = fd_fct.read_json_file(json_file_path)
# country name is the input argument
self.country_name = input_argument
# Build list of countries needed
self.border_countries = {}
if calc_border_countries or os.path.isfile(input_argument):
self.calc_border_countries()
else:
self.border_countries[self.country_name] = {}
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):
"""
calculate relevant border countries for the given tiles
"""
# 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] = {}
# logging
print(f'+ Count of Border countries: {len(self.border_countries)}')
for country in self.border_countries:
print(f'+ Border country: {country}')
def filter_tags_from_country_osm_pbf_files(self):
"""
Filter tags from country osm.pbf files
"""
print('\n# 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(fd_fct.OUTPUT_DIR,
f'outFile-{key}.o5m')
out_file_o5m_filtered = os.path.join(fd_fct.OUTPUT_DIR,
f'outFileFiltered-{key}.o5m')
out_file_o5m_filtered_names = os.path.join(fd_fct.OUTPUT_DIR,
f'outFileFiltered-{key}-Names.o5m')
if not os.path.isfile(out_file_o5m_filtered) or self.force_processing is True:
print(f'\n+ Converting map of {key} to o5m format')
cmd = [os.path.join(fd_fct.TOOLING_WIN_DIR, 'osmconvert')]
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)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in OSMConvert with country: {key}')
sys.exit()
print(
f'\n# Filtering unwanted map objects out of map of {key}')
cmd = [os.path.join(fd_fct.TOOLING_WIN_DIR, 'osmfilter')]
cmd.append(out_file_o5m)
cmd.append('--keep="' + constants.FILTERED_TAGS_WIN + '"')
cmd.append('--keep-tags=all type= layer= "' +
constants.FILTERED_TAGS_WIN + '"')
cmd.append('-o=' + out_file_o5m_filtered)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in OSMFilter with country: {key}')
sys.exit()
cmd = [os.path.join(fd_fct.TOOLING_WIN_DIR, 'osmfilter')]
cmd.append(out_file_o5m)
cmd.append(
'--keep="' + constants.FILTERED_TAGS_WIN_NAMES + '"')
cmd.append('--keep-tags=all type= name= layer= "' +
constants.FILTERED_TAGS_WIN_NAMES + '"')
cmd.append('-o=' + out_file_o5m_filtered_names)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in OSMFilter with country: {key}')
sys.exit()
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(fd_fct.OUTPUT_DIR,
f'filtered-{key}.o5m.pbf')
out_file_o5m_filtered_names = os.path.join(fd_fct.OUTPUT_DIR,
f'outFileFiltered-{key}-Names.o5m.pbf')
if not os.path.isfile(out_file_o5m_filtered) or self.force_processing is True:
print(f'+ Create filtered country file for {key}')
# https://docs.osmcode.org/osmium/latest/osmium-tags-filter.html
cmd = ['osmium', 'tags-filter', '--remove-tags']
cmd.append(val['map_file'])
cmd.extend(constants.FILTERED_TAGS)
cmd.extend(['-o', out_file_o5m_filtered])
cmd.append('--overwrite')
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmium with country: {key}')
sys.exit()
cmd = ['osmium', 'tags-filter', '--remove-tags']
cmd.append(val['map_file'])
cmd.extend(constants.FILTERED_TAGS_NAMES)
cmd.extend(['-o', out_file_o5m_filtered_names])
cmd.append('--overwrite')
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmium with country: {key}')
sys.exit()
val['filtered_file'] = out_file_o5m_filtered
val['filtered_file_names'] = out_file_o5m_filtered_names
# logging
print('# Filter tags from country osm.pbf files: OK')
def generate_land(self):
"""
Generate land for all tiles
"""
print('\n# Generate land')
tile_count = 1
for tile in self.tiles:
land_file = os.path.join(fd_fct.OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'land.shp')
out_file = os.path.join(fd_fct.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:
print(
f'+ Generate land {tile_count} of {len(self.tiles)} for Coordinates: {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(fd_fct.LAND_POLYGONS_PATH)
subprocess.run(cmd, check=True)
# 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(fd_fct.TOOLING_DIR,
'shape2osm.py'), '-l', out_file, land_file]
# new shapefile for python 3 ?!
# 'shape2osm', 'shape2osm.py'), land_file, out_file+'1.osm']
# Non-Windows
else:
cmd = ['python3', os.path.join(fd_fct.TOOLING_DIR,
'shape2osm.py'), '-l', out_file, land_file]
subprocess.run(cmd, check=True)
tile_count += 1
# logging
print('# Generate land: OK')
def generate_sea(self):
"""
Generate sea for all tiles
"""
print('\n# Generate sea')
tile_count = 1
for tile in self.tiles:
out_file = os.path.join(fd_fct.OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', 'sea.osm')
if not os.path.isfile(out_file) or self.force_processing is True:
print(
f'+ Generate sea {tile_count} of {len(self.tiles)} for Coordinates: {tile["x"]},{tile["y"]}')
with open(os.path.join(fd_fct.TOOLING_DIR, 'sea.osm')) 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, 'w') as output_file:
output_file.write(sea_data)
tile_count += 1
# logging
print('# Generate sea: OK')
def split_filtered_country_files_to_tiles(self):
"""
Split filtered country files to tiles
"""
print('\n# Split filtered country files to tiles')
tile_count = 1
for tile in self.tiles:
for country, val in self.border_countries.items():
print(
f'+ Splitting tile {tile_count} of {len(self.tiles)} for Coordinates: {tile["x"]},{tile["y"]} from map of {country}')
out_file = os.path.join(fd_fct.OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', f'split-{country}.osm.pbf')
out_file_names = os.path.join(fd_fct.OUTPUT_DIR,
f'{tile["x"]}', f'{tile["y"]}', f'split-{country}-names.osm.pbf')
out_merged = os.path.join(fd_fct.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 = [os.path.join(
fd_fct.TOOLING_WIN_DIR, 'osmconvert'), '-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)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmosis with country: {country}')
sys.exit()
cmd = [os.path.join(
fd_fct.TOOLING_WIN_DIR, 'osmconvert'), '-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)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmosis with country: {country}')
sys.exit()
# 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'])
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmium with country: {country}')
sys.exit()
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'])
# print(cmd)
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(f'Error in Osmium with country: {country}')
sys.exit()
print(val['filtered_file'])
tile_count += 1
print('# Split filtered country files to tiles: OK')
def merge_splitted_tiles_with_land_and_sea(self, calc_border_countries):
"""
Merge splitted tiles with land an sea
"""
print('\n# Merge splitted tiles with land an sea')
tile_count = 1
for tile in self.tiles:
print(
f'+ Merging tiles for tile {tile_count} of {len(self.tiles)} for Coordinates: {tile["x"]},{tile["y"]}')
out_tile_dir = os.path.join(fd_fct.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 calc_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(fd_fct.COMMON_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 calc_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])
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(
f'Error in Osmosis with tile: {tile["x"]},{tile["y"]}')
sys.exit()
tile_count += 1
# logging
print('# 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
"""
print('\n# Sorting land* osm files')
# get all land* osm files
land_files = glob.glob(os.path.join(fd_fct.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])
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(
f'Error in Osmosis with sorting land* osm files of tile: {tile["x"]},{tile["y"]}')
sys.exit()
# logging
print('# Sorting land* osm files: OK')
def create_map_files(self, save_cruiser, tag_wahoo_xml):
"""
Creating .map files
"""
print('\n# 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:
print(
f'+ Creating map file for tile {tile_count} of {len(self.tiles)} for Coordinates: {tile["x"]},{tile["y"]}')
out_file = os.path.join(fd_fct.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(fd_fct.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)
# should work on macOS and Windows
cmd.append(
f'tag-conf-file={os.path.join(fd_fct.COMMON_DIR, "tag_wahoo_adjusted", tag_wahoo_xml)}')
result = subprocess.run(cmd, check=True)
if result.returncode != 0:
print(
f'Error in Osmosis with country: c // tile: {tile["x"]}, {tile["y"]}')
sys.exit()
# Windows
if platform.system() == "Windows":
cmd = [os.path.join(fd_fct.TOOLING_WIN_DIR, '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')
subprocess.run(cmd, check=True)
# Create "tile present" file
with open(out_file + '.lzma.12', 'wb') as tile_present_file:
tile_present_file.close()
tile_count += 1
# logging
print('# Creating .map files: OK')
def make_and_zip_files(self, keep_map_folders, extension):
"""
Zip .map or .map.lzma files
postfix: '.map.lzma' for Wahoo tiles
postfix: '.map' for Cruiser map files
"""
if extension == '.map.lzma':
folder_name = self.country_name
else:
folder_name = self.country_name + '-maps'
print(f'\n# Zip: {extension} files')
print(f'+ Country: {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
print(f'Copying {extension} tiles to output folders')
for tile in self.tiles:
src = os.path.join(f'{fd_fct.OUTPUT_DIR}',
f'{tile["x"]}', f'{tile["y"]}') + extension
dst = os.path.join(
f'{fd_fct.OUTPUT_DIR}', folder_name, f'{tile["x"]}', f'{tile["y"]}') + extension
outdir = os.path.join(
f'{fd_fct.OUTPUT_DIR}', folder_name, f'{tile["x"]}')
if not os.path.isdir(outdir):
os.makedirs(outdir)
try:
shutil.copy2(src, dst)
except:
print(
f'Error copying {extension} files of country {self.country_name}')
sys.exit()
if extension == '.map.lzma':
src = src + '.12'
dst = dst + '.12'
if not os.path.isdir(outdir):
os.makedirs(outdir)
try:
shutil.copy2(src, dst)
except:
print(
f'Error copying precense files of country {self.country_name}')
sys.exit()
# Windows
if platform.system() == "Windows":
path_7za = os.path.join(fd_fct.TOOLING_WIN_DIR, '7za')
cmd = [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])
subprocess.run(cmd, cwd=fd_fct.OUTPUT_DIR, check=True)
# Keep (True) or delete (False) the country/region map folders after compression
if keep_map_folders is False:
try:
shutil.rmtree(os.path.join(
f'{fd_fct.OUTPUT_DIR}', folder_name))
except OSError:
print(
f'Error, could not delete folder \
{os.path.join(fd_fct.OUTPUT_DIR, folder_name)}')
# logging
print('# Zip .map.lzma files: OK \n')