-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
78 lines (63 loc) · 1.58 KB
/
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
69
70
71
72
73
74
75
76
77
78
//
// Copyright (C) 2021 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/geojson
//
package geojson
import (
"encoding/json"
)
// Features Collection Type
type Collection[T interface{ BoundingBox() BoundingBox }] struct {
Features []T
}
// BoundingBox of the features collection
func (c Collection[T]) BoundingBox() BoundingBox {
if len(c.Features) == 0 {
return nil
}
bbox := c.Features[0].BoundingBox()
for i := 1; i < len(c.Features); i++ {
bbox.Join(c.Features[i].BoundingBox())
}
return bbox
}
// Encodes FeatureCollection GeoJSON
func (c Collection[T]) MarshalJSON() ([]byte, error) {
features, err := json.Marshal(c.Features)
if err != nil {
return nil, err
}
any := struct {
Type string `json:"type"`
BBox BoundingBox `json:"bbox,omitempty"`
Features json.RawMessage `json:"features,omitempty"`
}{
Type: "FeatureCollection",
BBox: c.BoundingBox(),
Features: features,
}
return json.Marshal(any)
}
// Decodes FeatureCollection GeoJSON
func (c *Collection[T]) UnmarshalJSON(b []byte) error {
type anyCollection struct {
Type string `json:"type"`
Features json.RawMessage `json:"features,omitempty"`
}
any := anyCollection{}
if err := json.Unmarshal(b, &any); err != nil {
return err
}
if any.Type != "FeatureCollection" {
return ErrorUnsupportedType
}
if any.Features != nil {
if err := json.Unmarshal(any.Features, &c.Features); err != nil {
return err
}
}
return nil
}