-
Notifications
You must be signed in to change notification settings - Fork 8
/
geocode.go
176 lines (146 loc) · 4.46 KB
/
geocode.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
package geocodio
import (
"errors"
"strings"
)
// BatchResponse
type BatchResponse struct {
Results []BatchResult `json:"results"`
Debug struct {
RawResponse []byte `json:"-"`
RequestedURL string `json:"requested_url"`
Status string `json:"status"`
StatusCode int `json:"status_code"`
} `json:"-"`
}
// BatchResult
type BatchResult struct {
Query string `json:"query"`
Response BatchResultItem `json:"response"`
}
type BatchResultItem struct {
Input Input `json:"input,omitempty"`
Results []Address `json:"results"`
Error string `json:"error,omitempty"`
}
// GeocodeResponse
type GeocodeResult struct {
Input Input `json:"input,omitempty"`
Results []Result `json:"results"`
Debug struct {
RawResponse []byte `json:"-"`
RequestedURL string `json:"requested_url"`
Status string `json:"status"`
StatusCode int `json:"status_code"`
} `json:"-"`
}
type ErrorResponse struct {
Message string `json:"error"`
Results []Address `json:"results"`
}
type Result struct {
Address
Error *ErrorResponse `json:"response,omitempty"`
}
func (self *GeocodeResult) SaveDebug(requestedURL, status string, statusCode int, body []byte) {
self.Debug.RequestedURL = requestedURL
self.Debug.Status = status
self.Debug.StatusCode = statusCode
self.Debug.RawResponse = body
}
func (self *GeocodeResult) Error() string {
if len(self.Results) > 0 {
if self.Results[0].Error != nil {
return self.Results[0].Error.Message
}
}
return ""
}
// ResponseAsString helper to return raw response
func (self *GeocodeResult) ResponseAsString() string {
return string(self.Debug.RawResponse)
}
func (self *BatchResponse) SaveDebug(requestedURL, status string, statusCode int, body []byte) {
self.Debug.RequestedURL = requestedURL
self.Debug.Status = status
self.Debug.StatusCode = statusCode
self.Debug.RawResponse = body
}
// ResponseAsString helper to return raw response
func (self *BatchResponse) ResponseAsString() string {
return string(self.Debug.RawResponse)
}
// Geocode single address
// See: http://geocod.io/docs/#toc_4
func (g *Geocodio) Geocode(address string) (GeocodeResult, error) {
resp := GeocodeResult{}
if address == "" {
return resp, ErrAddressIsEmpty
}
err := g.get("/geocode", map[string]string{"q": address}, &resp)
if err != nil {
return GeocodeResult{}, err
}
if len(resp.Results) == 0 {
return resp, ErrNoResultsFound
}
return resp, nil
}
// GeocodeBatch look up addresses
func (g *Geocodio) GeocodeBatch(addresses ...string) (BatchResponse, error) {
resp := BatchResponse{}
if len(addresses) == 0 {
return resp, ErrBatchAddressesIsEmpty
}
// TODO: support limit
err := g.post("/geocode", addresses, nil, &resp)
if err != nil {
return BatchResponse{}, err
}
if len(resp.Results) == 0 {
return resp, ErrNoResultsFound
}
return resp, nil
}
// GeocodeAndReturnTimezone will geocode and include Timezone in the fields response
func (g *Geocodio) GeocodeAndReturnTimezone(address string) (GeocodeResult, error) {
return g.GeocodeReturnFields(address, "timezone")
}
// GeocodeAndReturnZip4 will geocode and include zip4 in the fields response
func (g *Geocodio) GeocodeAndReturnZip4(address string) (GeocodeResult, error) {
return g.GeocodeReturnFields(address, "zip4")
}
// GeocodeAndReturnCongressionalDistrict will geocode and include Congressional District in the fields response
func (g *Geocodio) GeocodeAndReturnCongressionalDistrict(address string) (GeocodeResult, error) {
return g.GeocodeReturnFields(address, "cd")
}
// GeocodeAndReturnStateLegislativeDistricts will geocode and include State Legislative Districts in the fields response
func (g *Geocodio) GeocodeAndReturnStateLegislativeDistricts(address string) (GeocodeResult, error) {
return g.GeocodeReturnFields(address, "stateleg")
}
// TODO: School District (school)
// GeocodeReturnFields will geocode and includes additional fields in response
/*
See: http://geocod.io/docs/#toc_22
Note:
Each field counts as an additional lookup each
*/
func (g *Geocodio) GeocodeReturnFields(address string, fields ...string) (GeocodeResult, error) {
resp := GeocodeResult{}
if address == "" {
return resp, errors.New("address can not be empty")
}
fieldsCommaSeparated := strings.Join(fields, ",")
err := g.get("/geocode",
map[string]string{
"q": address,
"fields": fieldsCommaSeparated,
}, &resp)
if err != nil {
return resp, err
}
if len(resp.Results) == 0 {
return resp, ErrNoResultsFound
}
return resp, nil
}