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
import ../types
import ./products
import json
import options
import tables
proc parsePoint*(loc: JsonNode): Point =
let typeStr = loc{"type"}.getStr()
if typeStr == "S":
result.stop = some(Stop(
id: loc{"extId"}.getStr(),
name: loc{"name"}.getStr(),
location: Location(
latitude: loc{"crd"}{"y"}.getInt() / 1000000,
longitude: loc{"crd"}{"x"}.getInt() / 1000000,
),
products: parseProducts(loc{"pCls"}.getInt()),
))
elif typeStr == "P":
result.location = some(Location(
id: some(loc{"extId"}.getStr()),
name: some(loc{"name"}.getStr()),
latitude: loc{"crd"}{"y"}.getInt() / 1000000,
longitude: loc{"crd"}{"x"}.getInt() / 1000000,
))
elif typeStr == "A":
result.location = some(Location(
address: some(loc{"name"}.getStr()),
latitude: loc{"crd"}{"y"}.getInt() / 1000000,
longitude: loc{"crd"}{"x"}.getInt() / 1000000,
))
elif typeStr == "":
return
else:
raise newException(CatchableError, "Unimplemented hafas loc type: " & typeStr)
proc formatLocationIdentifier(d: Table[string, string]): string =
for key, val in d:
result &= key
result &= "="
result &= val
result &= "@"
proc formatCoord(c: float): string =
return $int(c * 1000000)
proc formatPoint*(point: Point): JsonNode =
if point.stop.isSome:
let stop = point.stop.get
return %* {
"type": "S",
"lid": formatLocationIdentifier({
"A": "1",
"L": $stop.id,
}.toTable),
}
elif point.location.isSome:
let loc = point.location.get
if loc.address.isSome:
return %* {
"type": "A",
"lid": formatLocationIdentifier({
"A": "2",
"O": loc.address.get,
"X": formatCoord(loc.longitude),
"Y": formatCoord(loc.latitude),
}.toTable),
}
elif loc.name.isSome and loc.id.isSome:
return %* {
"type": "P",
"lid": formatLocationIdentifier({
"A": "4",
"O": loc.address.get,
"L": loc.id.get,
"X": formatCoord(loc.longitude),
"Y": formatCoord(loc.latitude),
}.toTable),
}
raise newException(CatchableError, "Cannot format HAFAS location")