-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo.go
215 lines (190 loc) · 4.73 KB
/
geo.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
package georedis
import (
"fmt"
"reflect"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
)
// Geo is the core service for geolocation-related operation
type Geo struct {
pool *redis.Pool
}
// NewGeo creates a Geo service
func NewGeo(pool *redis.Pool) *Geo {
return &Geo{
pool: pool,
}
}
// Add adds key and related meta data to redis
func (s *Geo) Add(key string, data []*Member) error {
conn := s.pool.Get()
defer conn.Close()
for _, d := range data {
r, err := conn.Do("GEOADD", key, d.Coord.Lon, d.Coord.Lat, d.Name)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"key": key,
"name": d.Name,
"latitude": d.Coord.Lat,
"longitude": d.Coord.Lon,
}).Error("add failed")
return err
}
log.WithFields(log.Fields{
"result": r,
"key": key,
"name": d.Name,
"latitude": d.Coord.Lat,
"longitude": d.Coord.Lon,
}).Info("add success")
}
return nil
}
// Pos gets the meta data by key
// returned meta data hase the same order of names
// leave nil for the keys have no data
func (s *Geo) Pos(key string, names ...string) ([]*Member, error) {
conn := s.pool.Get()
defer conn.Close()
// get data from redis
args := []interface{}{key}
for i := range names {
args = append(args, names[i])
}
r, err := redis.Positions(conn.Do("GEOPOS", args...))
if err != nil {
log.WithFields(log.Fields{
"error": err,
"key": key,
"names": names,
}).Error("get positions failed")
return nil, err
}
// create meta data
data := make([]*Member, len(r))
for i := range r {
if r[i] == nil {
log.WithFields(log.Fields{
"name": names[i],
}).Info("no position data")
} else {
data[i] = NewMember(names[i], r[i][latIdx], r[i][lonIdx])
log.WithFields(log.Fields{
"name": names[i],
"latitude": r[i][latIdx],
"longitude": r[i][lonIdx],
}).Info("get position data")
}
}
return data, nil
}
// RadiusByName find nearby members of member
// the result include the name itself
func (s *Geo) RadiusByName(key string, name string, radius int, unit string, options ...Option) ([]*Neighbor, error) {
mems, err := s.Pos(key, name)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"key": key,
"name": name,
}).Error("no position data of this point")
return nil, err
}
if len(mems) != 1 {
log.WithFields(log.Fields{
"key": key,
"name": name,
"members": mems,
}).Error("wrong data number")
return nil, fmt.Errorf("have multiple or zero results, key: %v, name: %v, members: %v", key, name, mems)
}
return s.Radius(key, mems[0].Coord, radius, unit, options...)
}
// Radius find the neighbor with coordinate
func (s *Geo) Radius(key string, coord Coordinate, radius int, unit string, options ...Option) ([]*Neighbor, error) {
conn := s.pool.Get()
defer conn.Close()
// basic command
args := []interface{}{key, coord.Lon, coord.Lat, radius, unit}
// set options
for _, opt := range options {
args = append(args, optMap[opt])
}
// execute command
r, err := conn.Do("GEORADIUS", args...)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"key": key,
"latitude": coord.Lat,
"longitude": coord.Lon,
}).Error("get radius fail")
return nil, err
}
return rawToNeighbors(r, options...)
}
// Dist cc
func (s *Geo) Dist(key, a, b string, u Unit) (float64, error) {
conn := s.pool.Get()
defer conn.Close()
r, err := conn.Do("GEODIST", key, a, b, u)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"key": key,
"place a": a,
"place b": b,
"unit": u,
}).Error("get dist fail")
return 0, err
}
v := reflect.ValueOf(r)
f, err := toFloat64(v)
if err != nil {
log.Errorf("convert value to float64 failed, value: %v, error: %v", v, err)
return 0, err
}
log.WithFields(log.Fields{
"key": key,
"place a": a,
"place b": b,
"distance": f,
"unit": u,
}).Info("get distance")
return f, nil
}
// Hash return the geohash of place
func (s *Geo) Hash(key string, list ...string) ([]string, error) {
conn := s.pool.Get()
defer conn.Close()
args := []interface{}{key}
for _, l := range list {
args = append(args, l)
}
r, err := conn.Do("GEOHASH", args...)
if err != nil {
log.WithFields(log.Fields{
"key": key,
"list": list,
}).Error("get hash failed")
return nil, err
}
v := reflect.ValueOf(r)
hashs := make([]string, len(list))
for i := 0; i < v.Len(); i++ {
hashv := unpackValue(v.Index(i))
hash, err := toString(hashv)
if err != nil {
log.Errorf("convert to string failed, value: %v, error: %v", v.Index(i), err)
return nil, err
}
hashs[i] = hash
}
log.WithFields(log.Fields{
"key": key,
"places": list,
"hashs": hashs,
}).Info("get geohash")
return hashs, nil
}