-
Notifications
You must be signed in to change notification settings - Fork 58
/
example_test.go
53 lines (44 loc) · 1.23 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
package geojson_test
import (
"fmt"
geojson "github.com/paulmach/go.geojson"
)
func ExampleUnmarshalFeatureCollection() {
rawFeatureJSON := []byte(`
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]
}`)
fc, err := geojson.UnmarshalFeatureCollection(rawFeatureJSON)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("%s", fc.Features[0].Properties["prop0"])
// Output: value0
}
func ExampleUnmarshalGeometry() {
rawGeometryJSON := []byte(`{"type": "Point", "coordinates": [102.0, 0.5]}`)
g, err := geojson.UnmarshalGeometry(rawGeometryJSON)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("%s", g.Type)
// Output: Point
}
func ExampleFeatureCollection_MarshalJSON() {
fc := geojson.NewFeatureCollection()
fc.AddFeature(geojson.NewPointFeature([]float64{1, 2}))
rawJSON, err := fc.MarshalJSON()
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("%s", string(rawJSON))
// Output: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}
}