forked from fegoa89/zipcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipcodes.go
202 lines (172 loc) · 6.22 KB
/
zipcodes.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// zipcodes is a package that uses the GeoNames Postal Code dataset from http://www.geonames.org
// in order to perform zipcode lookup operations
package zipcodes
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
const (
earthRadiusKm = 6371
earthRadiusMi = 3958
)
// ZipCodeLocation struct represents each line of the dataset
type ZipCodeLocation struct {
ZipCode string
PlaceName string
AdminName string
State string
Lat float64
Lon float64
}
// Zipcodes contains the whole list of structs representing
// the zipcode dataset
type Zipcodes struct {
DatasetList map[string]ZipCodeLocation
}
// New loads the dataset that this packages uses and
// returns a struct that contains the dataset as a map interface
func New(datasetPath string) (*Zipcodes, error) {
zipcodes, err := LoadDataset(datasetPath)
if err != nil {
return nil, err
}
return &zipcodes, nil
}
// Lookup looks for a zipcode inside the map interface
func (zc *Zipcodes) Lookup(zipCode string) (*ZipCodeLocation, error) {
foundedZipcode := zc.DatasetList[zipCode]
if (foundedZipcode == ZipCodeLocation{}) {
return &ZipCodeLocation{}, fmt.Errorf("zipcodes: zipcode %s not found !", zipCode)
}
return &foundedZipcode, nil
}
// DistanceInKm returns the line of sight distance between two zipcodes in Kilometers
func (zc *Zipcodes) DistanceInKm(zipCodeA string, zipCodeB string) (float64, error) {
return zc.CalculateDistance(zipCodeA, zipCodeB, earthRadiusKm)
}
// DistanceInMiles returns the line of sight distance between two zipcodes in Miles
func (zc *Zipcodes) DistanceInMiles(zipCodeA string, zipCodeB string) (float64, error) {
return zc.CalculateDistance(zipCodeA, zipCodeB, earthRadiusMi)
}
// CalculateDistance returns the line of sight distance between two zipcodes in Kilometers
func (zc *Zipcodes) CalculateDistance(zipCodeA string, zipCodeB string, radius float64) (float64, error) {
locationA, errLocA := zc.Lookup(zipCodeA)
if errLocA != nil {
return 0, errLocA
}
locationB, errLocB := zc.Lookup(zipCodeB)
if errLocB != nil {
return 0, errLocB
}
return DistanceBetweenPoints(locationA.Lat, locationA.Lon, locationB.Lat, locationB.Lon, radius), nil
}
// DistanceInKmToZipcode calculates the distance between a zipcode and a give lat/lon in Kilometers
func (zc *Zipcodes) DistanceInKmToZipCode(zipCode string, latitude, longitude float64) (float64, error) {
location, errLoc := zc.Lookup(zipCode)
if errLoc != nil {
return 0, errLoc
}
return DistanceBetweenPoints(location.Lat, location.Lon, latitude, longitude, earthRadiusKm), nil
}
// DistanceInMilToZipcode calculates the distance between a zipcode and a give lat/lon in Miles
func (zc *Zipcodes) DistanceInMilToZipCode(zipCode string, latitude, longitude float64) (float64, error) {
location, errLoc := zc.Lookup(zipCode)
if errLoc != nil {
return 0, errLoc
}
return DistanceBetweenPoints(location.Lat, location.Lon, latitude, longitude, earthRadiusMi), nil
}
// GetZipcodesWithinKmRadius get all zipcodes within the radius of this zipcode
func (zc *Zipcodes) GetZipcodesWithinKmRadius(zipCode string, radius float64) ([]string, error) {
zipcodeList := []string{}
location, errLoc := zc.Lookup(zipCode)
if errLoc != nil {
return zipcodeList, errLoc
}
return zc.FindZipcodesWithinRadius(location, radius, earthRadiusKm), nil
}
// GetZipcodesWithinMlRadius get all zipcodes within the radius of this zipcode
func (zc *Zipcodes) GetZipcodesWithinMlRadius(zipCode string, radius float64) ([]string, error) {
zipcodeList := []string{}
location, errLoc := zc.Lookup(zipCode)
if errLoc != nil {
return zipcodeList, errLoc
}
return zc.FindZipcodesWithinRadius(location, radius, earthRadiusMi), nil
}
// FindZipcodesWithinRadius finds zipcodes within a given radius
func (zc *Zipcodes) FindZipcodesWithinRadius(location *ZipCodeLocation, maxRadius float64, earthRadius float64) []string {
zipcodeList := []string{}
for _, elm := range zc.DatasetList {
if elm.ZipCode != location.ZipCode {
distance := DistanceBetweenPoints(location.Lat, location.Lon, elm.Lat, elm.Lon, earthRadius)
if distance < maxRadius {
zipcodeList = append(zipcodeList, elm.ZipCode)
}
}
}
return zipcodeList
}
func hsin(t float64) float64 {
return math.Pow(math.Sin(t/2), 2)
}
// degreesToRadians converts degrees to radians
func degreesToRadians(d float64) float64 {
return d * math.Pi / 180
}
// DistanceBetweenPoints returns the distance between two lat/lon
// points using the Haversin distance formula.
func DistanceBetweenPoints(latitude1, longitude1, latitude2, longitude2 float64, radius float64) float64 {
lat1 := degreesToRadians(latitude1)
lon1 := degreesToRadians(longitude1)
lat2 := degreesToRadians(latitude2)
lon2 := degreesToRadians(longitude2)
diffLat := lat2 - lat1
diffLon := lon2 - lon1
a := hsin(diffLat) + math.Cos(lat1)*math.Cos(lat2)*hsin(diffLon)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := c * radius
return math.Round(distance*100) / 100
}
// LoadDataset reads and loads the dataset into a map interface
func LoadDataset(datasetPath string) (Zipcodes, error) {
file, err := os.Open(datasetPath)
if err != nil {
log.Fatal(err)
return Zipcodes{}, fmt.Errorf("zipcodes: error while opening file %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
zipcodeMap := Zipcodes{DatasetList: make(map[string]ZipCodeLocation)}
for scanner.Scan() {
splittedLine := strings.Split(scanner.Text(), "\t")
if len(splittedLine) != 12 {
return Zipcodes{}, fmt.Errorf("zipcodes: file line does not have 12 fields")
}
lat, errLat := strconv.ParseFloat(splittedLine[9], 64)
if errLat != nil {
return Zipcodes{}, fmt.Errorf("zipcodes: error while converting %s to Latitude", splittedLine[9])
}
lon, errLon := strconv.ParseFloat(splittedLine[10], 64)
if errLon != nil {
return Zipcodes{}, fmt.Errorf("zipcodes: error while converting %s to Longitude", splittedLine[10])
}
zipcodeMap.DatasetList[splittedLine[1]] = ZipCodeLocation{
ZipCode: splittedLine[1],
PlaceName: splittedLine[2],
AdminName: splittedLine[3],
State: splittedLine[4],
Lat: lat,
Lon: lon,
}
}
if err := scanner.Err(); err != nil {
return Zipcodes{}, fmt.Errorf("zipcodes: error while opening file %v", err)
}
return zipcodeMap, nil
}