forked from Hagellach37/osm_monitoring_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_osm_data.py
88 lines (67 loc) · 2.41 KB
/
merge_osm_data.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
#!/bin/python
# -*- coding: UTF-8 -*-
# Author: B. Herfort, 2016, GIScience Heidelberg
###########################################
import sys
import os
import time
def get_all_files(directory,extension):
#create list for all selected files
selected_files = []
#iterate over all items in directory and check file extension
for item in os.listdir(directory):
#change directory
os.chdir(directory)
#get file extension of current item
item_ext = item.split(".")[-1]
#check whether item is a file and has right file extension
if os.path.isfile(item) and item_ext == extension:
selected_files.append(item)
return selected_files
def main(directory,output_file,extension):
#Take start time
start_time = time.time()
selected_files = get_all_files(directory,extension)
cmd_middle = ''
cmd_suffix = ''
for i in range(0,len(selected_files)):
#copy first file
if i == 0:
filename = directory + '/' + selected_files[i]
cwd = os.getcwd()
osmosis = os.path.dirname(cwd) + '/osmosis-latest/bin/osmosis.bat'
cmd_1 = osmosis + ' --read-xml file="'+filename+'" --sort --write-xml file="'+output_file+'"'
os.system(cmd_1)
else:
filename = directory + '/' + selected_files[i]
cmd_middle = cmd_middle + '--read-xml file="'+filename+'" --sort '
cmd_suffix = cmd_suffix + '--merge '
if (i%50 == 0 ) or (i == (len(selected_files)-1)):
print(i)
osmosis = os.path.dirname(cwd) + '/osmosis-latest/bin/osmosis.bat'
cmd_prefix = osmosis +' --read-xml file="'+output_file+'" --sort '
cmd = cmd_prefix + cmd_middle + cmd_suffix + ' --write-xml file="'+output_file+'" -q'
os.system(cmd)
cmd = ''
cmd_middle = ''
cmd_suffix = ''
#Take end time and calculate program run time
end_time = time.time()
run_time = end_time - start_time
print('############ END ######################################')
print('##')
print('## output file: '+output_file)
print('##')
print('## runtime: '+str(run_time)+' s')
print('##')
print('## B. Herfort, GIScience Research Group')
print('##')
print('#######################################################')
if __name__ == "__main__":
#
# example run : $ python merge_osm_data.py D:/temp/osm_data merge.osm osm
#
if len( sys.argv ) != 4:
print("[ ERROR ] you must supply 3 arguments: osm_data_directory output_file file_extension")
sys.exit( 1 )
main( sys.argv[1], sys.argv[2], sys.argv[3])