-
Notifications
You must be signed in to change notification settings - Fork 5
/
geo.go
202 lines (161 loc) · 5.71 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
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/golang/geo/s1"
"github.com/golang/geo/s2"
"github.com/mmcloughlin/geohash"
)
const earthRadiusMeters = 6372797.560856
const skGeoCell = skN
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() dynamodb.AttributeValue {
return dynamodb.AttributeValue{
N: aws.String(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
)
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(skGeoCell, location.toAV())
resp, err := c.ddbClient.UpdateItemRequest(&dynamodb.UpdateItemInput{
ConditionExpression: builder.conditionExpression(),
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
Key: keyDef{pk: key, sk: member}.toAV(),
ReturnValues: dynamodb.ReturnValueAllOld,
TableName: aws.String(c.table),
UpdateExpression: builder.updateExpression(),
}).Send(context.TODO())
if err != nil {
return newlyAddedMembers, err
}
if len(resp.Attributes) < 1 {
newlyAddedMembers[member] = location
}
}
return newlyAddedMembers, nil
}
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
}
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
}
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.GetItemRequest(&dynamodb.GetItemInput{
ConsistentRead: aws.Bool(c.consistentReads),
Key: keyDef{pk: key, sk: member}.toAV(),
TableName: aws.String(c.table),
}).Send(context.TODO())
if err != nil {
return locations, err
}
if len(resp.Item) > 0 {
locations[member] = fromCellIDString(aws.StringValue(resp.Item[skGeoCell].N))
}
}
return
}
func (c Client) GEORADIUS(key string, center GLocation, radius float64, radiusUnit GUnit, count int64) (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(pk, StringValue{key})
builder.condition(fmt.Sprintf("#%v BETWEEN :start AND :stop", skGeoCell), skGeoCell)
builder.values["start"] = dynamodb.AttributeValue{N: aws.String(fmt.Sprintf("%d", cellID.RangeMin()))}
builder.values["stop"] = dynamodb.AttributeValue{N: aws.String(fmt.Sprintf("%d", cellID.RangeMax()))}
var cursor map[string]dynamodb.AttributeValue
hasMoreResults := true
for hasMoreResults && count > 0 {
resp, err := c.ddbClient.QueryRequest(&dynamodb.QueryInput{
ConsistentRead: aws.Bool(c.consistentReads),
ExclusiveStartKey: cursor,
ExpressionAttributeNames: builder.expressionAttributeNames(),
ExpressionAttributeValues: builder.expressionAttributeValues(),
IndexName: c.getIndex(skGeoCell),
KeyConditionExpression: builder.conditionExpression(),
Limit: aws.Int64(count),
TableName: aws.String(c.table),
}).Send(context.TODO())
if err != nil {
return positions, err
}
if len(resp.LastEvaluatedKey) > 0 {
cursor = resp.LastEvaluatedKey
} else {
hasMoreResults = false
}
for _, item := range resp.Items {
location := fromCellIDString(aws.StringValue(item[skGeoCell].N))
member := aws.StringValue(item[sk].S)
if center.DistanceTo(location, radiusUnit) <= radius {
positions[member] = location
count--
}
}
}
}
return
}
func (c Client) GEORADIUSBYMEMBER(key string, member string, radius float64, radiusUnit GUnit, count int64) (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
}