-
Notifications
You must be signed in to change notification settings - Fork 5
/
postal_codes.go
80 lines (67 loc) · 1.83 KB
/
postal_codes.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
package geonames
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"github.com/remizovm/geonames/models"
)
const postalCodesURL = `http://download.geonames.org/export/zip/%s.zip`
// PostalCodes returns all postal codes for the selected countries iso2 code
func (c *Client) PostalCodes(iso2code string) (map[string]*models.PostalCode, error) {
var err error
result := make(map[string]*models.PostalCode)
if len(iso2code) != 2 {
return nil, errors.New("invalid iso2code")
}
url := fmt.Sprintf(postalCodesURL, strings.ToUpper(iso2code))
zipped, err := c.httpGet(url)
if err != nil {
return nil, err
}
f, err := unzip(zipped)
if err != nil {
return nil, err
}
data, err := getZipData(f, strings.ToUpper(iso2code)+".txt")
if err != nil {
return nil, err
}
parse(data, 0, func(raw [][]byte) bool {
if len(raw) != 12 {
return true
}
latitude, err := strconv.ParseFloat(string(raw[9]), 64)
if err != nil {
log.Printf("while parsing postal code latitude: %s", string(raw[9]))
return true
}
longitude, err := strconv.ParseFloat(string(raw[10]), 64)
if err != nil {
log.Printf("while parsing postal code longitude: %s", string(raw[10]))
return true
}
accuracy, err := strconv.Atoi(string(raw[11]))
if err != nil {
log.Printf("while parsing postal code accuracy: %s", string(raw[11]))
return true
}
result[string(raw[1])] = &models.PostalCode{
CountryIso2Code: string(raw[0]),
PostalCode: string(raw[1]),
PlaceName: string(raw[2]),
AdminName1: string(raw[3]),
AdminCode1: string(raw[4]),
AdminName2: string(raw[5]),
AdminCode2: string(raw[6]),
AdminName3: string(raw[7]),
AdminCode3: string(raw[8]),
Latitude: latitude,
Longitude: longitude,
Accuracy: accuracy,
}
return true
})
return result, nil
}