-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
225 lines (202 loc) · 5.82 KB
/
query.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
package main
import (
"bytes"
"database/sql"
"fmt"
"math/rand"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
"github.com/pkg/errors"
)
type shapeType string
type operationType string
const (
// cellShape is the bounds of an s2 cell. This is the best case for s2.
cellShape shapeType = "cell"
// capShape is a point and radius.
capShape shapeType = "cap"
// rectShape is the rectangle definited by two opposite corners.
rectShape shapeType = "rect"
// containsOperation retrieves indexed bounding boxes entirely contained by
// this shape's bounding box.
containsOperation operationType = "contains"
// containsOperation retrieves indexed bounding boxes entirely containing
// this shape's bounding box.
containingOperation operationType = "containing"
// intersectsOperation retrieves indexed bounding boxes that intersect
// this shape's bounding box. This is the union of contains and containing.
intersectsOperation operationType = "intersects"
)
func arcFromLevel(level int) float64 {
return s2.AvgAngleSpanMetric.Value(level)
}
func metersFromLevel(level int) float64 {
return float64(earthRadiusMeters) * arcFromLevel(level)
}
type queryReader struct {
rr *roadReader
rng *rand.Rand
selectivity int
op operationType
center s2.LatLng
}
func makeQueryReader(path string, selectivity int, queryOp operationType) (*queryReader, error) {
rr, err := makeRoadReader(path)
if err != nil {
return nil, err
}
qr := &queryReader{
rr: rr,
rng: rand.New(rand.NewSource(0)),
selectivity: selectivity,
op: queryOp,
}
return qr, nil
}
func (qr *queryReader) Next() (query, bool) {
for {
road, ok := qr.rr.Next()
if !ok {
return query{}, false
}
if !(qr.rng.Intn(100) < qr.selectivity) {
continue
}
q := query{
center: road.lls[0],
shape: queryShape,
op: qr.op,
}
return q, ok
}
}
type query struct {
center s2.LatLng
shape shapeType
op operationType
}
// ancestorCells returns the set of cells containing these cells, not including
// the given cells themselves.
func ancestorCells(cells ...s2.CellID) []s2.CellID {
var ancestors []s2.CellID
seen := make(map[s2.CellID]struct{})
for _, c := range cells {
for l := c.Level() - 1; l >= 0; l-- {
a := c.Parent(l)
if _, ok := seen[a]; ok {
break
}
ancestors = append(ancestors, a)
seen[a] = struct{}{}
}
}
return ancestors
}
func containsQ(w *bytes.Buffer, cells []s2.CellID) {
for i, c := range cells {
if i != 0 {
w.WriteString(` OR `)
}
fmt.Fprintf(w, `s2 BETWEEN '%s' AND '%s'`, c.RangeMin().ToToken(), c.RangeMax().ToToken())
}
}
func containingQ(w *bytes.Buffer, cells []s2.CellID) {
w.WriteString(`s2 IN (`)
for i, c := range cells {
if i != 0 {
w.WriteString(`, `)
}
fmt.Fprintf(w, `'%s'`, c.ToToken())
}
w.WriteString(`)`)
}
var errQuerySkipped = errors.New(`skipped`)
func (q query) ReadS2(db *sql.DB, cfg *s2IndexConfig, level int) (int64, error) {
var r s2.Region
switch q.shape {
case cellShape:
r = s2.CellFromCellID(s2.CellIDFromLatLng(q.center).Parent(level))
case capShape:
arc := s1.Angle(arcFromLevel(level))
r = s2.CapFromCenterAngle(s2.PointFromLatLng(q.center), arc)
case rectShape:
cell := s2.CellFromCellID(s2.CellIDFromLatLng(q.center).Parent(level))
rect := s2.NewRectBounder()
rect.AddPoint(cell.Vertex(0))
rect.AddPoint(cell.Vertex(2))
r = rect.RectBound()
default:
panic(`unhandled shape: ` + q.shape)
}
covering := cfg.Covering(r)
if covering == nil {
// Couldn't do this covering.
return 0, errQuerySkipped
}
var queryBuf bytes.Buffer
queryBuf.WriteString(`SELECT `)
if cfg.maxCells == 1 || true {
// Take advantage of the fact that there's only one cell indexed for each
// shape.
queryBuf.WriteString(`count(id)`)
} else {
queryBuf.WriteString(`count(distinct(id))`)
}
queryBuf.WriteString(` FROM roads_s2_idx WHERE `)
switch q.op {
case containsOperation:
containsQ(&queryBuf, covering)
case containingOperation:
containingQ(&queryBuf, append(ancestorCells(covering...), covering...))
case intersectsOperation:
containsQ(&queryBuf, covering)
queryBuf.WriteString(` OR `)
// Don't append the covering cells themselves like we do for
// containingOperation because the LIKE already handles it.
containingQ(&queryBuf, ancestorCells(covering...))
default:
panic(`unhandled operation: ` + q.op)
}
// TODO(dan): Prepare all these. A little tricky since containing and
// intersects each need a one version for each cell level.
var count int64
err := db.QueryRow(queryBuf.String()).Scan(&count)
err = errors.Wrapf(err, `executing: %s`, queryBuf.String())
return count, err
}
func (q query) ReadPostGIS(db *sql.DB, level int) (int64, error) {
var geom string
switch q.shape {
case cellShape, rectShape:
cell := s2.CellFromCellID(s2.CellIDFromLatLng(q.center).Parent(level))
v0, v2 := s2.LatLngFromPoint(cell.Vertex(0)), s2.LatLngFromPoint(cell.Vertex(2))
// Yeah, lng and lat and swapped from what you'd expect.
geom = fmt.Sprintf(`ST_MakeEnvelope(%v, %v, %v, %v)`,
v0.Lng.Degrees(), v0.Lat.Degrees(), v2.Lng.Degrees(), v2.Lat.Degrees())
case capShape:
// Yeah, lng and lat and swapped from what you'd expect.
geom = fmt.Sprintf(`ST_Buffer(ST_MakePoint(%v, %v)::geography, %d)::geometry`,
q.center.Lng.Degrees(), q.center.Lat.Degrees(), int(metersFromLevel(level)))
default:
panic(`unhandled shape: ` + q.shape)
}
var op string
switch q.op {
case containsOperation:
op = `@`
case containingOperation:
op = `~`
case intersectsOperation:
op = `&&`
default:
panic(`unhandled operation: ` + q.op)
}
query := fmt.Sprintf(
`WITH t (geom) AS (SELECT %s) SELECT count(id) FROM roads, t WHERE roads.geometry %s t.geom`,
geom, op,
)
var count int64
err := db.QueryRow(query).Scan(&count)
err = errors.Wrapf(err, `executing: %s`, query)
return count, err
}