-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.lua
85 lines (63 loc) · 1.79 KB
/
gen.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
local open = io.open
local tag_key = {} -- table to collect fields
function fromCSV (s)
s = s .. ';' -- ending comma
local t = {} -- table to collect fields
local fieldstart = 1
repeat
local nexti = string.find(s, ';', fieldstart)
table.insert(t, string.sub(s, fieldstart, nexti-1))
fieldstart = nexti + 1
until fieldstart > string.len(s)
return t
end
function trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local wayid = 1
local nodeid = 1
function createNode(id)
lat = id * 0.0001
return '<node id="'..id..'" lat="'..lat..'" lon="10" /><node id="'..id + 1 ..'" lat="'..lat..'" lon="11" />\n'
end
local nodes_string = ""
local ways_string = ""
function createXML(tb)
nodes_string = nodes_string .. createNode(nodeid)
ways_string = ways_string .. '<way id="' .. wayid .. '"><nd ref="' .. nodeid .. '"/><nd ref="' .. nodeid + 1 .. '"/>'
wayid = wayid + 1
nodeid = nodeid + 2
for k, v in pairs(tb) do
ways_string = ways_string .. '<tag k="' .. k .. '" v="' .. v .. '"/>'
end
ways_string = ways_string .. '</way>\n'
end
read_file = function (path)
local file = io.open(path, "rb")
if not file then return nil end
local lines = {}
for line in io.lines(path) do
t = fromCSV(line)
if #tag_key == 0 then
tag_key = t
else
tb = {}
for i, v in pairs(t) do
tv = trim1(v)
if tv ~= "" then
tb[tag_key[i]] = tv
--print(i, string.byte(v))
end
end
createXML(tb)
end
end
file:close()
return lines;
end
print([[<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">]])
read_file("highway.csv")
print(nodes_string)
print(ways_string)
print([[</osm>]])