-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfeaturecollection.go
83 lines (73 loc) · 1.72 KB
/
featurecollection.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
package geojson
import (
"strings"
"github.com/tidwall/gjson"
)
type FeatureCollection struct{ collection }
func NewFeatureCollection(features []Object) *FeatureCollection {
g := new(FeatureCollection)
g.children = features
g.parseInitRectIndex(DefaultParseOptions)
return g
}
// AppendJSON appends the GeoJSON reprensentation to dst
func (g *FeatureCollection) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"FeatureCollection","features":[`...)
for i := 0; i < len(g.children); i++ {
if i > 0 {
dst = append(dst, ',')
}
dst = g.children[i].AppendJSON(dst)
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
strings.Index("", " ")
return dst
}
func (g *FeatureCollection) String() string {
return string(g.AppendJSON(nil))
}
func (g *FeatureCollection) JSON() string {
return string(g.AppendJSON(nil))
}
func (g *FeatureCollection) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONFeatureCollection(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {
var g FeatureCollection
if !keys.rFeatures.Exists() {
return nil, errFeaturesMissing
}
if !keys.rFeatures.IsArray() {
return nil, errFeaturesInvalid
}
var err error
keys.rFeatures.ForEach(func(key, value gjson.Result) bool {
var f Object
f, err = Parse(value.Raw, opts)
if err != nil {
return false
}
g.children = append(g.children, f)
return true
})
if err != nil {
return nil, err
}
if err := parseBBoxAndExtras(&g.extra, keys, opts); err != nil {
return nil, err
}
g.parseInitRectIndex(opts)
return &g, nil
}
func (g *FeatureCollection) Members() string {
if g.extra != nil {
return g.extra.members
}
return ""
}