-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonenumber.go
169 lines (149 loc) · 3.84 KB
/
phonenumber.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gophonenumbers
import (
"fmt"
"io"
"os"
"path"
"strconv"
"github.com./grokify/mogo/encoding/csvutil"
"github.com./grokify/mogo/sort/sortutil"
geo "github.com./kellydunn/golang-geo"
)
const (
A2gCsvRelPath = "github.com./grokify/mogo/strconv/phonenumber/us-area-code-geo.csv"
)
type AreaCodeInfo struct {
AreaCode uint16
Point *geo.Point
}
// NewAreaCodeInfoStrings returns an AreaCodeInfo based on string area code,
// lat and lon values.
func NewAreaCodeInfoStrings(ac, lat, lon string) (AreaCodeInfo, error) {
aci := AreaCodeInfo{}
i, err := strconv.Atoi(ac)
if err != nil {
return aci, err
}
if i < 100 || i > 999 {
return aci, fmt.Errorf("invalid Area Code [%v]", i)
}
aci.AreaCode = uint16(i)
geo, err := NewPointString(lat, lon)
if err != nil {
return aci, err
}
aci.Point = geo
return aci, nil
}
// NewPointString returns a *geo.Point based on string lat and lon values.
func NewPointString(lat string, lon string) (*geo.Point, error) {
f1, err := strconv.ParseFloat(lat, 64)
if err != nil {
return geo.NewPoint(0, 0), err
}
f2, err := strconv.ParseFloat(lon, 64)
if err != nil {
return geo.NewPoint(0, 0), err
}
return geo.NewPoint(f1, f2), nil
}
type AreaCodeToGeo struct {
AreaCodeInfos map[uint16]AreaCodeInfo
DistanceMatrix map[uint16]map[uint16]float64
}
func NewAreaCodeToGeo() AreaCodeToGeo {
return AreaCodeToGeo{AreaCodeInfos: map[uint16]AreaCodeInfo{}}
}
func (a2g *AreaCodeToGeo) ReadData() error {
return a2g.ReadCsvPath(A2gCsvFullPath())
}
func (a2g *AreaCodeToGeo) ReadCsvPath(csvpath string) error {
csv, file, err := csvutil.NewReaderFile(A2gCsvFullPath(), ',')
if err != nil {
return err
}
for {
rec, errx := csv.Read()
if errx == io.EOF {
break
} else if errx != nil {
err = errx
break
} else if len(rec) != 3 {
err = fmt.Errorf("bad LatLon Data [%v]", rec)
break
}
aci, errx := NewAreaCodeInfoStrings(rec[0], rec[1], rec[2])
if errx != nil {
err = errx
break
}
a2g.AreaCodeInfos[aci.AreaCode] = aci
}
file.Close()
if err != nil {
return err
}
a2g.Inflate()
return nil
}
func (a2g *AreaCodeToGeo) AreaCodeSlice() []AreaCodeInfo {
acSlice := []AreaCodeInfo{}
for _, aci := range a2g.AreaCodeInfos {
acSlice = append(acSlice, aci)
}
return acSlice
}
func (a2g *AreaCodeToGeo) AreaCodes() []uint16 {
acSlice := []uint16{}
for _, aci := range a2g.AreaCodeInfos {
acSlice = append(acSlice, aci.AreaCode)
}
return acSlice
}
func (a2g *AreaCodeToGeo) AreaCodesSorted() []uint16 {
acs := a2g.AreaCodes()
sortutil.Slice(acs)
return acs
}
func (a2g *AreaCodeToGeo) Inflate() {
a2g.DistanceMatrix = a2g.GetDistanceMatrix()
}
func (a2g *AreaCodeToGeo) GetDistanceMatrix() map[uint16]map[uint16]float64 {
acis := a2g.AreaCodeSlice()
distanceMatrix := map[uint16]map[uint16]float64{}
l := len(acis)
for i := 0; i < l; i++ {
for j := i + 1; j < l; j++ {
ac1 := acis[i]
ac2 := acis[j]
gcd := ac1.Point.GreatCircleDistance(ac2.Point)
if _, ok := distanceMatrix[ac1.AreaCode]; !ok {
distanceMatrix[ac1.AreaCode] = map[uint16]float64{}
}
distanceMatrix[ac1.AreaCode][ac2.AreaCode] = gcd
if _, ok := distanceMatrix[ac2.AreaCode]; !ok {
distanceMatrix[ac2.AreaCode] = map[uint16]float64{}
}
distanceMatrix[ac2.AreaCode][ac1.AreaCode] = gcd
}
}
return distanceMatrix
}
func (a2g *AreaCodeToGeo) GcdAreaCodes(ac1Int uint16, ac2Int uint16) (float64, error) {
ac1, ok := a2g.AreaCodeInfos[ac1Int]
if !ok {
return 0, fmt.Errorf("areacode not found [%v]", ac1Int)
}
ac2, ok := a2g.AreaCodeInfos[ac2Int]
if !ok {
return 0, fmt.Errorf("areacode not found [%v]", ac2Int)
}
dist2 := ac1.Point.GreatCircleDistance(ac2.Point)
return dist2, nil
}
// A2gCsvFullPath reads data from:
// https://github.com./ravisorg/Area-Code-Geolocation-Database
func A2gCsvFullPath() string {
return path.Join(os.Getenv("GOPATH"), "src", A2gCsvRelPath)
}