/
line_centroid.go
114 lines (96 loc) · 3.86 KB
/
line_centroid.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
package xy
import (
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/xy/internal"
)
// LinesCentroid computes the centroid of all the LineStrings provided as arguments.
//
// Algorithm: Compute the average of the midpoints of all line segments weighted by the segment length.
func LinesCentroid(line *geom.LineString, extraLines ...*geom.LineString) (centroid geom.Coord) {
calculator := NewLineCentroidCalculator(line.Layout())
calculator.AddLine(line)
for _, l := range extraLines {
calculator.AddLine(l)
}
return calculator.GetCentroid()
}
// LinearRingsCentroid computes the centroid of all the LinearRings provided as arguments.
//
// Algorithm: Compute the average of the midpoints of all line segments weighted by the segment length.
func LinearRingsCentroid(line *geom.LinearRing, extraLines ...*geom.LinearRing) (centroid geom.Coord) {
calculator := NewLineCentroidCalculator(line.Layout())
calculator.AddLinearRing(line)
for _, l := range extraLines {
calculator.AddLinearRing(l)
}
return calculator.GetCentroid()
}
// MultiLineCentroid computes the centroid of the MultiLineString string
//
// Algorithm: Compute the average of the midpoints of all line segments weighted by the segment length.
func MultiLineCentroid(line *geom.MultiLineString) (centroid geom.Coord) {
calculator := NewLineCentroidCalculator(line.Layout())
start := 0
for _, end := range line.Ends() {
calculator.addLine(line.FlatCoords(), start, end)
start = end
}
return calculator.GetCentroid()
}
// LineCentroidCalculator is the data structure that contains the centroid calculation
// data. This type cannot be used using its 0 values, it must be created
// using NewLineCentroid
type LineCentroidCalculator struct {
layout geom.Layout
stride int
centSum geom.Coord
totalLength float64
}
// NewLineCentroidCalculator creates a new instance of the calculator.
// Once a calculator is created polygons, linestrings or linear rings can be added and the
// GetCentroid method can be used at any point to get the current centroid
// the centroid will naturally change each time a geometry is added
func NewLineCentroidCalculator(layout geom.Layout) *LineCentroidCalculator {
return &LineCentroidCalculator{
layout: layout,
stride: layout.Stride(),
centSum: geom.Coord(make([]float64, layout.Stride())),
}
}
// GetCentroid obtains centroid currently calculated. Returns a 0 coord if no geometries have been added
func (calc *LineCentroidCalculator) GetCentroid() geom.Coord {
cent := geom.Coord(make([]float64, calc.layout.Stride()))
cent[0] = calc.centSum[0] / calc.totalLength
cent[1] = calc.centSum[1] / calc.totalLength
return cent
}
// AddPolygon adds a Polygon to the calculation.
func (calc *LineCentroidCalculator) AddPolygon(polygon *geom.Polygon) *LineCentroidCalculator {
for i := 0; i < polygon.NumLinearRings(); i++ {
calc.AddLinearRing(polygon.LinearRing(i))
}
return calc
}
// AddLine adds a LineString to the current calculation
func (calc *LineCentroidCalculator) AddLine(line *geom.LineString) *LineCentroidCalculator {
coords := line.FlatCoords()
calc.addLine(coords, 0, len(coords))
return calc
}
// AddLinearRing adds a LinearRing to the current calculation
func (calc *LineCentroidCalculator) AddLinearRing(line *geom.LinearRing) *LineCentroidCalculator {
coords := line.FlatCoords()
calc.addLine(coords, 0, len(coords))
return calc
}
func (calc *LineCentroidCalculator) addLine(line []float64, startLine, endLine int) {
lineMinusLastPoint := endLine - calc.stride
for i := startLine; i < lineMinusLastPoint; i += calc.stride {
segmentLen := internal.Distance2D(geom.Coord(line[i:i+2]), geom.Coord(line[i+calc.stride:i+calc.stride+2]))
calc.totalLength += segmentLen
midx := (line[i] + line[i+calc.stride]) / 2
calc.centSum[0] += segmentLen * midx
midy := (line[i+1] + line[i+calc.stride+1]) / 2
calc.centSum[1] += segmentLen * midy
}
}