-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreets_util.go
91 lines (74 loc) · 2.38 KB
/
streets_util.go
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
package gohoa
import (
"encoding/json"
"log"
"os"
"strings"
"sync"
)
// Gloabl variable, in memory at all times for street lookup
var streetNameToID = map[string]string{"Contrivy": "CTV", "Central": "CTL"}
// This mapping shouild be stored in the streets.json file
var shortIdToStreetMapping = map[string]StreetMapping{
"CTV": {StreetName: "Contrivy", ShortID: "CTV", TrieNames: []string{"contrivy"}, FullName: "Contrivy Rd."},
"CTL": {StreetName: "Central", ShortID: "CTL", TrieNames: []string{"central"}, FullName: "Central Ave."},
}
var trieNameMap = map[string]StreetMapping{}
var loadOnce sync.Once
type StreetMapping struct {
StreetName string `json:"name"`
FullName string `json:"fullname"`
ShortID string `json:"code"`
TrieNames []string `json:"trienames"`
}
func LoadStreetMappingsJson() {
loadOnce.Do(func() {
if GetConfig().StreetsJson == "" {
log.Println("Didn't find streets.json file, not loading from disk")
return
}
file, err := os.Open(GetConfig().StreetsJson)
if err != nil {
log.Fatalf("Cannot open streets json file %s eror %s\n", GetConfig().StreetsJson, err.Error())
}
defer file.Close()
var streetMappings []StreetMapping
decoder := json.NewDecoder(file)
if err = decoder.Decode(&streetMappings); err != nil {
log.Fatalf("Cannot unmarshall file %s into stucts error %s\n", GetConfig().StreetsJson, err.Error())
} else {
for _, sm := range streetMappings {
streetNameToID[sm.StreetName] = sm.ShortID
shortIdToStreetMapping[sm.ShortID] = sm
//Populate the trieNameMap so full street info retrieval from trie is possible
if sm.TrieNames != nil {
for _, name := range sm.TrieNames {
trieNameMap[name] = sm
}
} else {
lowerName := strings.ToLower(sm.StreetName)
trieNameMap[lowerName] = sm
}
}
}
})
}
func GetShortIDFromStreetName(streetName string) string {
return streetNameToID[streetName]
}
func GetStreetMappingFromShortID(shortId string) StreetMapping {
return shortIdToStreetMapping[shortId]
}
func GetStreetMap() map[string]StreetMapping {
return shortIdToStreetMapping
}
func GetStreetMappingFromTrieName(trieName string) StreetMapping {
return trieNameMap[trieName]
}
func GetAllStreetMappings() []StreetMapping {
var streetMappings []StreetMapping
for _, sm := range shortIdToStreetMapping {
streetMappings = append(streetMappings, sm)
}
return streetMappings
}