-
Notifications
You must be signed in to change notification settings - Fork 4
/
feature_collection.go
168 lines (150 loc) · 3.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
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
package geo
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
const (
featureCollectionJSONPrefix = `{"type":"FeatureCollection","features":[`
featureCollectionWKTPrefix = `GEOMETRYCOLLECTION`
)
// FeatureCollection represents a feature collection.
type FeatureCollection []*Feature
// Equal compares one feature collection to another.
func (coll FeatureCollection) Equal(g Geometry) bool {
other, ok := g.(*FeatureCollection)
if !ok {
return false
}
if len(coll) != len(*other) {
return false
}
for i, feat := range coll {
if !feat.Equal((*other)[i]) {
return false
}
}
return true
}
// Contains returns true if every feature in the collection
// contains the p, and false otherwise.
func (coll FeatureCollection) Contains(p Point) bool {
for _, feat := range coll {
if !feat.Contains(p) {
return false
}
}
return true
}
// MarshalJSON marshals the feature collection to geojson.
func (coll FeatureCollection) MarshalJSON() ([]byte, error) {
buf := []byte(featureCollectionJSONPrefix)
for i, feature := range coll {
feat, err := json.Marshal(feature)
if err != nil {
return nil, err
}
if i != 0 {
buf = append(buf, ',')
}
buf = append(buf, feat...)
}
return append(buf, ']', '}'), nil
}
// Scan scans the feature collection from WKT.
// This method expects a GEOMETRYCOLLECTION.
// TODO: implement this.
func (coll *FeatureCollection) Scan(src interface{}) error {
return scan(coll, src)
}
// scan scans the feature collection from WKT.
// This method expects a GEOMETRYCOLLECTION.
// TODO: implement this.
func (coll *FeatureCollection) scan(s string) error {
return nil
}
// String converts the feature collection to a GEOMETRYCOLLECTION.
func (coll FeatureCollection) String() string {
s := featureCollectionWKTPrefix + "("
for i, feat := range coll {
if i == 0 {
s += feat.Geometry.String()
} else {
s += ", " + feat.String()
}
}
return s + ")"
}
// featureCollection is a utility type used to unmarshal a geojson FeatureCollection.
type featureCollection struct {
Type string `json:"type"`
Features []*feature `json:"features"`
BBox []float64 `json:"bbox"`
}
func (fc *featureCollection) ToFeatureCollection() (*FeatureCollection, error) {
coll := make([]*Feature, len(fc.Features))
for i, feat := range fc.Features {
f, err := feat.ToFeature()
if err != nil {
return nil, err
}
coll[i] = f
}
featcoll := FeatureCollection(coll)
return &featcoll, nil
}
// UnmarshalJSON unmarshals the feature collection from geojson.
func (coll *FeatureCollection) UnmarshalJSON(data []byte) error {
fc, err := unmarshalFeatureCollection(data)
if err != nil {
return err
}
featcoll, err := fc.ToFeatureCollection()
if err != nil {
return err
}
*coll = *featcoll
return nil
}
// Value returns WKT for the feature collection.
// Note that this returns a GEOMETRYCOLLECTION.
func (coll FeatureCollection) Value() (driver.Value, error) {
return coll.String(), nil
}
// Transform transforms the geometry point by point.
func (coll *FeatureCollection) Transform(t Transformer) {
for _, feat := range *coll {
feat.Transform(t)
}
}
// VisitCoordinates visits each point in the geometry.
func (coll FeatureCollection) VisitCoordinates(v Visitor) {
for _, feat := range coll {
feat.VisitCoordinates(v)
}
}
func unmarshalFeatureCollection(data []byte) (*featureCollection, error) {
fc := &featureCollection{}
if err := json.Unmarshal(data, fc); err != nil {
return nil, err
}
// Check the type.
if expected, got := FeatureCollectionType, fc.Type; expected != got {
return nil, fmt.Errorf("expected %s type, got %s", expected, got)
}
return fc, nil
}
func unmarshalFeatureCollectionBBox(data []byte) (Geometry, error) {
fc, err := unmarshalFeatureCollection(data)
if err != nil {
return nil, err
}
coll, err := fc.ToFeatureCollection()
if err != nil {
return nil, err
}
if len(fc.BBox) > 0 {
return WithBBox(fc.BBox, coll), nil
}
return coll, nil
}