/
stations.go
169 lines (156 loc) · 4.4 KB
/
stations.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
/*
* Copyright 2017 Google Inc. All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package data
import (
"encoding/json"
"path/filepath"
"fmt"
"io/ioutil"
"github.com/golang/glog"
"log"
"math"
"net/http"
"strconv"
"strings"
rtree "github.com/dhconnelly/rtreego"
geojson "github.com/paulmach/go.geojson"
)
// GeoJSON is a cache of the NYC Subway Station and Line data.
var GeoJSON = make(map[string][]byte)
// cacheGeoJSON loads files under data into `GeoJSON`.
func CacheGeoJSON() {
filenames, err := filepath.Glob("static/data/*")
if err != nil {
// Note: this will take down the GAE instance by exiting this process.
glog.Fatalf("%v \n", err)
}
for _, f := range filenames {
name := filepath.Base(f)
dat, err := ioutil.ReadFile(f)
if err != nil {
glog.Fatalf("%v \n", err)
}
GeoJSON[name] = dat
}
}
// Stations is an RTree housing the stations
var Stations = rtree.NewTree(2, 25, 50)
// Station is a wrapper for `*geojson.Feature` so that we can implement
// `rtree.Spatial` interface type.
type Station struct {
feature *geojson.Feature
}
func NewStation(f *geojson.Feature) *Station {
return &Station{f}
}
// Bounds implements `rtree.Spatial` so we can load
// stations into an `rtree.Rtree`.
func (s *Station) Bounds() *rtree.Rect {
return rtree.Point{
s.feature.Geometry.Point[0],
s.feature.Geometry.Point[1],
}.ToRect(1e-6)
}
// loadStations loads the geojson features from
// `subway-stations.geojson` into the `Stations` rtree.
func LoadStations() {
stationsGeojson := GeoJSON["subway-stations.geojson"]
fc, err := geojson.UnmarshalFeatureCollection(stationsGeojson)
if err != nil {
// Note: this will take down the GAE instance by exiting this process.
log.Fatal(err)
}
for _, f := range fc.Features {
Stations.Insert(&Station{f})
glog.Infof("Feature inserted into tree %v %v\n", f.Geometry, f.Properties)
}
}
// subwayStationsHandler reads r for a "viewport" query parameter
// and writes a GeoJSON response of the features contained in
// that viewport into w.
func SubwayStationsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
vp := r.FormValue("viewport")
rect, err := newRect(vp)
if err != nil {
str := fmt.Sprintf("Couldn't parse viewport: %s", err)
http.Error(w, str, 400)
return
}
zm, err := strconv.ParseInt(r.FormValue("zoom"), 10, 0)
if err != nil {
str := fmt.Sprintf("Couldn't parse zoom: %s", err)
http.Error(w, str, 400)
return
}
s := Stations.SearchIntersect(rect)
if len(s) == 0 {
glog.Infof("Nothing in s %v \n", s)
}
fc, err := clusterStations(s, int(zm))
if err != nil {
str := fmt.Sprintf("Couldn't cluster results: %s", err)
http.Error(w, str, 500)
return
}
err = json.NewEncoder(w).Encode(fc)
if err != nil {
str := fmt.Sprintf("Couldn't encode results: %s", err)
http.Error(w, str, 500)
return
}
}
// newRect constructs a `*rtree.Rect` for a viewport.
func newRect(vp string) (*rtree.Rect, error) {
ss := strings.Split(vp, "|")
sw := strings.Split(ss[0], ",")
swLat, err := strconv.ParseFloat(sw[0], 64)
if err != nil {
return nil, err
}
swLng, err := strconv.ParseFloat(sw[1], 64)
if err != nil {
return nil, err
}
ne := strings.Split(ss[1], ",")
neLat, err := strconv.ParseFloat(ne[0], 64)
if err != nil {
return nil, err
}
neLng, err := strconv.ParseFloat(ne[1], 64)
if err != nil {
return nil, err
}
minLat := math.Min(swLat, neLat)
minLng := math.Min(swLng, neLng)
distLat := math.Max(swLat, neLat) - minLat
distLng := math.Max(swLng, neLng) - minLng
// Grow the rect to ameliorate issues with stations
// disappearing on Zoom in, and being slow to appear
// on Pan or Zoom out.
r, err := rtree.NewRect(
rtree.Point{
minLng - distLng/10,
minLat - distLat/10,
},
[]float64{
distLng * 1.2,
distLat * 1.2,
})
if err != nil {
return nil, err
}
return r, nil
}