-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeocoding.go
310 lines (253 loc) · 8.56 KB
/
geocoding.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package mapbox
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
const (
GeocodingBatchEndpoint = "/search/geocode/v6/batch"
GeocodingReverseEndpoint = "/search/geocode/v6/reverse"
GeocodingForwardEndpoint = "/search/geocode/v6/forward"
)
//////////////////////////////////////////////////////////////////
type ReverseGeocodeRequest struct {
Coordinate
// optional
Country string `json:"country,omitempty"`
Language string `json:"language,omitempty"`
Limit int `json:"limit,omitempty"`
Types Types `json:"types,omitempty"`
}
type ForwardGeocodeBatchRequest []ForwardGeocodeRequest
type ForwardGeocodeBatchResponse struct {
Batch []GeocodeResponse `json:"batch"`
}
type ReverseGeocodeBatchRequest []ReverseGeocodeRequest
type ForwardGeocodeRequest struct {
SearchText string
AddressLine1 string
Postcode string
Place string
Autocomplete bool
BBox BoundingBox
Country string
Language string
Limit int
Proximity Coordinate
Types Types
}
func (r ForwardGeocodeRequest) MarshalJSON() ([]byte, error) {
type forwardGeocodeRequest struct {
SearchText string `json:"q,omitempty"`
AddressLine1 string `json:"address_line1,omitempty"`
Postcode string `json:"postcode,omitempty"`
Place string `json:"place,omitempty"`
Autocomplete bool `json:"autocomplete,omitempty"`
BBox *BoundingBox `json:"bbox,omitempty"`
Country string `json:"country,omitempty"`
Language string `json:"language,omitempty"`
Limit int `json:"limit,omitempty"`
Proximity *Coordinate `json:"proximity,omitempty"`
Types []string `json:"types,omitempty"`
}
var resp = forwardGeocodeRequest{
SearchText: r.SearchText,
AddressLine1: r.AddressLine1,
Postcode: r.Postcode,
Place: r.Place,
Autocomplete: r.Autocomplete,
Country: r.Country,
Language: r.Language,
Limit: r.Limit,
}
if !r.BBox.Min.IsZero() && !r.BBox.Max.IsZero() {
resp.BBox = &r.BBox
}
types := r.Types.strings()
if len(types) > 0 {
resp.Types = types
}
return json.Marshal(resp)
}
type GeocodeResponse struct {
Type string `json:"type"`
Features []*Feature `json:"features"`
Attribution string `json:"attribution"`
}
type GeocodeBatchResponse struct {
Batch []GeocodeResponse `json:"batch"`
}
//////////////////////////////////////////////////////////////////
type Feature struct {
ID string `json:"id"`
Type string `json:"type"`
Geometry *Geometry `json:"geometry"` // The center of Properties.BoundingBox
Properties *Properties `json:"properties,omitempty"`
}
type Properties struct {
MapboxID string `json:"mapbox_id"`
FeatureType Type `json:"feature_type"`
Name string `json:"name"`
NamePreferred string `json:"name_preferred"`
PlaceFormatted string `json:"place_formatted"`
FullAddress string `json:"full_address"`
Coordinates ExtendedCoordinate `json:"coordinates"`
Context map[Type]Context `json:"context,omitempty"`
BoundingBox []float64 `json:"bbox,omitempty"`
MatchCode *MatchCode `json:"match_code,omitempty"`
}
// There are many different types of context objects, which are all mashed together
// here.
// https://docs.mapbox.com/api/search/geocoding/#the-context-object
type Context struct {
// Always present
MapboxID string `json:"mapbox_id"`
Name string `json:"name"`
// Optional but shared between many context types
WikidataID string `json:"wikidata_id,omitempty"`
// Region fields
RegionCode string `json:"region_code,omitempty"`
RegionCodeFull string `json:"region_code_full,omitempty"`
// Address fields
AddressNumber string `json:"address_number,omitempty"`
StreetName string `json:"street_name,omitempty"`
// Country fields
CountryCode string `json:"country_code,omitempty"`
CountryCodeAlpha3 string `json:"country_code_alpha_3,omitempty"`
}
type MatchCode struct {
AddressNumber MatchCodeValue `json:"address_number"`
Street MatchCodeValue `json:"street"`
Postcode MatchCodeValue `json:"postcode"`
Place MatchCodeValue `json:"place"`
Region MatchCodeValue `json:"region"`
Locality MatchCodeValue `json:"locality"`
Country MatchCodeValue `json:"country"`
Confidence MatchCodeConfidence `json:"confidence"`
}
type MatchCodeConfidence string
type MatchCodeValue string
const (
MatchCodeConfidenceExact MatchCodeConfidence = "exact"
MatchCodeConfidenceHigh MatchCodeConfidence = "high"
MatchCodeConfidenceMedium MatchCodeConfidence = "medium"
MatchCodeConfidenceLow MatchCodeConfidence = "low"
MatchCodeValueMatched MatchCodeValue = "matched"
MatchCodeValueUnmatched MatchCodeValue = "unmatched"
MatchCodeValueNotApplicable MatchCodeValue = "not_applicable"
MatchCodeValueInferred MatchCodeValue = "inferred"
MatchCodeValuePlausible MatchCodeValue = "plausible"
)
//////////////////////////////////////////////////////////////////
// https://docs.mapbox.com/api/search/geocoding/#forward-geocoding-with-search-text-input
func forwardGeocode(ctx context.Context, client *Client, req *ForwardGeocodeRequest) (*GeocodeResponse, error) {
query := url.Values{}
query.Set("access_token", client.apiKey)
query.Set("autocomplete", strconv.FormatBool(req.Autocomplete))
if req.SearchText != "" {
query.Set("q", req.SearchText)
}
if req.AddressLine1 != "" {
query.Set("address_line1", req.AddressLine1)
}
if req.Postcode != "" {
query.Set("postcode", req.Postcode)
}
if req.Place != "" {
query.Set("place", req.Place)
}
if !req.BBox.Min.IsZero() {
query.Set("bbox", req.BBox.query())
}
if req.Country != "" {
query.Set("country", req.Country)
}
if req.Language != "" {
query.Set("language", req.Language)
}
if req.Limit != 0 {
query.Set("limit", strconv.Itoa(req.Limit))
}
if !req.Proximity.IsZero() {
query.Set("proximity", req.Proximity.WGS84Format())
}
if len(req.Types) != 0 {
query.Set("types", req.Types.query())
}
apiResponse, err := client.get(ctx, GeocodingForwardEndpoint, query)
if err != nil {
return nil, err
}
var response GeocodeResponse
if err := client.handleResponse(apiResponse, &response, GeocodingRateLimit); err != nil {
return nil, err
}
return &response, nil
}
// https://docs.mapbox.com/api/search/geocoding/#batch-geocoding
func forwardGeocodeBatch(ctx context.Context, client *Client, req ForwardGeocodeBatchRequest) (*GeocodeBatchResponse, error) {
query := url.Values{}
query.Set("access_token", client.apiKey)
b, err := json.Marshal(req)
if err != nil {
return nil, err
}
apiResponse, err := client.post(ctx, GeocodingBatchEndpoint, query, bytes.NewReader(b))
if err != nil {
return nil, err
}
var response *GeocodeBatchResponse
if err := client.handleResponse(apiResponse, &response, GeocodingRateLimit); err != nil {
return nil, err
}
return response, nil
}
// https://docs.mapbox.com/api/search/geocoding/#reverse-geocoding
func reverseGeocode(ctx context.Context, client *Client, req *ReverseGeocodeRequest) (*GeocodeResponse, error) {
query := url.Values{}
query.Set("access_token", client.apiKey)
query.Set("latitude", strconv.FormatFloat(req.Lat, 'f', -1, 64))
query.Set("longitude", strconv.FormatFloat(req.Lng, 'f', -1, 64))
if req.Country != "" {
query.Set("country", req.Country)
}
if req.Language != "" {
query.Set("language", req.Language)
}
if req.Limit > 0 {
query.Set("limit", fmt.Sprintf("%v", req.Limit))
}
if len(req.Types) > 0 {
query.Set("types", req.Types.query())
}
apiResponse, err := client.get(ctx, GeocodingReverseEndpoint, query)
if err != nil {
return nil, err
}
var response GeocodeResponse
if err := client.handleResponse(apiResponse, &response, GeocodingRateLimit); err != nil {
return nil, err
}
return &response, nil
}
// https://docs.mapbox.com/api/search/geocoding/#batch-geocoding, but only supports reverse
func reverseGeocodeBatch(ctx context.Context, client *Client, req ReverseGeocodeBatchRequest) (*GeocodeBatchResponse, error) {
query := url.Values{}
query.Set("access_token", client.apiKey)
b, err := json.Marshal(req)
if err != nil {
return nil, err
}
apiResponse, err := client.post(ctx, GeocodingBatchEndpoint, query, bytes.NewReader(b))
if err != nil {
return nil, err
}
var response *GeocodeBatchResponse
if err := client.handleResponse(apiResponse, &response, GeocodingRateLimit); err != nil {
return nil, err
}
return response, nil
}