forked from OSU-App-Club/corvallis-bus-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstops.go
253 lines (206 loc) · 5.54 KB
/
stops.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
251
252
253
package corvallisbus
import (
"appengine"
"appengine/datastore"
"appengine/memcache"
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"sync"
"github.com/kellydunn/golang-geo"
)
//
// Global
//
var stopLocations map[int64]*geo.Point // StopID to location point
/*
/stops (endpoint to access stop information)
Default: returns all stops
Paramaters:
ids: comma delimited list of stop ids (optional); Default: ""
lat: latitude for search; Default: ""
lng: longitude for search; Default: ""
radius: radius in meters to make search; Default: 500
limit: limit the amount of stops returned; Default: none
Response:
stops: array of stops objects
-- Sort order different based on paramaters
-- location: sorted by distance
-- ids: sorted by ids
*/
func Stops(c appengine.Context, w http.ResponseWriter, r *http.Request) {
// Determing if need to search based on location or use stopNumbers
var stops []*Stop
var err error
if r.FormValue("ids") != "" {
// Split on commas
sepIDs := strings.Split(r.FormValue("ids"), ",")
// Make keys
keys := make([]*datastore.Key, len(sepIDs))
actualCount := 0
for _, id := range sepIDs {
idVal, idErr := strconv.ParseInt(id, 10, 64)
if idErr == nil {
keys[actualCount] = datastore.NewKey(c, "Stop", "", idVal, nil)
actualCount++
}
}
keys = keys[:actualCount]
if len(keys) > 0 {
// Do a search by IDs
stops = make([]*Stop, len(keys))
for i, _ := range stops {
stops[i] = new(Stop)
}
datastore.GetMulti(c, keys, stops)
// Populate IDs
for i, stop := range stops {
stop.ID = keys[i].IntID()
}
}
} else if r.FormValue("lat") != "" && r.FormValue("lng") != "" {
// Do a radius search
lat, latErr := strconv.ParseFloat(r.FormValue("lat"), 64)
lng, lngErr := strconv.ParseFloat(r.FormValue("lng"), 64)
if latErr == nil && lngErr == nil {
// Determine radius to use
radius := 500
if r.FormValue("radius") != "" {
val, radErr := strconv.Atoi(r.FormValue("radius"))
if radErr == nil {
radius = val
}
}
stops, err = stopsInRadius(c, lat, lng, radius)
} else {
err = errors.New("Error in parsing Latitude and Longitude")
}
} else {
// Return all stops -- will take a while
var keys []*datastore.Key
keys, err = datastore.NewQuery("Stop").Order("__key__").GetAll(c, &stops)
// Populate IDs
for i, stop := range stops {
stop.ID = keys[i].IntID()
}
}
if err != nil {
http.Error(w, "Get Stops Error: "+err.Error(), 500)
return
}
// Limit response size
limit := len(stops)
if r.FormValue("limit") != "" {
val, limErr := strconv.Atoi(r.FormValue("limit"))
if limErr == nil && val < limit {
limit = val
} else if limErr != nil {
http.Error(w, "Get Stops Error: "+limErr.Error(), 500)
return
}
}
// Prevent nil array
if stops == nil {
stops = make([]*Stop, 1)
}
result := map[string]interface{}{
"stops": stops[:limit],
}
//Print out the pattern information
data, errJSON := json.Marshal(result)
if errJSON != nil {
http.Error(w, errJSON.Error(), 500)
return
}
// Output JSON
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(data))
}
func stopsInRadius(c appengine.Context, lat, lng float64, radiusMeters int) ([]*Stop, error) {
// Check if we need to populate memory
if stopLocations == nil {
// Get all stops -- only coordinates
stops := []Stop{}
keys, err := datastore.NewQuery("Stop").GetAll(c, &stops)
if err != nil {
c.Debugf("Stop QUERY ERROR", stops)
return nil, err
}
stopLocations = make(map[int64]*geo.Point)
for i, stop := range stops {
stopLocations[keys[i].IntID()] = geo.NewPoint(stop.Lat, stop.Long)
}
}
// Determine geohash for given position
currentLoc := geo.NewPoint(lat, lng)
// Create array of stopIDS
desiredStops := make([]*datastore.Key, len(stopLocations))
distances := make([]float64, len(stopLocations))
// Filter by distance
matchingCount := 0
for key, loc := range stopLocations {
dist := currentLoc.GreatCircleDistance(loc) * 1000.0 // In meters
if int(dist) <= radiusMeters {
desiredStops[matchingCount] = datastore.NewKey(c, "Stop", "", key, nil)
distances[matchingCount] = dist
matchingCount++
}
}
// Sort by distance closest to farthest
var stops []*Stop
if matchingCount > 0 {
var err error
stops, err = getStopsWithKeys(c, desiredStops[:matchingCount])
if err != nil {
return nil, err
}
}
// Populate with key and distance
for i, stop := range stops {
stop.ID = desiredStops[i].IntID()
stop.Distance = distances[i]
}
// Sort by distance
sort.Sort(ByDistance{stops})
// Return stops
return stops, nil
}
func getStopsWithKeys(c appengine.Context, keys []*datastore.Key) ([]*Stop, error) {
// Get these stops
stops := make([]*Stop, len(keys))
var wg sync.WaitGroup
for i, key := range keys {
wg.Add(1)
go func(idx int, k *datastore.Key) {
defer wg.Done()
// Determine Cache Name
cacheName := "Stop:" + strconv.FormatInt(k.IntID(), 10)
// Check memcache
stop := new(Stop)
if _, memError := memcache.Gob.Get(c, cacheName, stop); memError == memcache.ErrCacheMiss {
err := datastore.Get(c, k, stop)
if err != nil {
c.Debugf("Get Stops Error: ", k)
}
// Add to memcache
item := &memcache.Item{
Key: cacheName,
Object: stop,
}
wg.Add(1)
go func() {
defer wg.Done()
memcache.Gob.Set(c, item) // We do enough work below that this will finish
}()
}
// Add to list
stops[idx] = stop
}(i, key)
}
wg.Wait()
return stops, nil
}