forked from arne-cl/tile38
/
object.go
441 lines (421 loc) 路 11.5 KB
/
object.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package geojson
import (
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"github.com/quesurifn/tile38/pkg/geojson/poly"
)
const (
point = 0
multiPoint = 1
lineString = 2
multiLineString = 3
polygon = 4
multiPolygon = 5
geometryCollection = 6
feature = 7
featureCollection = 8
)
var (
errNotEnoughData = errors.New("not enough data")
errTooMuchData = errors.New("too much data")
errInvalidData = errors.New("invalid data")
)
var ( // json errors
fmtErrTypeIsUnknown = "The type '%s' is unknown"
errInvalidTypeMember = errors.New("Type member is invalid. Expecting a string")
errInvalidCoordinates = errors.New("Coordinates member is invalid. Expecting an array")
errCoordinatesRequired = errors.New("Coordinates member is required")
errInvalidGeometries = errors.New("Geometries member is invalid. Expecting an array")
errGeometriesRequired = errors.New("Geometries member is required")
errInvalidGeometryMember = errors.New("Geometry member is invalid. Expecting an object")
errGeometryMemberRequired = errors.New("Geometry member is required")
errInvalidFeaturesMember = errors.New("Features member is invalid. Expecting an array")
errFeaturesMemberRequired = errors.New("Features member is required")
errInvalidFeature = errors.New("Invalid feature in collection")
errInvalidPropertiesMember = errors.New("Properties member in invalid. Expecting an array")
errInvalidCoordinatesValue = errors.New("Coordinates member has an invalid value")
errLineStringInvalidCoordinates = errors.New("Coordinates must be an array of two or more positions")
errInvalidNumberOfPositionValues = errors.New("Position must have two or more numbers")
errInvalidPositionValue = errors.New("Position has an invalid value")
errCoordinatesMustBeArray = errors.New("Coordinates member must be an array of positions")
errMustBeALinearRing = errors.New("Polygon must have at least 4 positions and the first and last position must be the same")
errBBoxInvalidType = errors.New("BBox member is an invalid. Expecting an array")
errBBoxInvalidNumberOfValues = errors.New("BBox member requires exactly 4 or 6 values")
errBBoxInvalidValue = errors.New("BBox has an invalid value")
errInvalidGeometry = errors.New("Invalid geometry in collection")
)
const nilz = 0
// Object is a geojson object
type Object interface {
bboxPtr() *BBox
hasPositions() bool
appendJSON(dst []byte) []byte
// WithinBBox detects if the object is fully contained inside a bbox.
WithinBBox(bbox BBox) bool
// IntersectsBBox detects if the object intersects a bbox.
IntersectsBBox(bbox BBox) bool
// Within detects if the object is fully contained inside another object.
Within(o Object) bool
// Intersects detects if the object intersects another object.
Intersects(o Object) bool
// WithinCircle detects if the object is fully contained inside a circle.
WithinCircle(center Position, meters float64) bool
// IntersectsCircle detects if the object intersects a circle.
IntersectsCircle(center Position, meters float64) bool
// Nearby detects if the object is nearby a position.
Nearby(center Position, meters float64) bool
// CalculatedBBox is exterior bbox containing the object.
CalculatedBBox() BBox
// CalculatedPoint is a point representation of the object.
CalculatedPoint() Position
// JSON is the json representation of the object. This might not be exactly the same as the original.
JSON() string
// String returns a string representation of the object. This may be JSON or something else.
String() string
// PositionCount return the number of coordinates.
PositionCount() int
// Weight returns the in-memory size of the object.
Weight() int
// MarshalJSON allows the object to be encoded in json.Marshal calls.
MarshalJSON() ([]byte, error)
// Geohash converts the object to a geohash value.
Geohash(precision int) (string, error)
// IsBBoxDefined returns true if the object has a defined bbox.
IsBBoxDefined() bool
// IsGeometry return true if the object is a geojson geometry object. false if it something else.
IsGeometry() bool
}
func positionBBox(i int, bbox BBox, ps []Position) (int, BBox) {
for _, p := range ps {
if i == 0 {
bbox.Min = p
bbox.Max = p
} else {
if p.X < bbox.Min.X {
bbox.Min.X = p.X
}
if p.Y < bbox.Min.Y {
bbox.Min.Y = p.Y
}
if p.X > bbox.Max.X {
bbox.Max.X = p.X
}
if p.Y > bbox.Max.Y {
bbox.Max.Y = p.Y
}
}
i++
}
return i, bbox
}
func isLinearRing(ps []Position) bool {
return len(ps) >= 4 && ps[0] == ps[len(ps)-1]
}
// ObjectJSON parses geojson and returns an Object
func ObjectJSON(json string) (Object, error) {
return objectMap(json, root)
}
var (
root = 0 // accept all types
gcoll = 1 // accept only geometries
feat = 2 // accept only geometries
fcoll = 3 // accept only features
)
func objectMap(json string, from int) (Object, error) {
var err error
res := gjson.Get(json, "type")
if res.Type != gjson.String {
return nil, errInvalidTypeMember
}
typ := res.String()
if from != root {
switch from {
case gcoll, feat:
switch typ {
default:
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
case "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection":
}
case fcoll:
switch typ {
default:
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
case "Feature":
}
}
}
var o Object
switch typ {
default:
return nil, fmt.Errorf(fmtErrTypeIsUnknown, typ)
case "Point":
o, err = fillSimplePointOrPoint(fillLevel1Map(json))
case "MultiPoint":
o, err = fillMultiPoint(fillLevel2Map(json))
case "LineString":
o, err = fillLineString(fillLevel2Map(json))
case "MultiLineString":
o, err = fillMultiLineString(fillLevel3Map(json))
case "Polygon":
o, err = fillPolygon(fillLevel3Map(json))
case "MultiPolygon":
o, err = fillMultiPolygon(fillLevel4Map(json))
case "GeometryCollection":
o, err = fillGeometryCollectionMap(json)
case "Feature":
o, err = fillFeatureMap(json)
case "FeatureCollection":
o, err = fillFeatureCollectionMap(json)
}
return o, err
}
func withinObjectShared(g Object, o Object, pin func(v Polygon) bool) bool {
bbp := o.bboxPtr()
if bbp != nil {
if !g.WithinBBox(*bbp) {
return false
}
if o.IsBBoxDefined() {
return true
}
}
switch v := o.(type) {
default:
return false
case Point:
return g.WithinBBox(v.CalculatedBBox())
case SimplePoint:
return g.WithinBBox(v.CalculatedBBox())
case MultiPoint:
for i := range v.Coordinates {
if g.Within(Point{Coordinates: v.Coordinates[i]}) {
return true
}
}
return false
case LineString:
if len(v.Coordinates) == 0 {
return false
}
switch g := g.(type) {
default:
return false
case SimplePoint:
return poly.Point(Position{X: g.X, Y: g.Y, Z: 0}).IntersectsLineString(polyPositions(v.Coordinates))
case Point:
return poly.Point(g.Coordinates).IntersectsLineString(polyPositions(v.Coordinates))
case MultiPoint:
if len(v.Coordinates) == 0 {
return false
}
for _, p := range v.Coordinates {
if !poly.Point(p).IntersectsLineString(polyPositions(v.Coordinates)) {
return false
}
}
return true
}
case MultiLineString:
for i := range v.Coordinates {
if g.Within(v.getLineString(i)) {
return true
}
}
return false
case Polygon:
if len(v.Coordinates) == 0 {
return false
}
return pin(v)
case MultiPolygon:
for i := range v.Coordinates {
if pin(v.getPolygon(i)) {
return true
}
}
return false
case Feature:
return g.Within(v.Geometry)
case FeatureCollection:
if len(v.Features) == 0 {
return false
}
for _, f := range v.Features {
if !g.Within(f) {
return false
}
}
return true
case GeometryCollection:
if len(v.Geometries) == 0 {
return false
}
for _, f := range v.Geometries {
if !g.Within(f) {
return false
}
}
return true
}
}
func intersectsObjectShared(g Object, o Object, pin func(v Polygon) bool) bool {
bbp := o.bboxPtr()
if bbp != nil {
if !g.IntersectsBBox(*bbp) {
return false
}
if o.IsBBoxDefined() {
return true
}
}
switch v := o.(type) {
default:
return false
case Point:
return g.IntersectsBBox(v.CalculatedBBox())
case SimplePoint:
return g.IntersectsBBox(v.CalculatedBBox())
case MultiPoint:
for i := range v.Coordinates {
if (Point{Coordinates: v.Coordinates[i]}).Intersects(g) {
return true
}
}
return false
case LineString:
if g, ok := g.(LineString); ok {
a := polyPositions(g.Coordinates)
b := polyPositions(v.Coordinates)
return a.LineStringIntersectsLineString(b)
}
return o.Intersects(g)
case MultiLineString:
for i := range v.Coordinates {
if g.Intersects(v.getLineString(i)) {
return true
}
}
return false
case Polygon:
if len(v.Coordinates) == 0 {
return false
}
return pin(v)
case MultiPolygon:
for _, coords := range v.Coordinates {
if pin(Polygon{Coordinates: coords}) {
return true
}
}
return false
case Feature:
return g.Intersects(v.Geometry)
case FeatureCollection:
if len(v.Features) == 0 {
return false
}
for _, f := range v.Features {
if g.Intersects(f) {
return true
}
}
return false
case GeometryCollection:
if len(v.Geometries) == 0 {
return false
}
for _, f := range v.Geometries {
if g.Intersects(f) {
return true
}
}
return false
}
}
// CirclePolygon returns a Polygon around the radius.
func CirclePolygon(x, y, meters float64, steps int) Polygon {
if steps < 3 {
steps = 3
}
p := Polygon{
Coordinates: [][]Position{make([]Position, steps+1)},
}
center := Position{X: x, Y: y, Z: 0}
step := 360.0 / float64(steps)
i := 0
for deg := 360.0; deg > 0; deg -= step {
c := Position(poly.Point(center.Destination(meters, deg)))
p.Coordinates[0][i] = c
i++
}
p.Coordinates[0][i] = p.Coordinates[0][0]
return p
}
// The object's calculated bounding box must intersect the radius of the circle to pass.
func nearbyObjectShared(g Object, x, y float64, meters float64) bool {
if !g.hasPositions() {
return false
}
center := Position{X: x, Y: y, Z: 0}
bbox := g.CalculatedBBox()
if bbox.Min.X == bbox.Max.X && bbox.Min.Y == bbox.Max.Y {
// just a point, return is point is inside of the circle
return center.DistanceTo(bbox.Min) <= meters
}
circlePoly := CirclePolygon(x, y, meters, 12)
return g.Intersects(circlePoly)
}
func jsonMarshalString(s string) []byte {
for i := 0; i < len(s); i++ {
if s[i] < ' ' || s[i] == '\\' || s[i] == '"' || s[i] > 126 {
b, _ := json.Marshal(s)
return b
}
}
b := make([]byte, len(s)+2)
b[0] = '"'
copy(b[1:], s)
b[len(b)-1] = '"'
return b
}
func stripWhitespace(s string) string {
var p []byte
var str bool
var escs int
for i := 0; i < len(s); i++ {
c := s[i]
if str {
// We're inside of a string. Look out for '"' and '\' characters.
if c == '\\' {
// Increment the escape character counter.
escs++
} else {
if c == '"' && escs%2 == 0 {
// We reached the end of string
str = false
}
// Reset the escape counter
escs = 0
}
} else if c == '"' {
// We encountared a double quote character.
str = true
} else if c <= ' ' {
// Ignore the whitespace
if p == nil {
p = []byte(s[:i])
}
continue
}
// Append the character
if p != nil {
p = append(p, c)
}
}
if p == nil {
return s
}
return string(p)
}