-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatial.go
185 lines (165 loc) · 3.55 KB
/
spatial.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
package main
import (
"fmt"
"log"
"math"
"sync"
"time"
"github.com/pmezard/rtreego"
)
type OfferLoc struct {
Id string
Date time.Time
Point Point
Loc rtreego.Rect
}
func (l *OfferLoc) Bounds() *rtreego.Rect {
return &l.Loc
}
var (
locExtent = [2]float64{1e-6, 1e-6}
)
func makeOfferLocation(id string, date time.Time, loc *Location) (
*OfferLoc, error) {
if loc == nil {
return nil, nil
}
lon := loc.Lon - locExtent[0]/2
lat := loc.Lat - locExtent[1]/2
rect, err := rtreego.NewRect(rtreego.Point{lon, lat}, locExtent)
if err != nil {
return nil, err
}
return &OfferLoc{
Id: id,
Date: date,
Loc: rect,
Point: Point{
Lon: loc.Lon,
Lat: loc.Lat,
},
}, nil
}
func getOfferLocation(store *Store, geocoder *Geocoder, id string) (*OfferLoc, error) {
loc, date, err := store.GetLocation(id)
if err != nil || loc == nil {
return nil, err
}
return makeOfferLocation(id, date, loc)
}
type SpatialIndex struct {
lock sync.RWMutex
rtree *rtreego.Rtree
known map[string]*OfferLoc
}
func NewSpatialIndex() *SpatialIndex {
return &SpatialIndex{
rtree: rtreego.NewTree(2, 25),
known: map[string]*OfferLoc{},
}
}
func (s *SpatialIndex) Add(o *OfferLoc) {
s.lock.Lock()
defer s.lock.Unlock()
prev := s.known[o.Id]
if prev != nil {
s.rtree.Delete(prev)
}
s.rtree.Insert(o)
s.known[o.Id] = o
}
func (s *SpatialIndex) Remove(id string) {
s.lock.Lock()
defer s.lock.Unlock()
o := s.known[id]
if o != nil {
s.rtree.Delete(o)
delete(s.known, id)
}
}
func (s *SpatialIndex) Get(id string) *OfferLoc {
s.lock.RLock()
defer s.lock.RUnlock()
return s.known[id]
}
func (s *SpatialIndex) List() []string {
s.lock.RLock()
defer s.lock.RUnlock()
ids := make([]string, 0, len(s.known))
for id := range s.known {
ids = append(ids, id)
}
return ids
}
func makeGeoRect(lat, lon, radius float64) (rtreego.Rect, error) {
earth := float64(6371000)
dlat := (radius / (math.Pi * earth)) * 180.0
r := earth * math.Cos((math.Pi*lat)/180.0)
dlon := (radius / (math.Pi * r)) * 180.
lon -= dlon
lat -= dlat
return rtreego.NewRect(rtreego.Point{lon, lat}, [2]float64{2 * dlon, 2 * dlat})
}
func (s *SpatialIndex) FindNearest(lat, lon, maxDist float64) ([]datedOffer, error) {
s.lock.RLock()
defer s.lock.RUnlock()
query, err := makeGeoRect(lat, lon, maxDist)
if err != nil {
return nil, err
}
offers := []datedOffer{}
results := s.rtree.SearchIntersect(&query)
for _, r := range results {
loc := r.(*OfferLoc)
offers = append(offers, datedOffer{
Date: loc.Date.Format(time.RFC3339),
Id: loc.Id,
})
}
return offers, nil
}
func (s *SpatialIndex) FindAll() []datedOffer {
s.lock.RLock()
defer s.lock.RUnlock()
offers := make([]datedOffer, 0, len(s.known))
for _, loc := range s.known {
offers = append(offers, datedOffer{
Date: loc.Date.Format(time.RFC3339),
Id: loc.Id,
})
}
return offers
}
var (
spatialCmd = app.Command("spatial", "create spatial index (for benchmarks)")
)
func spatialFn(cfg *Config) error {
store, err := OpenStore(cfg.Store())
if err != nil {
return err
}
defer store.Close()
geocoder, err := NewGeocoder(cfg.GeocodingKey(), cfg.Geocoder())
if err != nil {
return err
}
defer geocoder.Close()
ids, err := store.List()
if err != nil {
return err
}
spatial := NewSpatialIndex()
for i, id := range ids {
if (i+1)%500 == 0 {
log.Printf("%d spatially indexed", i+1)
}
loc, err := getOfferLocation(store, geocoder, id)
if err != nil {
return fmt.Errorf("could not get offer location for %s: %s", id, err)
}
if loc != nil {
spatial.Add(loc)
}
}
return nil
}