-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
250 lines (209 loc) · 8.48 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
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
package redimo
import (
"context"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
"github.com/mmcloughlin/geohash"
)
const earthRadiusMeters = 6372797.560856
type GLocation struct {
Lat float64
Lon float64
}
func (l GLocation) s2CellID() string {
return fmt.Sprintf("%d", s2.CellIDFromLatLng(l.s2LatLng()))
}
func (l GLocation) Geohash() string {
return geohash.Encode(l.Lat, l.Lon)
}
func (l GLocation) toAV() types.AttributeValue {
return &types.AttributeValueMemberN{Value: l.s2CellID()}
}
func (l *GLocation) setCellIDString(cellIDStr string) {
cellID, _ := strconv.ParseUint(cellIDStr, 10, 64)
s2Cell := s2.CellID(cellID)
s2LatLon := s2Cell.LatLng()
l.Lat = s2LatLon.Lat.Degrees()
l.Lon = s2LatLon.Lng.Degrees()
}
func (l GLocation) DistanceTo(other GLocation, unit GUnit) (distance float64) {
return Meters.To(unit, l.s2LatLng().Distance(other.s2LatLng()).Radians()*earthRadiusMeters)
}
func (l GLocation) s2LatLng() s2.LatLng {
return s2.LatLngFromDegrees(l.Lat, l.Lon)
}
func fromCellIDString(cellID string) (l GLocation) {
l.setCellIDString(cellID)
return
}
type GUnit float64
func (from GUnit) To(to GUnit, d float64) float64 {
return (d / float64(to)) * float64(from)
}
const (
Meters GUnit = 1.0
Kilometers GUnit = 1000.0
Miles GUnit = 1609.34
Feet GUnit = 0.3048
)
// GEOADD adds the given members into the key. Members are represented by a map of name to GLocation, which is just a wrapper
// for latitude and longitude. If a member already exists, its location will be updated. The method only returns the members
// that were added as part of the operation and did not already exist.
//
// Cost is O(1) / 1 WCU for each member being added or updated.
//
// Works similar to https://redis.io/commands/geoadd
func (c Client) GEOADD(key string, members map[string]GLocation) (newlyAddedMembers map[string]GLocation, err error) {
newlyAddedMembers = make(map[string]GLocation)
for member, location := range members {
builder := newExpresionBuilder()
builder.updateSetAV(c.sortKeyNum, location.toAV())
resp, err := c.ddbClient.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
ConditionExpression: builder.conditionExpression(),
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
Key: keyDef{pk: key, sk: member}.toAV(c),
ReturnValues: types.ReturnValueAllOld,
TableName: aws.String(c.tableName),
UpdateExpression: builder.updateExpression(),
})
if err != nil {
return newlyAddedMembers, err
}
if len(resp.Attributes) < 1 {
newlyAddedMembers[member] = location
}
}
return newlyAddedMembers, nil
}
// GEODIST returns the scalar distance between the two members, converted to the given unit. If either of
// the members or the key is missing, ok will be false. Each GUnit also has convenience methods to convert
// distances into other units.
//
// Cost is O(1) / 1 RCU for each of the two members.
//
// Works similar to https://redis.io/commands/geodist
func (c Client) GEODIST(key string, member1, member2 string, unit GUnit) (distance float64, ok bool, err error) {
locations, err := c.GEOPOS(key, member1, member2)
if err != nil || len(locations) < 2 {
return
}
return locations[member1].DistanceTo(locations[member2], unit), true, nil
}
// GEOHASH returns the Geohash strings (see https://en.wikipedia.org/wiki/Geohash) of the given members. If any members
// were not found, they will not be present in the returned map.
//
// Cost is O(1) / 1 RCU for each member.
//
// Works similar to https://redis.io/commands/geohash
func (c Client) GEOHASH(key string, members ...string) (geohashes map[string]string, err error) {
geohashes = make(map[string]string)
locations, err := c.GEOPOS(key, members...)
for _, member := range members {
if location, found := locations[member]; found {
geohashes[member] = location.Geohash()
}
}
return
}
// GEOPOS returns the stored locations for each of the given members, as a map of member to location.
// If a member cannot be found, it will not be present in the returned map.
//
// Cost is O(1) / 1 RCU for each member.
//
// Works similar to https://redis.io/commands/geopos
func (c Client) GEOPOS(key string, members ...string) (locations map[string]GLocation, err error) {
locations = make(map[string]GLocation)
for _, member := range members {
resp, err := c.ddbClient.GetItem(context.TODO(), &dynamodb.GetItemInput{
ConsistentRead: aws.Bool(c.consistentReads),
Key: keyDef{pk: key, sk: member}.toAV(c),
TableName: aws.String(c.tableName),
})
if err != nil {
return locations, err
}
if len(resp.Item) > 0 {
locations[member] = fromCellIDString(resp.Item[c.sortKeyNum].(*types.AttributeValueMemberN).Value)
}
}
return
}
// GEORADIUS returns the members (limited to the given count) that are located within the given radius
// of the given center. Note that the positions are returned in no particular order – if there are more
// members inside the given radius than the given count, the method *does not* guarantee that the returned
// locations are the closest.
//
// The GLocation type has convenience methods to calculate the distance between points, this can be used
// to sort the locations as required.
//
// Cost is O(N) where N is the number of locations inside the square / bounding box that contains the circle
// we're searching inside.
//
// Works similar to https://redis.io/commands/georadius
func (c Client) GEORADIUS(key string, center GLocation, radius float64, radiusUnit GUnit, count int32) (positions map[string]GLocation, err error) {
positions = make(map[string]GLocation)
radiusCap := s2.CapFromCenterAngle(s2.PointFromLatLng(center.s2LatLng()), s1.Angle(radiusUnit.To(Meters, radius)/earthRadiusMeters))
for _, cellID := range radiusCap.CellUnionBound() {
builder := newExpresionBuilder()
builder.addConditionEquality(c.partitionKey, StringValue{key})
builder.condition(fmt.Sprintf("#%v BETWEEN :start AND :stop", c.sortKeyNum), c.sortKeyNum)
builder.values["start"] = &types.AttributeValueMemberN{Value: fmt.Sprintf("%d", cellID.RangeMin())}
builder.values["stop"] = &types.AttributeValueMemberN{Value: fmt.Sprintf("%d", cellID.RangeMax())}
var cursor map[string]types.AttributeValue
hasMoreResults := true
for hasMoreResults && count > 0 {
resp, err := c.ddbClient.Query(context.TODO(), &dynamodb.QueryInput{
ConsistentRead: aws.Bool(c.consistentReads),
ExclusiveStartKey: cursor,
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
IndexName: aws.String(c.indexName),
KeyConditionExpression: builder.conditionExpression(),
Limit: aws.Int32(count),
TableName: aws.String(c.tableName),
})
if err != nil {
return positions, err
}
if len(resp.LastEvaluatedKey) > 0 {
cursor = resp.LastEvaluatedKey
} else {
hasMoreResults = false
}
for _, item := range resp.Items {
location := fromCellIDString(item[c.sortKeyNum].(*types.AttributeValueMemberN).Value)
member := item[c.sortKey].(*types.AttributeValueMemberS).Value
if center.DistanceTo(location, radiusUnit) <= radius {
positions[member] = location
count--
}
}
}
}
return
}
// GEORADIUSBYMEMBER returns the members (limited to the given count) that are located within the given radius
// of the given member. Note that the positions are returned in no particular order – if there are more
// members inside the given radius than the given count, the method *does not* guarantee that the returned
// locations are the closest.
//
// The GLocation type has convenience methods to calculate the distance between points, this can be used
// to sort the locations as required.
//
// Cost is O(N) where N is the number of locations inside the square / bounding box that contains the circle
// we're searching inside.
//
// Works similar to https://redis.io/commands/georadiusbymember
func (c Client) GEORADIUSBYMEMBER(key string, member string, radius float64, radiusUnit GUnit, count int32) (positions map[string]GLocation, err error) {
locations, err := c.GEOPOS(key, member)
if err == nil {
positions, err = c.GEORADIUS(key, locations[member], radius, radiusUnit, count)
}
return
}