-
Notifications
You must be signed in to change notification settings - Fork 103
/
example_test.go
193 lines (162 loc) · 4.59 KB
/
example_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
191
192
193
package geojson_test
import (
"encoding/json"
"fmt"
"log"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/quadtree"
)
func ExampleFeature_Point() {
f := geojson.NewFeature(orb.Point{1, 1})
f.Properties["key"] = "value"
qt := quadtree.New(f.Geometry.Bound().Pad(1))
err := qt.Add(f) // add the feature to a quadtree
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
// type assert the feature back into a Feature from
// the orb.Pointer interface.
feature := qt.Find(orb.Point{0, 0}).(*geojson.Feature)
fmt.Printf("key=%s", feature.Properties["key"])
// Output:
// key=value
}
func ExampleFeatureCollection_foreignMembers() {
rawJSON := []byte(`
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
],
"title": "Title as Foreign Member"
}`)
fc := geojson.NewFeatureCollection()
err := json.Unmarshal(rawJSON, &fc)
if err != nil {
log.Fatalf("invalid json: %v", err)
}
fmt.Println(fc.Features[0].Geometry)
fmt.Println(fc.ExtraMembers["title"])
data, _ := json.Marshal(fc)
fmt.Println(string(data))
// Output:
// [102 0.5]
// Title as Foreign Member
// {"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"value0"}}],"title":"Title as Foreign Member","type":"FeatureCollection"}
}
// MyFeatureCollection is a depricated/no longer supported way to extract
// foreign/extra members from a feature collection. Now an UnmarshalJSON
// method, like below, is required for it to work.
type MyFeatureCollection struct {
geojson.FeatureCollection
Title string `json:"title"`
}
// UnmarshalJSON implemented as below is now required for the extra members
// to be decoded directly into the type.
func (fc *MyFeatureCollection) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &fc.FeatureCollection)
if err != nil {
return err
}
fc.Title = fc.ExtraMembers.MustString("title", "")
return nil
}
func ExampleFeatureCollection_foreignMembersCustom() {
// Note: this approach to handling foreign/extra members requires
// implementing an `UnmarshalJSON` method on the new type.
// See MyFeatureCollection type and its UnmarshalJSON function above.
rawJSON := []byte(`
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
],
"title": "Title as Foreign Member"
}`)
fc := &MyFeatureCollection{}
err := json.Unmarshal(rawJSON, &fc)
if err != nil {
log.Fatalf("invalid json: %v", err)
}
fmt.Println(fc.FeatureCollection.Features[0].Geometry)
fmt.Println(fc.Features[0].Geometry)
fmt.Println(fc.Title)
// Output:
// [102 0.5]
// [102 0.5]
// Title as Foreign Member
}
func ExampleUnmarshalFeatureCollection() {
rawJSON := []byte(`
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]
}`)
fc, _ := geojson.UnmarshalFeatureCollection(rawJSON)
// Geometry will be unmarshalled into the correct geo.Geometry type.
point := fc.Features[0].Geometry.(orb.Point)
fmt.Println(point)
// Output:
// [102 0.5]
}
func Example_unmarshal() {
rawJSON := []byte(`
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]
}`)
fc := geojson.NewFeatureCollection()
err := json.Unmarshal(rawJSON, &fc)
if err != nil {
log.Fatalf("invalid json: %v", err)
}
// Geometry will be unmarshalled into the correct geo.Geometry type.
point := fc.Features[0].Geometry.(orb.Point)
fmt.Println(point)
// Output:
// [102 0.5]
}
func ExampleFeatureCollection_MarshalJSON() {
fc := geojson.NewFeatureCollection()
fc.Append(geojson.NewFeature(orb.Point{1, 2}))
_, err := fc.MarshalJSON()
if err != nil {
log.Fatalf("marshal error: %v", err)
}
// standard lib encoding/json package will also work
data, err := json.MarshalIndent(fc, "", " ")
if err != nil {
log.Fatalf("marshal error: %v", err)
}
fmt.Println(string(data))
// Output:
// {
// "features": [
// {
// "type": "Feature",
// "geometry": {
// "type": "Point",
// "coordinates": [
// 1,
// 2
// ]
// },
// "properties": null
// }
// ],
// "type": "FeatureCollection"
// }
}