forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_collection.go
68 lines (57 loc) · 1.91 KB
/
feature_collection.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
/*
Package geojson is a library for encoding and decoding GeoJSON into Go structs using
the geometries in the orb package. Supports both the json.Marshaler and json.Unmarshaler
interfaces as well as helper functions such as `UnmarshalFeatureCollection` and `UnmarshalFeature`.
*/
package geojson
import (
"encoding/json"
"fmt"
)
const featureCollection = "FeatureCollection"
// A FeatureCollection correlates to a GeoJSON feature collection.
type FeatureCollection struct {
Type string `json:"type"`
BBox BBox `json:"bbox,omitempty"`
Features []*Feature `json:"features"`
}
// NewFeatureCollection creates and initializes a new feature collection.
func NewFeatureCollection() *FeatureCollection {
return &FeatureCollection{
Type: featureCollection,
Features: []*Feature{},
}
}
// Append appends a feature to the collection.
func (fc *FeatureCollection) Append(feature *Feature) *FeatureCollection {
fc.Features = append(fc.Features, feature)
return fc
}
// MarshalJSON converts the feature collection object into the proper JSON.
// It will handle the encoding of all the child features and geometries.
// Alternately one can call json.Marshal(fc) directly for the same result.
func (fc FeatureCollection) MarshalJSON() ([]byte, error) {
type tempFC FeatureCollection
c := tempFC{
Type: featureCollection,
BBox: fc.BBox,
Features: fc.Features,
}
if c.Features == nil {
c.Features = []*Feature{}
}
return json.Marshal(c)
}
// UnmarshalFeatureCollection decodes the data into a GeoJSON feature collection.
// Alternately one can call json.Unmarshal(fc) directly for the same result.
func UnmarshalFeatureCollection(data []byte) (*FeatureCollection, error) {
fc := &FeatureCollection{}
err := json.Unmarshal(data, fc)
if err != nil {
return nil, err
}
if fc.Type != featureCollection {
return nil, fmt.Errorf("geojson: not a feature collection: type=%s", fc.Type)
}
return fc, nil
}