forked from tidwall/geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
336 lines (306 loc) · 6.72 KB
/
collection.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package geojson
import (
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/rtree"
)
type collection struct {
children []Object
extra *extra
tree *rtree.RTree
prect geometry.Rect
pempty bool
}
func (g *collection) Indexed() bool {
return g.tree != nil
}
func (g *collection) Children() []Object {
return g.children
}
// ForEach ...
func (g *collection) ForEach(iter func(geom Object) bool) bool {
for _, child := range g.children {
if !child.ForEach(iter) {
return false
}
}
return true
}
// Base ...
func (g *collection) Base() []Object {
return g.children
}
func (g *collection) Search(rect geometry.Rect, iter func(child Object) bool) {
if g.tree != nil {
g.tree.Search(
[2]float64{rect.Min.X, rect.Min.Y},
[2]float64{rect.Max.X, rect.Max.Y},
func(_, _ [2]float64, value interface{}) bool {
return iter(value.(Object))
},
)
} else {
for _, child := range g.children {
if child.Empty() {
continue
}
if child.Rect().IntersectsRect(rect) {
if !iter(child) {
break
}
}
}
}
}
// Empty ...
func (g *collection) Empty() bool {
return g.pempty
}
// Valid ...
func (g *collection) Valid() bool {
return g.Rect().Valid()
}
// Rect ...
func (g *collection) Rect() geometry.Rect {
return g.prect
}
// Center ...
func (g *collection) Center() geometry.Point {
return g.Rect().Center()
}
// AppendJSON ...
func (g *collection) AppendJSON(dst []byte) []byte {
// this should never be called
return append(dst, "null"...)
}
// JSON ...
func (g *collection) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *collection) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// String ...
func (g *collection) String() string {
return string(g.AppendJSON(nil))
}
// Within ...
func (g *collection) Within(obj Object) bool {
return obj.Contains(g)
}
// Contains ...
func (g *collection) Contains(obj Object) bool {
if g.Empty() {
return false
}
// all of obj must be contained by any number of the collection children
var objContained bool
obj.ForEach(func(geom Object) bool {
if geom.Empty() {
// ignore empties
return true
}
var geomContained bool
g.Search(geom.Rect(), func(child Object) bool {
if child.Contains(geom) {
// found a child object that contains geom, end inner loop
geomContained = true
return false
}
return true
})
if !geomContained {
// unmark and quit the loop
objContained = false
return false
}
// mark that at least one geom is contained
objContained = true
return true
})
return objContained
}
func (g *collection) Spatial() Spatial { return g }
func (g *collection) WithinRect(rect geometry.Rect) bool {
if g.Empty() {
return false
}
var withinCount int
g.Search(rect, func(child Object) bool {
if child.Spatial().WithinRect(rect) {
withinCount++
return true
}
return false
})
return withinCount == len(g.children)
}
func (g *collection) WithinPoint(point geometry.Point) bool {
if g.Empty() {
return false
}
var withinCount int
g.Search(point.Rect(), func(child Object) bool {
if child.Spatial().WithinPoint(point) {
withinCount++
return true
}
return false
})
return withinCount == len(g.children)
}
func (g *collection) WithinLine(line *geometry.Line) bool {
if g.Empty() {
return false
}
var withinCount int
g.Search(line.Rect(), func(child Object) bool {
if child.Spatial().WithinLine(line) {
withinCount++
return true
}
return false
})
return withinCount == len(g.children)
}
func (g *collection) WithinPoly(poly *geometry.Poly) bool {
if g.Empty() {
return false
}
var withinCount int
g.Search(poly.Rect(), func(child Object) bool {
if child.Spatial().WithinPoly(poly) {
withinCount++
return true
}
return false
})
return withinCount == len(g.children)
}
// Intersects ...
func (g *collection) Intersects(obj Object) bool {
// check if any of obj intersects with any of collection
var intersects bool
obj.ForEach(func(geom Object) bool {
if geom.Empty() {
// ignore the empties
return true
}
g.Search(geom.Rect(), func(child Object) bool {
if child.Intersects(geom) {
intersects = true
return false
}
return true
})
if intersects {
return false
}
return true
})
return intersects
}
func (g *collection) IntersectsPoint(point geometry.Point) bool {
var intersects bool
g.Search(point.Rect(), func(child Object) bool {
if child.Spatial().IntersectsPoint(point) {
intersects = true
return false
}
return true
})
return intersects
}
func (g *collection) IntersectsRect(rect geometry.Rect) bool {
var intersects bool
g.Search(rect, func(child Object) bool {
if child.Spatial().IntersectsRect(rect) {
intersects = true
return false
}
return true
})
return intersects
}
func (g *collection) IntersectsLine(line *geometry.Line) bool {
var intersects bool
g.Search(line.Rect(), func(child Object) bool {
if child.Spatial().IntersectsLine(line) {
intersects = true
return false
}
return true
})
return intersects
}
func (g *collection) IntersectsPoly(poly *geometry.Poly) bool {
var intersects bool
g.Search(poly.Rect(), func(child Object) bool {
if child.Spatial().IntersectsPoly(poly) {
intersects = true
return false
}
return true
})
return intersects
}
// NumPoints ...
func (g *collection) NumPoints() int {
var n int
for _, child := range g.children {
n += child.NumPoints()
}
return n
}
func (g *collection) parseInitRectIndex(opts *ParseOptions) {
g.pempty = true
var count int
for _, child := range g.children {
if child.Empty() {
continue
}
if g.pempty && !child.Empty() {
g.pempty = false
}
if count == 0 {
g.prect = child.Rect()
} else {
if len(g.children) == 1 {
g.prect = child.Rect()
} else {
g.prect = unionRects(g.prect, child.Rect())
}
}
count++
}
if count > 0 && opts.IndexChildren != 0 && count >= opts.IndexChildren {
g.tree = new(rtree.RTree)
for _, child := range g.children {
if child.Empty() {
continue
}
rect := child.Rect()
g.tree.Insert(
[2]float64{rect.Min.X, rect.Min.Y},
[2]float64{rect.Max.X, rect.Max.Y},
child,
)
}
}
}
// Distance ...
func (g *collection) Distance(obj Object) float64 {
return obj.Spatial().DistancePoint(g.Center())
}
func (g *collection) DistancePoint(point geometry.Point) float64 {
return geoDistancePoints(g.Center(), point)
}
func (g *collection) DistanceRect(rect geometry.Rect) float64 {
return geoDistancePoints(g.Center(), rect.Center())
}
func (g *collection) DistanceLine(line *geometry.Line) float64 {
return geoDistancePoints(g.Center(), line.Rect().Center())
}
func (g *collection) DistancePoly(poly *geometry.Poly) float64 {
return geoDistancePoints(g.Center(), poly.Rect().Center())
}