-
Notifications
You must be signed in to change notification settings - Fork 0
/
communes.go
130 lines (110 loc) · 3.29 KB
/
communes.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
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
129
130
package databases
import (
"encoding/csv"
"os"
"strconv"
"sync"
"github.com/NoelM/minigo/notel/logs"
)
type Commune struct {
CodeCommune string `json:"code_commune"`
NomCommune string `json:"nom_commune"`
CodePostal string `json:"code_postal"`
CodeDepartement string `json:"code_departement"`
NomDepartement string `json:"nom_departement"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type CommuneDatabase struct {
mutex sync.RWMutex
CodePostalToCommune map[string][]Commune
}
func NewCommuneDatabase() *CommuneDatabase {
return &CommuneDatabase{
CodePostalToCommune: make(map[string][]Commune),
}
}
func (c *CommuneDatabase) GetCommunesFromCodePostal(codePostal string) []Commune {
if codePostal[0] == '0' {
codePostal = codePostal[1:]
}
c.mutex.RLock()
defer c.mutex.RUnlock()
communes, ok := c.CodePostalToCommune[codePostal]
if !ok {
return nil
}
return communes
}
func (c *CommuneDatabase) LoadCommuneDatabase(filePath string) error {
const codeCommuneColId = 0
const nomCommuneColId = 10
const codePostalColId = 2
const latitudeColId = 5
const longitudeColId = 6
const codeDepartementColId = 11
const nomDepartementColId = 12
communesFile, err := os.Open(filePath)
if err != nil {
logs.ErrorLog("unable to open file at: %s\n", filePath)
return err
}
defer communesFile.Close()
fileReader := csv.NewReader(communesFile)
fileReader.Comma = ','
communeRecords, err := fileReader.ReadAll()
if err != nil {
logs.ErrorLog("unable to read commune CSV records: %s\n", err.Error())
return err
}
c.mutex.Lock()
defer c.mutex.Unlock()
for _, record := range communeRecords {
communeName := record[nomCommuneColId]
codePostal := record[codePostalColId]
codeDepartement := record[codeDepartementColId]
parsedDepCode, err := strconv.ParseInt(codeDepartement, 10, 32)
if err != nil && codeDepartement != "2A" && codeDepartement != "2B" {
logs.WarnLog("unable to parse code departement for commune %s: %s\n", communeName, err.Error())
continue
} else if parsedDepCode > 95 {
//logs.InfoLog("ignore commune %s because out of metropole\n", communeName)
continue
}
lat, err := strconv.ParseFloat(record[latitudeColId], 32)
if err != nil {
logs.WarnLog("unable to parse latitude for commune %s: %s\n", communeName, err.Error())
continue
}
lon, err := strconv.ParseFloat(record[longitudeColId], 32)
if err != nil {
logs.WarnLog("unable to parse longitude for commune %s: %s\n", communeName, err.Error())
continue
}
_, ok := c.CodePostalToCommune[codePostal]
if !ok {
c.CodePostalToCommune[codePostal] = []Commune{
{
CodeCommune: record[codeCommuneColId],
NomCommune: communeName,
CodePostal: codePostal,
CodeDepartement: codeDepartement,
NomDepartement: record[nomDepartementColId],
Latitude: lat,
Longitude: lon,
},
}
} else {
c.CodePostalToCommune[codePostal] = append(c.CodePostalToCommune[codePostal], Commune{
CodeCommune: record[codeCommuneColId],
NomCommune: communeName,
CodePostal: codePostal,
CodeDepartement: codeDepartement,
NomDepartement: record[nomDepartementColId],
Latitude: lat,
Longitude: lon,
})
}
}
return nil
}