-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkatm_gpx.rb
245 lines (224 loc) · 5.36 KB
/
katm_gpx.rb
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
##################################################
#
# KML file convert to add on time GPX file ( Katm_gpx.rb )
#
# 2012/09/12 KINOSHITA Kenichi
#
##################################################
def timeset( setline )
ctime = setline.scan(/[\d]+/)
settime = Time.local( ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5])
return settime
end
def timecal( stime, etime)
iscaltm = ( etime - stime )
puts "logging time : #{iscaltm} seconds"
return iscaltm
end
#interval calculations
def invalcal( ipnt, itime )
puts "logging points : #{ipnt}"
ival = itime / ipnt
return ival
end
#Main Task
if ARGV.size >= 2
$name = "name"
$descr = "<description>"
$sdescr = "</description>"
$coord = "<coordinates>"
$scoord = "</coordinates>"
$point = "/Point"
$LS = "<LineString>"
$LSLST = "</LineString>"
$LAT = "lat="
$LON = "lon="
$ELEH = "<ele>"
$ELEHU = "</ele>"
$ELET = ".000000"
$DC = "\""
$TIME = "<time>"
$TIMEU = "</time>"
$comma = ","
$ELOG = "EXILIM LOG"
$cdata = "CDATA"
pflag = 0
sflag = 0
pline = 0
elog = 0
fTm = ""
stm = ""
etm = ""
spos = ""
epos = ""
tstmp = ""
# KML File Open
infile = ARGV[0]
begin
s = File::stat( infile )
rescue
puts "Convert master file not found!!"
exit!
end
inline = open( infile, "r")
# Time interval set
# GPX File Open
outfile = ARGV[1]
begin
s = File::stat( outfile )
rescue
else
puts "Convert file is exist!!"
exit!
end
outline = open( outfile, "w")
# Temp file Open
require 'tempfile'
temp = Tempfile::new("katm", "/tmp")
# Write GPX File Header
outline.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
outline.puts("<gpx version=\"1.0\">")
while( line = inline.gets )
nm = line if /#{$name}/
if /#{$ELOG}/ =~ line
elog = 1
end
if elog == 0
if /#{$descr}/ =~ line
if sflag == 0
starttm = timeset( line )
else
if sflag == 1
endtm = timeset( line )
end
end
sflag = sflag + 1
cm = line.gsub(/description+/,"cmt")
dm = cm.gsub(/cmt+/,"desc")
end
cod = line if /#{$coord}/
if /#{$point}/ =~ line
pos = cod.scan(/([\d\.]+)/)
ym = dm.scan(/([\d\.\:]+)/)
tm = dm.scan(/[\d]+/)
if sflag == 1
stm = starttm
end
# Write Start Point & Goal Point
outline.puts("<wpt #{$LAT}#{$DC}#{pos[1]}#{$DC} #{$LON}#{$DC}#{pos[0]}#{$DC} >#{$ELEH}#{pos[2]}#{$ELET}#{$ELEHU}")
outline.puts("#{nm}")
outline.puts("#{cm}")
outline.puts("#{dm}")
outline.puts("</wpt>")
# Write Start Point & Goal Point
end
if /#{$scoord}/ =~ line
pflag = 0
end
# Count Track Point Start
if pflag == 1
if /#{$comma}/ =~ line
pline = pline + 1
end
end
if /#{$LS}/ =~ line
pflag = 1
end
# Count Track Point End
elsif
if /#{$cdata}/ =~ line
cline = line.scan(/[\d]+/)
fTm = Time.local( cline[0], cline[1], cline[2], cline[3], cline[4], cline[5])
tstmp = fTm.strftime("%Y-%m-%dT%H:%M:%SZ")
if pline == 0
stm = fTm
elsif
etm = fTm
end
elsif /#{$coord}/ =~ line
if /#{$comma}/ =~ line
pos = line.scan(/([\d\.]+)/)
temp.puts("<trkpt #{$LAT}#{$DC}#{pos[1]}#{$DC} #{$LON}#{$DC}#{pos[0]}#{$DC}>")
temp.puts("#{$ELEH}#{pos[2]}#{$ELEHU}#{$TIME}#{tstmp}#{$TIMEU}")
temp.puts("</trkpt>")
if pline == 0
spos = pos
elsif
epos = pos
end
pline = pline + 1
end
end
end
end # Read KML File Loop END
inline.close
temp.close
# EXILIM LOG Write Start Point and End point
if elog == 1
temp.open
tstmp = stm.strftime("%Y/%m/%d-%H:%M:%S")
outline.puts("<wpt #{$LAT}#{$DC}#{spos[1]}#{$DC} #{$LON}#{$DC}#{spos[0]}#{$DC} >#{$ELEH}#{spos[2]}#{$ELEHU}")
outline.puts("<name> EXILIM LOG Start </name>")
outline.puts("<cmt> Start Time : #{tstmp} </cmt>")
outline.puts("<desc> Start Time : #{tstmp} </desc>")
outline.puts("</wpt>")
tstmp = etm.strftime("%Y/%m/%d-%H:%M:%S")
outline.puts("<wpt #{$LAT}#{$DC}#{epos[1]}#{$DC} #{$LON}#{$DC}#{epos[0]}#{$DC} >#{$ELEH}#{epos[2]}#{$ELEHU}")
outline.puts("<name> EXILIM LOG END </name>")
outline.puts("<cmt> End Time : #{tstmp} </cmt>")
outline.puts("<desc> End Time : #{tstmp} </desc>")
outline.puts("</wpt>")
outline.puts("<trk>")
outline.puts("<trkseg>")
end
if elog == 0
puts "Auto Make Time Stamps"
iruntm = timecal( starttm, endtm )
interval = invalcal( pline, iruntm )
puts "interval : #{interval}"
elsif
puts "Get EXILIM KML Time Data"
end
if elog == 0
# KML File reOpen
infile = ARGV[0]
inline = open( infile, "r")
pline = 0
while(line = inline.gets) # Write Track Point Loop
if /#{$scoord}/ =~ line
pflag = 0
end
# Write Track Point Start
if pflag == 1
if /#{$comma}/ =~ line
if pline > 0
stm = stm + interval
tstmp = stm.strftime("%Y-%m-%dT%H:%M:%SZ")
else
outline.puts("<trk>")
outline.puts("<trkseg>")
end
pos = line.scan(/([\d\.]+)/)
outline.puts("<trkpt #{$LAT}#{$DC}#{pos[1]}#{$DC} #{$LON}#{$DC}#{pos[0]}#{$DC}>")
outline.puts("#{$ELEH}#{pos[2]}#{$ELET}#{$ELEHU}#{$TIME}#{tstmp}#{$TIMEU}")
outline.puts("</trkpt>")
pline = pline + 1
end
end
# Write Track Point End
if /#{$LS}/ =~ line
pflag = 1
end
end # Read KML File Loop END
elsif
temp.each { |line| outline.puts(line) }
temp.close(true)
end
end
outline.puts("</trkseg>")
outline.puts("</trk>")
outline.puts("</gpx>")
if elog == 0
inline.close
end
outline.close