79 lines
2.2 KiB
Nim
79 lines
2.2 KiB
Nim
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,
|
|
))
|
|
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")
|