-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultipolygon.go
59 lines (49 loc) · 1.32 KB
/
multipolygon.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"
)
// GeometryTypeMultiPolygon is the value for a MultiPolygon's 'type' member.
const GeometryTypeMultiPolygon = "MultiPolygon"
// MultiPolygon is a slice of Polygon geometries.
type MultiPolygon []Polygon
// Raw exposes the data for this geometry as primitive types.
func (p MultiPolygon) Raw() [][][][]float64 {
out := make([][][][]float64, len(p))
for i, pl := range p {
out[i] = pl.Raw()
}
return out
}
// MarshalJSON is a custom JSON marshaller.
func (p MultiPolygon) MarshalJSON() ([]byte, error) {
lrs := make([][]LinearRing, 0, len(p))
for _, lr := range p {
lrs = append(lrs, lr)
}
return json.Marshal(&struct {
Polygons [][]LinearRing `json:"coordinates"`
Type string `json:"type"`
}{
lrs,
GeometryTypeMultiPolygon,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (p *MultiPolygon) UnmarshalJSON(b []byte) error {
var tmp struct {
Polygons [][]LinearRing `json:"coordinates"`
Type string `json:"type"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
if tmp.Type != GeometryTypeMultiPolygon {
return fmt.Errorf("invalid type %q, expected %q", tmp.Type, GeometryTypeMultiPolygon)
}
*p = make(MultiPolygon, len(tmp.Polygons))
for i, pl := range tmp.Polygons {
[]Polygon(*p)[i] = pl
}
return nil
}