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