forked from twpayne/go-geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipolygon.go
115 lines (102 loc) · 2.79 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
package geom
// A MultiPolygon is a collection of Polygons.
type MultiPolygon struct {
geom3
}
// NewMultiPolygon returns a new MultiPolygon with no Polygons.
func NewMultiPolygon(layout Layout) *MultiPolygon {
return NewMultiPolygonFlat(layout, nil, nil)
}
// NewMultiPolygonFlat returns a new MultiPolygon with the given flat coordinates.
func NewMultiPolygonFlat(layout Layout, flatCoords []float64, endss [][]int) *MultiPolygon {
g := new(MultiPolygon)
g.layout = layout
g.stride = layout.Stride()
g.flatCoords = flatCoords
g.endss = endss
return g
}
// Area returns the sum of the area of the individual Polygons.
func (g *MultiPolygon) Area() float64 {
return doubleArea3(g.flatCoords, 0, g.endss, g.stride) / 2
}
// Clone returns a deep copy.
func (g *MultiPolygon) Clone() *MultiPolygon {
return deriveCloneMultiPolygon(g)
}
// Length returns the sum of the perimeters of the Polygons.
func (g *MultiPolygon) Length() float64 {
return length3(g.flatCoords, 0, g.endss, g.stride)
}
// MustSetCoords sets the coordinates and panics on any error.
func (g *MultiPolygon) MustSetCoords(coords [][][]Coord) *MultiPolygon {
Must(g.SetCoords(coords))
return g
}
// NumPolygons returns the number of Polygons.
func (g *MultiPolygon) NumPolygons() int {
return len(g.endss)
}
// Polygon returns the ith Polygon.
func (g *MultiPolygon) Polygon(i int) *Polygon {
if len(g.endss[i]) == 0 {
return NewPolygon(g.layout)
}
// Find the offset from the previous non-empty polygon element.
offset := 0
lastNonEmptyIdx := i - 1
for lastNonEmptyIdx >= 0 {
ends := g.endss[lastNonEmptyIdx]
if len(ends) > 0 {
offset = ends[len(ends)-1]
break
}
lastNonEmptyIdx--
}
ends := make([]int, len(g.endss[i]))
if offset == 0 {
copy(ends, g.endss[i])
} else {
for j, end := range g.endss[i] {
ends[j] = end - offset
}
}
return NewPolygonFlat(g.layout, g.flatCoords[offset:g.endss[i][len(g.endss[i])-1]], ends)
}
// Push appends a Polygon.
func (g *MultiPolygon) Push(p *Polygon) error {
if p.layout != g.layout {
return ErrLayoutMismatch{Got: p.layout, Want: g.layout}
}
offset := len(g.flatCoords)
var ends []int
if len(p.ends) > 0 {
ends = make([]int, len(p.ends))
if offset == 0 {
copy(ends, p.ends)
} else {
for i, end := range p.ends {
ends[i] = end + offset
}
}
}
g.flatCoords = append(g.flatCoords, p.flatCoords...)
g.endss = append(g.endss, ends)
return nil
}
// SetCoords sets the coordinates.
func (g *MultiPolygon) SetCoords(coords [][][]Coord) (*MultiPolygon, error) {
if err := g.setCoords(coords); err != nil {
return nil, err
}
return g, nil
}
// SetSRID sets the SRID of g.
func (g *MultiPolygon) SetSRID(srid int) *MultiPolygon {
g.srid = srid
return g
}
// Swap swaps the values of g and g2.
func (g *MultiPolygon) Swap(g2 *MultiPolygon) {
*g, *g2 = *g2, *g
}