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
/
example_test.go
138 lines (115 loc) · 2.91 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
package geojson_test
import (
"encoding/json"
"fmt"
"log"
"github.com/planetfederal/orb"
"github.com/planetfederal/orb/geojson"
"github.com/planetfederal/orb/quadtree"
)
func ExampleFeature_Point() {
f := geojson.NewFeature(orb.Point{1, 1})
f.Properties["key"] = "value"
qt := quadtree.New(f.Geometry.Bound().Pad(1))
qt.Add(f) // add the feature to a quadtree
// 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"
}`)
type MyFeatureCollection struct {
geojson.FeatureCollection
Title string `json:"title"`
}
fc := &MyFeatureCollection{}
json.Unmarshal(rawJSON, &fc)
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}))
data, 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:
// {
// "type": "FeatureCollection",
// "features": [
// {
// "type": "Feature",
// "geometry": {
// "type": "Point",
// "coordinates": [
// 1,
// 2
// ]
// },
// "properties": null
// }
// ]
// }
}