-
Notifications
You must be signed in to change notification settings - Fork 1
/
multilinestring.go
59 lines (49 loc) · 1.38 KB
/
multilinestring.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
package joejson
import (
"encoding/json"
"fmt"
)
// GeometryTypeMultiLineString is the value for a MultiLineString's 'type' member.
const GeometryTypeMultiLineString = "MultiLineString"
// MultiLineString is a slice of LineStrings.
type MultiLineString []LineString
// Raw exposes the data for this geometry as primitive types.
func (g MultiLineString) Raw() [][][]float64 {
out := make([][][]float64, len(g))
for i, ls := range g {
out[i] = ls.Raw()
}
return out
}
// MarshalJSON is a custom JSON marshaller.
func (g MultiLineString) MarshalJSON() ([]byte, error) {
positions := make([][]Position, 0, len(g))
for _, ls := range g {
positions = append(positions, ls)
}
return json.Marshal(&struct {
LineStrings [][]Position `json:"coordinates"`
Type string `json:"type"`
}{
positions,
GeometryTypeMultiLineString,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (g *MultiLineString) UnmarshalJSON(b []byte) error {
var tmp struct {
LineStrings [][]Position `json:"coordinates"`
Type string `json:"type"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
if tmp.Type != GeometryTypeMultiLineString {
return fmt.Errorf("invalid type %q, expected %q", tmp.Type, GeometryTypeMultiLineString)
}
*g = make([]LineString, len(tmp.LineStrings))
for i, pl := range tmp.LineStrings {
[]LineString(*g)[i] = pl
}
return nil
}