This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_collection_test.go
190 lines (158 loc) · 4.57 KB
/
feature_collection_test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package geojson
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/planetfederal/orb"
)
func TestNewFeatureCollection(t *testing.T) {
fc := NewFeatureCollection()
if fc.Type != "FeatureCollection" {
t.Errorf("should have type of FeatureCollection, got %v", fc.Type)
}
}
func TestUnmarshalFeatureCollection(t *testing.T) {
rawJSON := `
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}`
fc, err := UnmarshalFeatureCollection([]byte(rawJSON))
if err != nil {
t.Fatalf("should unmarshal feature collection without issue, err %v", err)
}
if fc.Type != "FeatureCollection" {
t.Errorf("should have type of FeatureCollection, got %v", fc.Type)
}
if len(fc.Features) != 3 {
t.Errorf("should have 3 features but got %d", len(fc.Features))
}
f := fc.Features[0]
if gt := f.Geometry.GeoJSONType(); gt != "Point" {
t.Errorf("incorrect feature type: %v != %v", gt, "Point")
}
f = fc.Features[1]
if gt := f.Geometry.GeoJSONType(); gt != "LineString" {
t.Errorf("incorrect feature type: %v != %v", gt, "LineString")
}
f = fc.Features[2]
if gt := f.Geometry.GeoJSONType(); gt != "Polygon" {
t.Errorf("incorrect feature type: %v != %v", gt, "Polygon")
}
// check unmarshal/marshal loop
var expected interface{}
err = json.Unmarshal([]byte(rawJSON), &expected)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
data, err := json.MarshalIndent(fc, "", " ")
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
var raw interface{}
err = json.Unmarshal(data, &raw)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !reflect.DeepEqual(raw, expected) {
t.Errorf("invalid marshalling: \n%v", string(data))
}
// not a feature collection
data, _ = NewFeature(orb.Point{}).MarshalJSON()
_, err = UnmarshalFeatureCollection(data)
if err == nil {
t.Error("should return error if not a feature collection")
}
if !strings.Contains(err.Error(), "not a feature collection") {
t.Errorf("incorrect error: %v", err)
}
// invalid json
_, err = UnmarshalFeatureCollection([]byte(`{"type": "FeatureCollection",`)) // truncated
if err == nil {
t.Errorf("should return error for invalid json")
}
}
func TestFeatureCollectionMarshalJSON(t *testing.T) {
fc := NewFeatureCollection()
blob, err := fc.MarshalJSON()
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}
func TestFeatureCollectionMarshal(t *testing.T) {
fc := NewFeatureCollection()
fc.Features = nil
blob, err := json.Marshal(fc)
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}
func TestFeatureCollectionMarshal_BBox(t *testing.T) {
fc := NewFeatureCollection()
// nil bbox
fc.BBox = nil
blob, err := json.Marshal(fc)
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if bytes.Contains(blob, []byte(`"bbox"`)) {
t.Errorf("should not contain bbox attribute if empty")
}
// with a bbox
fc.BBox = []float64{1, 2, 3, 4}
blob, err = json.Marshal(fc)
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"bbox":[1,2,3,4]`)) {
t.Errorf("did not marshal bbox correctly: %v", string(blob))
}
}
func TestFeatureCollectionMarshalValue(t *testing.T) {
fc := NewFeatureCollection()
fc.Features = nil
blob, err := json.Marshal(*fc)
if err != nil {
t.Fatalf("should marshal to json just fine but got %v", err)
}
if !bytes.Contains(blob, []byte(`"features":[]`)) {
t.Errorf("json should set features object to at least empty array")
}
}