-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature.go
236 lines (208 loc) · 5.93 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
226
227
228
229
230
231
232
233
234
235
236
package joejson
import (
"encoding/json"
"fmt"
)
// TypeFeature is the value for a Feature's 'type' member.
const TypeFeature string = "Feature"
// Feature represents a spatially bounded 'thing'.
type Feature struct {
// ID is an optional Feature identifier ('id').
ID any
// Properties is an optional JSON object ('properties').
Properties map[string]any
// Bbox optionally includes information on the coordinate range for the Feature Geometry.
Bbox []Position
// geometry is an unexported field representing one of
// (Point|LineString|MultiPoint|MultiLineString|Polygon|MultiPolygon|GeometryCollection).
geometry any
}
// GeometryType is the type of the Feature's Geometry.
func (f Feature) GeometryType() string {
switch f.geometry.(type) {
case Point:
return GeometryTypePoint
case MultiPoint:
return GeometryTypeMultiPoint
case LineString:
return GeometryTypeLineString
case MultiLineString:
return GeometryTypeMultiLineString
case Polygon:
return GeometryTypePolygon
case MultiPolygon:
return GeometryTypeMultiPolygon
case GeometryCollection:
return GeometryTypeGeometryCollection
default:
return ""
}
}
// WithPoint sets the Feature's Geometry to the provided Point.
func (f Feature) WithPoint(g Point) Feature {
f.geometry = g
return f
}
// AsPoint casts the Feature's Geometry to a Point.
func (f Feature) AsPoint() (Point, bool) {
p, ok := f.geometry.(Point)
return p, ok
}
// WithMultiPoint sets the Feature's Geometry to the provided MultiPoint.
func (f Feature) WithMultiPoint(g MultiPoint) Feature {
f.geometry = g
return f
}
// AsMultiPoint casts the Feature's Geometry to a MultiPoint.
func (f Feature) AsMultiPoint() (MultiPoint, bool) {
p, ok := f.geometry.(MultiPoint)
return p, ok
}
// WithLineString sets the Feature's Geometry to the provided LineString.
func (f Feature) WithLineString(g LineString) Feature {
f.geometry = g
return f
}
// AsLineString casts the Feature's Geometry to a LineString.
func (f Feature) AsLineString() (LineString, bool) {
p, ok := f.geometry.(LineString)
return p, ok
}
// WithMultiLineString sets the Feature's Geometry to the provided LineMultiString.
func (f Feature) WithMultiLineString(g MultiLineString) Feature {
f.geometry = g
return f
}
// AsMultiLineString casts the Feature's Geometry to a MultiLineString.
func (f Feature) AsMultiLineString() (MultiLineString, bool) {
p, ok := f.geometry.(MultiLineString)
return p, ok
}
// WithPolygon sets the Feature's Geometry to the provided Polygon.
func (f Feature) WithPolygon(g Polygon) Feature {
f.geometry = g
return f
}
// AsPolygon casts the Feature's Geometry to a Polygon.
func (f Feature) AsPolygon() (Polygon, bool) {
p, ok := f.geometry.(Polygon)
return p, ok
}
// WithMultiPolygon sets the Feature's Geometry to the provided MultiPolygon.
func (f Feature) WithMultiPolygon(g MultiPolygon) Feature {
f.geometry = g
return f
}
// AsMultiPolygon casts the Feature's Geometry to a MultiPolygon.
func (f Feature) AsMultiPolygon() (MultiPolygon, bool) {
p, ok := f.geometry.(MultiPolygon)
return p, ok
}
// WithGeometryCollection sets the Feature's Geometry to the provided GeometryCollection.
func (f Feature) WithGeometryCollection(g GeometryCollection) Feature {
f.geometry = g
return f
}
// AsGeometryCollection casts Feature's Geometry to a GeometryCollection.
func (f Feature) AsGeometryCollection() (GeometryCollection, bool) {
p, ok := f.geometry.(GeometryCollection)
return p, ok
}
// MarshalJSON is a custom JSON marshaller.
func (f Feature) MarshalJSON() ([]byte, error) {
switch f.ID.(type) {
case string, uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64, nil:
default:
return nil, fmt.Errorf(`invalid type "%T" for id, expected string or numeric`, f.ID)
}
return json.Marshal(&struct {
ID any `json:"id,omitempty"`
Type string `json:"type"`
Geometry any `json:"geometry"`
Properties map[string]any `json:"properties,omitempty"`
BBox []Position `json:"bbox,omitempty"`
}{
f.ID,
TypeFeature,
f.geometry,
f.Properties,
f.Bbox,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (f *Feature) UnmarshalJSON(b []byte) error {
var tmp struct {
ID any `json:"id"`
Geometry json.RawMessage `json:"geometry"`
Properties map[string]any `json:"properties"`
Bbox []Position `json:"bbox"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
switch tmp.ID.(type) {
case string, float64, nil:
default:
return fmt.Errorf(`invalid type "%T" for id, expected string or numeric`, tmp.ID)
}
f.ID = tmp.ID
f.Properties = tmp.Properties
f.Bbox = tmp.Bbox
var err error
f.geometry, err = unmarshalGeometry(tmp.Geometry)
return err
}
func unmarshalGeometry(bs []byte) (any, error) {
var tmp struct {
Type string `json:"type"`
}
if err := json.Unmarshal(bs, &tmp); err != nil {
return nil, err
}
switch tmp.Type {
case GeometryTypePoint:
var g Point
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypeMultiPoint:
var g MultiPoint
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypeLineString:
var g LineString
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypeMultiLineString:
var g MultiLineString
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypePolygon:
var g Polygon
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypeMultiPolygon:
var g MultiPolygon
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
case GeometryTypeGeometryCollection:
var g GeometryCollection
if err := json.Unmarshal(bs, &g); err != nil {
return nil, err
}
return g, nil
default:
return nil, fmt.Errorf("unknown geometry type: %q", tmp.Type)
}
}