-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmsplitter.lua
130 lines (113 loc) · 3.25 KB
/
kmsplitter.lua
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
-- functions needed
-- trwlayer:waypoints() -> iterator wpname wp
-- wp:comment()
-- track:trackpoints() -> iterator
-- tp:coord()
-- wp:coord()
-- Viking.dist(coord1, coord2)
--
--
-- Later we can have unified types with a "we generated this so should free it in gc()" flag
--
--function run(vtl)
-- for trname, tr in vtl:tracks() do
-- io.write("track::: " .. trname .. "\n")
-- end
-- for wpname, wp in vtl:waypoints() do
-- cmt = wp:comment()
-- if cmt then
-- io.write("waypoint::: " .. wpname .. " = " .. cmt .. "\n")
-- end
-- end
--end
-- functions needed
-- trwlayer:waypoints() -> iterator wpname wp
-- track:trackpoints() -> iterator
-- tp:coord()
-- wp:coord()
-- Viking.dist(coord1, coord2)
-- wp:comment()
--
--
-- Later we can have unified types with a "we generated this so should free it in gc()" flag
--
-- Used to escape "'s by toCSV
function escape_csv(orig_s)
s = tostring(orig_s)
if string.find(s, '[,"]') or (type(orig_s) == "string" and string.find(s, "^[0-9\\.]+$")) then
s = '"' .. string.gsub(s, '"', '""') .. '"'
end
return s
end
function map(func, tbl)
local newtbl = {}
for i,v in pairs(tbl) do
newtbl[i] = func(v)
end
return newtbl
end
function csv_line(tbl)
return table.concat(map(escape_csv, tbl), ",") .. "\n"
end
function write_coord(coord, newsegment, outfile)
newsegment = newsegment and " newsegment=\"yes\"" or ""
write("type=\"trackpoint\" latitude=\"" .. coord:lat() .. "\" longitude=\"" .. coord:lon() .. "\"" .. newsegment .. "\n", outfile)
end
function write(s, outfile)
if outfile then
outfile:write(s)
end
io.write(s)
end
--Written for 5.0; could be made slightly cleaner with 5.1
--Splits a string based on a separator string or pattern;
--returns an array of pieces of the string.
--(May optionally supply a table as the third parameter which will be filled with the results.)
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern,
theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function run(vw, thresholds, outfilename)
thresholds = map(function(t) return(t * 1000) end, thresholds:split(","))
local i = 1
local vtl = vw:top_trwlayer()
local newsegment = true
local outfile = nil
if (outfilename) then
outfile = io.open(outfilename, "w")
end
write("type=\"track\" name=\"luaoutput\"\n", outfile)
local thisfile = string.sub(debug.getinfo(1).source,2)
local track = nil
for trname, tr in vtl:tracks() do
track = tr
end
local len = 0
local last_coord = nil
for tp in track:trackpoints() do
if last_coord then
len = len + coord.diff(tp:coord(), last_coord)
end
last_coord = tp:coord()
if thresholds[i] and len > thresholds[i] then
i = i + 1
newsegment = true
end
if (i % 2) == 0 then
write_coord(tp:coord(), newsegment, outfile)
newsegment = false
end
end
if outfile then outfile:close() end
end