forked from pace/bricks
/
client.go
167 lines (144 loc) · 4.63 KB
/
client.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
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
// Created at 2018/10/12 by Vincent Landgraf
package nominatim
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
opentracing "github.com/opentracing/opentracing-go"
olog "github.com/opentracing/opentracing-go/log"
"lab.jamit.de/pace/go-microservice/maintenance/log"
"lab.jamit.de/pace/go-microservice/pkg/synctx"
)
// Client for nominatim using the configured endpoint
type Client struct {
// Endpoint url e.g. https://nominatim.example.org/
Endpoint string
}
// ErrUnableToGeocode lat/lon don't match to a known address
var ErrUnableToGeocode = errors.New("unable to geocode")
// ErrRequestFailed either the connection was lost or similar
var ErrRequestFailed = errors.New("HTTP request failed")
// SolidifiedReverse does two requests to the nominatim APIs one with zoom level 10 and one with
// zoom level 18 and combines the results from each of them. This will fix the
// 'city', 'town', 'village', 'hamlet', 'suburb' reported by OSM.
// See https://github.com/openstreetmap/Nominatim/issues/885 for details.
func (c *Client) SolidifiedReverse(ctx context.Context, lat, lon float64) (*Result, error) {
wq := synctx.NewWorkQueue(ctx)
var resultHigh, resultLow *Result
// ignore errors for the zoom level 10, if there are any errors
// for that zoom level the request will simply not be enriched
wq.Add("query nominatim zoom level 10", func(ctx context.Context) error {
resultHigh, _ = c.Reverse(ctx, lat, lon, 10) // nolint: errcheck,gosec
return nil
})
wq.Add("query nominatim zoom level 18", func(ctx context.Context) (err error) {
resultLow, err = c.Reverse(ctx, lat, lon, 18)
return
})
wq.Wait()
if err := wq.Err(); err != nil {
return nil, err
}
if resultHigh != nil && resultHigh.Address != nil && resultLow != nil && resultLow.Address != nil {
if resultLow.Address.City == "" {
resultLow.Address.City = resultHigh.Address.City
}
if resultLow.Address.Town == "" {
resultLow.Address.Town = resultHigh.Address.Town
}
if resultLow.Address.Village == "" {
resultLow.Address.Village = resultHigh.Address.Village
}
if resultLow.Address.Hamlet == "" {
resultLow.Address.Hamlet = resultHigh.Address.Hamlet
}
if resultLow.Address.Suburb == "" {
resultLow.Address.Suburb = resultHigh.Address.Suburb
}
if resultLow.Address.County == "" {
resultLow.Address.County = resultHigh.Address.County
}
if resultLow.Address.StateDistrict == "" {
resultLow.Address.StateDistrict = resultHigh.Address.StateDistrict
}
if resultLow.Address.State == "" {
resultLow.Address.State = resultHigh.Address.State
}
} else {
return nil, ErrUnableToGeocode
}
return resultLow, nil
}
// Reverse executes a request to the nominatim service and returns
// the genuine result
func (c *Client) Reverse(ctx context.Context, lat, lon float64, zoom int) (*Result, error) {
var result Result
var err error
// add logging & tracing statement
startTime := time.Now()
span, _ := opentracing.StartSpanFromContext(ctx, "Nominatim reverse",
opentracing.StartTime(startTime))
defer func() {
dur := float64(time.Since(startTime)) / float64(time.Millisecond)
le := log.Ctx(ctx).Debug().
Float64("lat", lat).
Float64("lon", lon).
Int("zoom", zoom).
Float64("duration", dur)
// add error or result set info
if err != nil {
le = le.Err(err)
span.LogFields(olog.Error(err))
} else {
span.LogFields(olog.String("address", result.DisplayName))
le = le.Str("address", result.DisplayName)
}
le.Msg("Nominatim reverse")
}()
span.LogFields(
olog.Float64("lat", lat),
olog.Float64("lon", lon),
olog.Int("zoom", zoom))
defer span.Finish()
// prepate request
u, err := url.Parse(c.Endpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse nominatim endpoint URL %q: %v", c.Endpoint, err)
}
u.Path = "nominatim/reverse"
values := make(url.Values)
values.Add("format", "jsonv2")
values.Add("lat", strconv.FormatFloat(lat, 'f', 10, 64))
values.Add("lon", strconv.FormatFloat(lon, 'f', 10, 64))
values.Add("zoom", strconv.Itoa(zoom))
u.RawQuery = values.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
// execute request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() // nolint: errcheck
if resp.StatusCode != http.StatusOK {
return nil, ErrRequestFailed
}
// parse response
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
// Handle geocoding error
if result.Error == "Unable to geocode" {
return nil, ErrUnableToGeocode
}
return &result, nil
}