-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
polygonm.go
67 lines (55 loc) · 1.71 KB
/
polygonm.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
package geom
import (
"errors"
)
// ErrNilPolygonM is thrown when a polygonz is nil but shouldn't be
var ErrNilPolygonM = errors.New("geom: nil PolygonM")
// ErrInvalidLinearRingM is thrown when a LinearRingM is malformed
var ErrInvalidLinearRingM = errors.New("geom: invalid LinearRingM")
// ErrInvalidPolygonM is thrown when a Polygon is malformed
var ErrInvalidPolygonM = errors.New("geom: invalid PolygonM")
// PolygonM is a geometry consisting of multiple closed LineStringMs.
// There must be only one exterior LineStringM with a clockwise winding order.
// There may be one or more interior LineStringMs with a counterclockwise winding orders.
// The last point in the linear ring will not match the first point.
type PolygonM [][][3]float64
// LinearRings returns the coordinates of the linear rings
func (p PolygonM) LinearRings() [][][3]float64 {
return p
}
// SetLinearRingZs modifies the array of 2D+1 coordinates
func (p *PolygonM) SetLinearRings(input [][][3]float64) (err error) {
if p == nil {
return ErrNilPolygonM
}
*p = append((*p)[:0], input...)
return
}
// AsSegments returns the polygon as a slice of lines. This will make no attempt to only add unique segments.
func (p PolygonM) AsSegments() (segs [][]LineM, err error) {
if len(p) == 0 {
return nil, nil
}
segs = make([][]LineM, 0, len(p))
for i := range p {
switch len(p[i]) {
case 0, 1, 2:
continue
// TODO(gdey) : why are we getting invalid points.
/*
case 1, 2:
return nil, ErrInvalidLinearRing
*/
default:
pilen := len(p[i])
subr := make([]LineM, pilen)
pj := pilen - 1
for j := 0; j < pilen; j++ {
subr[j] = LineM{p[i][pj], p[i][j]}
pj = j
}
segs = append(segs, subr)
}
}
return segs, nil
}