-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature.go
150 lines (129 loc) · 3.72 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
package geojson
import (
"encoding/json"
"fmt"
)
// "type" properties.
const (
TypePropFeature = "Feature"
TypePropFeatureCollection = "FeatureCollection"
)
// Feature consists of a specific geometry type and a list of properties.
type Feature struct {
Geometry Geometry
BBox *BoundingBox
Properties PropertyList
}
// Geometry contains the points represented by a particular geometry type.
type Geometry interface {
json.Marshaler
json.Unmarshaler
Type() GeometryType
Validate() error
}
// MarshalJSON returns the JSON encoding of the Feature.
func (f Feature) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
BBox *BoundingBox `json:"bbox,omitempty"`
Geometry Geometry `json:"geometry"`
Properties PropertyList `json:"properties,omitempty"`
}{
Type: TypePropFeature,
BBox: f.BBox,
Geometry: f.Geometry,
Properties: f.Properties,
})
}
// UnmarshalJSON parses the JSON-encoded data and stores the result.
func (f *Feature) UnmarshalJSON(data []byte) error {
var feature struct {
Type string `json:"type"`
BBox *BoundingBox `json:"bbox,omitempty"`
Geometry json.RawMessage `json:"geometry"`
Properties PropertyList `json:"properties,omitempty"`
}
if err := json.Unmarshal(data, &feature); err != nil {
return err
} else if feature.Type != TypePropFeature {
return fmt.Errorf("type is '%s', expecting '%s'", feature.Type, TypePropFeature)
}
f.BBox = feature.BBox
f.Properties = feature.Properties
geo, err := unmarshalGeometry(feature.Geometry)
if err != nil {
return err
}
f.Geometry = geo
return nil
}
// WithBoundingBox sets the optional bounding box.
func (f *Feature) WithBoundingBox(bottomLeft, topRight Position) *Feature {
f.BBox = &BoundingBox{
BottomLeft: bottomLeft,
TopRight: topRight,
}
return f
}
// WithProperties sets the optional properties, removing all existing properties.
func (f *Feature) WithProperties(props ...Property) *Feature {
f.Properties = PropertyList(props)
return f
}
// AddProperty appends a new property.
func (f *Feature) AddProperty(name string, value interface{}) *Feature {
f.Properties = append(f.Properties, Property{
Name: name,
Value: value,
})
return f
}
// FeatureCollection is a list of Features.
type FeatureCollection struct {
Features []Feature
BBox *BoundingBox
}
// NewFeatureCollection returns a FeatureCollection consisting of the supplied Features.
func NewFeatureCollection(features ...*Feature) *FeatureCollection {
c := FeatureCollection{
Features: make([]Feature, len(features)),
}
for i, f := range features {
c.Features[i] = *f
}
return &c
}
// MarshalJSON returns the JSON encoding of the FeatureCollection.
func (c FeatureCollection) MarshalJSON() ([]byte, error) {
return json.Marshal(&featureCollection{
Type: TypePropFeatureCollection,
BBox: c.BBox,
Features: c.Features,
})
}
// UnmarshalJSON parses the JSON-encoded data and stores the result.
func (c *FeatureCollection) UnmarshalJSON(data []byte) error {
col := featureCollection{}
if err := json.Unmarshal(data, &col); err != nil {
return err
}
if col.Type != TypePropFeatureCollection {
return fmt.Errorf("type is '%s', expecting '%s'", col.Type, TypePropFeatureCollection)
}
c.BBox = col.BBox
c.Features = col.Features
return nil
}
// WithBoundingBox sets the optional bounding box.
func (c *FeatureCollection) WithBoundingBox(bottomLeft, topRight Position) *FeatureCollection {
c.BBox = &BoundingBox{
BottomLeft: bottomLeft,
TopRight: topRight,
}
return c
}
type featureCollection struct {
Type string `json:"type"`
BBox *BoundingBox `json:"bbox,omitempty"`
Features []Feature `json:"features"`
}