-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpoint.go
257 lines (218 loc) · 5.13 KB
/
point.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
package geojson
import (
"math"
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/gjson"
)
type Point struct {
base geometry.Point
extra *extra
}
func NewPoint(point geometry.Point) *Point {
return &Point{base: point}
}
func NewPointZ(point geometry.Point, z float64) *Point {
return &Point{
base: point,
extra: &extra{dims: 1, values: []float64{z}},
}
}
func (g *Point) ForEach(iter func(geom Object) bool) bool {
return iter(g)
}
func (g *Point) Empty() bool {
return g.base.Empty()
}
func (g *Point) Valid() bool {
return g.base.Valid()
}
func (g *Point) Rect() geometry.Rect {
return g.base.Rect()
}
func (g *Point) Spatial() Spatial {
return g
}
func (g *Point) Center() geometry.Point {
return g.base
}
func (g *Point) Base() geometry.Point {
return g.base
}
func (g *Point) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"Point","coordinates":`...)
dst = appendJSONPoint(dst, g.base, g.extra, 0)
dst = g.extra.appendJSONExtra(dst, false)
dst = append(dst, '}')
return dst
}
func (g *Point) JSON() string {
return string(g.AppendJSON(nil))
}
func (g *Point) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func (g *Point) String() string {
return string(g.AppendJSON(nil))
}
func (g *Point) Within(obj Object) bool {
return obj.Contains(g)
}
func (g *Point) Contains(obj Object) bool {
return obj.Spatial().WithinPoint(g.base)
}
func (g *Point) Intersects(obj Object) bool {
if obj, ok := obj.(*Circle); ok {
return obj.Contains(g)
}
return obj.Spatial().IntersectsPoint(g.base)
}
func (g *Point) WithinRect(rect geometry.Rect) bool {
return rect.ContainsPoint(g.base)
}
func (g *Point) WithinPoint(point geometry.Point) bool {
return point.ContainsPoint(g.base)
}
func (g *Point) WithinLine(line *geometry.Line) bool {
return line.ContainsPoint(g.base)
}
func (g *Point) WithinPoly(poly *geometry.Poly) bool {
return poly.ContainsPoint(g.base)
}
func (g *Point) IntersectsPoint(point geometry.Point) bool {
return g.base.IntersectsPoint(point)
}
func (g *Point) IntersectsRect(rect geometry.Rect) bool {
return g.base.IntersectsRect(rect)
}
func (g *Point) IntersectsLine(line *geometry.Line) bool {
return g.base.IntersectsLine(line)
}
func (g *Point) IntersectsPoly(poly *geometry.Poly) bool {
return g.base.IntersectsPoly(poly)
}
func (g *Point) NumPoints() int {
return 1
}
func (g *Point) Z() float64 {
if g.extra != nil && len(g.extra.values) > 0 {
return g.extra.values[0]
}
return 0
}
func parseJSONPoint(keys *parseKeys, opts *ParseOptions) (Object, error) {
var o Object
base, extra, err := parseJSONPointCoords(keys, gjson.Result{}, opts)
if err != nil {
return nil, err
}
if err := parseBBoxAndExtras(&extra, keys, opts); err != nil {
return nil, err
}
if extra == nil && opts.AllowSimplePoints {
var g SimplePoint
g.Point = base
o = &g
} else {
var g Point
g.base = base
g.extra = extra
o = &g
}
if opts.RequireValid {
if !o.Valid() {
return nil, errCoordinatesInvalid
}
}
return o, nil
}
func parseJSONPointCoords(
keys *parseKeys, rcoords gjson.Result, opts *ParseOptions,
) (geometry.Point, *extra, error) {
var coords geometry.Point
var ex *extra
if !rcoords.Exists() {
rcoords = keys.rCoordinates
if !rcoords.Exists() {
return coords, nil, errCoordinatesMissing
}
if !rcoords.IsArray() {
return coords, nil, errCoordinatesInvalid
}
}
var err error
var count int
var nums [4]float64
rcoords.ForEach(func(key, value gjson.Result) bool {
if count == 4 {
return false
}
if value.Type != gjson.Number {
if value.Type == gjson.Null {
// convert "null" to "NaN"
nums[count] = math.NaN()
count++
return true
}
err = errCoordinatesInvalid
return false
}
nums[count] = value.Float()
count++
return true
})
if err != nil {
return coords, nil, err
}
if count < 2 {
return coords, nil, errCoordinatesInvalid
}
coords = geometry.Point{X: nums[0], Y: nums[1]}
if count > 2 {
ex = new(extra)
if count > 3 {
ex.dims = 2
} else {
ex.dims = 1
}
ex.values = make([]float64, count-2)
for i := 2; i < count; i++ {
ex.values[i-2] = nums[i]
}
}
return coords, ex, nil
}
func (g *Point) Distance(obj Object) float64 {
return obj.Spatial().DistancePoint(g.base)
}
func (g *Point) DistancePoint(point geometry.Point) float64 {
return geoDistancePoints(g.Center(), point)
}
func (g *Point) DistanceRect(rect geometry.Rect) float64 {
return geoDistancePoints(g.Center(), rect.Center())
}
func (g *Point) DistanceLine(line *geometry.Line) float64 {
return geoDistancePoints(g.Center(), line.Rect().Center())
}
func (g *Point) DistancePoly(poly *geometry.Poly) float64 {
return geoDistancePoints(g.Center(), poly.Rect().Center())
}
// IsSimple returns true if the Point can be converted to a SimplePoint
func (g *Point) IsSimple() bool {
return g.extra == nil
}
// IsPoint returns true if the object is a {"type":"Point"}
func IsPoint(obj Object) (z float64, ok bool) {
switch pt := obj.(type) {
case *SimplePoint:
return 0, true
case *Point:
return pt.Z(), true
}
return 0, false
}
func (g *Point) Members() string {
if g.extra != nil {
return g.extra.members
}
return ""
}