-
Notifications
You must be signed in to change notification settings - Fork 28
/
multipolygon.go
116 lines (106 loc) · 2.47 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
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
package geojson
import (
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/gjson"
)
type MultiPolygon struct{ collection }
func NewMultiPolygon(polys []*geometry.Poly) *MultiPolygon {
g := new(MultiPolygon)
for _, poly := range polys {
g.children = append(g.children, NewPolygon(poly))
}
g.parseInitRectIndex(DefaultParseOptions)
return g
}
func (g *MultiPolygon) AppendJSON(dst []byte) []byte {
dst = append(dst, `{"type":"MultiPolygon","coordinates":[`...)
for i, g := range g.children {
if i > 0 {
dst = append(dst, ',')
}
dst = append(dst,
gjson.GetBytes(g.AppendJSON(nil), "coordinates").String()...)
}
dst = append(dst, ']')
if g.extra != nil {
dst = g.extra.appendJSONExtra(dst, false)
}
dst = append(dst, '}')
return dst
}
func (g *MultiPolygon) String() string {
return string(g.AppendJSON(nil))
}
func (g *MultiPolygon) Valid() bool {
valid := true
for _, p := range g.children {
if !p.Valid() {
valid = false
}
}
return valid
}
func (g *MultiPolygon) JSON() string {
return string(g.AppendJSON(nil))
}
func (g *MultiPolygon) MarshalJSON() ([]byte, error) {
return g.AppendJSON(nil), nil
}
func parseJSONMultiPolygon(
keys *parseKeys, opts *ParseOptions,
) (Object, error) {
var g MultiPolygon
var err error
if !keys.rCoordinates.Exists() {
return nil, errCoordinatesMissing
}
if !keys.rCoordinates.IsArray() {
return nil, errCoordinatesInvalid
}
var coords [][]geometry.Point
var ex *extra
keys.rCoordinates.ForEach(func(_, value gjson.Result) bool {
coords, ex, err = parseJSONPolygonCoords(keys, value, opts)
if err != nil {
return false
}
if len(coords) == 0 {
err = errCoordinatesInvalid // must be a linear ring
return false
}
for _, p := range coords {
if len(p) < 4 || p[0] != p[len(p)-1] {
err = errCoordinatesInvalid // must be a linear ring
return false
}
}
exterior := coords[0]
var holes [][]geometry.Point
if len(coords) > 1 {
holes = coords[1:]
}
gopts := toGeometryOpts(opts)
poly := geometry.NewPoly(exterior, holes, &gopts)
g.children = append(g.children, &Polygon{base: *poly, extra: ex})
return true
})
if err != nil {
return nil, err
}
if err := parseBBoxAndExtras(&g.extra, keys, opts); err != nil {
return nil, err
}
if opts.RequireValid {
if !g.Valid() {
return nil, errCoordinatesInvalid
}
}
g.parseInitRectIndex(opts)
return &g, nil
}
func (g *MultiPolygon) Members() string {
if g.extra != nil {
return g.extra.members
}
return ""
}