forked from tidwall/geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.go
225 lines (190 loc) · 4.88 KB
/
feature.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 geojson
import (
"strings"
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/gjson"
"github.com/tidwall/pretty"
"github.com/tidwall/sjson"
)
// Feature ...
type Feature struct {
base Object
extra *extra
}
// NewFeature returns a new GeoJSON Feature.
// The members must be a valid json object such as
// `{"id":"391","properties":{}}`, or it must be an empty string. It should not
// contain a "feature" member.
func NewFeature(geometry Object, members string) *Feature {
g := new(Feature)
g.base = geometry
members = strings.TrimSpace(members)
if members != "" && members != "{}" {
if gjson.Valid(members) && gjson.Parse(members).IsObject() {
if gjson.Get(members, "feature").Exists() {
members, _ = sjson.Delete(members, "feature")
}
g.extra = new(extra)
g.extra.members = string(pretty.UglyInPlace([]byte(members)))
}
}
return g
}
// ForEach ...
func (g *Feature) ForEach(iter func(geom Object) bool) bool {
return iter(g)
}
// Empty ...
func (g *Feature) Empty() bool {
return g.base.Empty()
}
// Valid ...
func (g *Feature) Valid() bool {
return g.base.Valid()
}
// Rect ...
func (g *Feature) Rect() geometry.Rect {
return g.base.Rect()
}
// Center ...
func (g *Feature) Center() geometry.Point {
return g.Rect().Center()
}
// Base ...
func (g *Feature) Base() Object {
return g.base
}
// Members ...
func (g *Feature) Members() string {
if g.extra != nil {
return g.extra.members
}
return ""
}
// AppendJSON ...
func (g *Feature) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"Feature","geometry":`...)
dst = g.base.AppendJSON(dst)
dst = g.extra.appendJSONExtra(dst, true)
dst = append(dst, '}')
return dst
}
// String ...
func (g *Feature) String() string {
return string(g.AppendJSON(nil))
}
// JSON ...
func (g *Feature) JSON() string {
return string(g.AppendJSON(nil))
}
// MarshalJSON ...
func (g *Feature) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
// Spatial ...
func (g *Feature) Spatial() Spatial {
return g
}
// Within ...
func (g *Feature) Within(obj Object) bool {
return obj.Contains(g)
}
// Contains ...
func (g *Feature) Contains(obj Object) bool {
return g.base.Contains(obj)
}
// WithinRect ...
func (g *Feature) WithinRect(rect geometry.Rect) bool {
return g.base.Spatial().WithinRect(rect)
}
// WithinPoint ...
func (g *Feature) WithinPoint(point geometry.Point) bool {
return g.base.Spatial().WithinPoint(point)
}
// WithinLine ...
func (g *Feature) WithinLine(line *geometry.Line) bool {
return g.base.Spatial().WithinLine(line)
}
// WithinPoly ...
func (g *Feature) WithinPoly(poly *geometry.Poly) bool {
return g.base.Spatial().WithinPoly(poly)
}
// Intersects ...
func (g *Feature) Intersects(obj Object) bool {
return g.base.Intersects(obj)
}
// IntersectsPoint ...
func (g *Feature) IntersectsPoint(point geometry.Point) bool {
return g.base.Spatial().IntersectsPoint(point)
}
// IntersectsRect ...
func (g *Feature) IntersectsRect(rect geometry.Rect) bool {
return g.base.Spatial().IntersectsRect(rect)
}
// IntersectsLine ...
func (g *Feature) IntersectsLine(line *geometry.Line) bool {
return g.base.Spatial().IntersectsLine(line)
}
// IntersectsPoly ...
func (g *Feature) IntersectsPoly(poly *geometry.Poly) bool {
return g.base.Spatial().IntersectsPoly(poly)
}
// NumPoints ...
func (g *Feature) NumPoints() int {
return g.base.NumPoints()
}
// parseJSONFeature will return a valid GeoJSON object.
func parseJSONFeature(keys *parseKeys, opts *ParseOptions) (Object, error) {
var g Feature
if !keys.rGeometry.Exists() {
return nil, errGeometryMissing
}
var err error
g.base, err = Parse(keys.rGeometry.Raw, opts)
if err != nil {
return nil, err
}
if err := parseBBoxAndExtras(&g.extra, keys, opts); err != nil {
return nil, err
}
if point, ok := g.base.(*Point); ok {
if g.extra != nil {
members := g.extra.members
if !opts.DisableCircleType &&
gjson.Get(members, "properties.type").String() == "Circle" {
// Circle
radius := gjson.Get(members, "properties.radius").Float()
units := gjson.Get(members, "properties.radius_units").String()
switch units {
case "", "m":
case "km":
radius *= 1000
default:
return nil, errCircleRadiusUnitsInvalid
}
return NewCircle(point.base, radius, 64), nil
}
}
}
return &g, nil
}
// Distance ...
func (g *Feature) Distance(obj Object) float64 {
return g.base.Distance(obj)
}
// DistancePoint ...
func (g *Feature) DistancePoint(point geometry.Point) float64 {
return g.base.Spatial().DistancePoint(point)
}
// DistanceRect ...
func (g *Feature) DistanceRect(rect geometry.Rect) float64 {
return g.base.Spatial().DistanceRect(rect)
}
// DistanceLine ...
func (g *Feature) DistanceLine(line *geometry.Line) float64 {
return g.base.Spatial().DistanceLine(line)
}
// DistancePoly ...
func (g *Feature) DistancePoly(poly *geometry.Poly) float64 {
return g.base.Spatial().DistancePoly(poly)
}