aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/planar/area.go
blob: 0482bbd63609c5c7e7ebdee956a71a5c7627043a (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Package planar computes properties on geometries assuming they are
// in 2d euclidean space.
package planar

import (
	"fmt"
	"math"

	"github.com/paulmach/orb"
)

// Area returns the area of the geometry in the 2d plane.
func Area(g orb.Geometry) float64 {
	// TODO: make faster non-centroid version.
	_, a := CentroidArea(g)
	return a
}

// CentroidArea returns both the centroid and the area in the 2d plane.
// Since the area is need for the centroid, return both.
// Polygon area will always be >= zero. Ring area my be negative if it has
// a clockwise winding orider.
func CentroidArea(g orb.Geometry) (orb.Point, float64) {
	if g == nil {
		return orb.Point{}, 0
	}

	switch g := g.(type) {
	case orb.Point:
		return multiPointCentroid(orb.MultiPoint{g}), 0
	case orb.MultiPoint:
		return multiPointCentroid(g), 0
	case orb.LineString:
		return multiLineStringCentroid(orb.MultiLineString{g}), 0
	case orb.MultiLineString:
		return multiLineStringCentroid(g), 0
	case orb.Ring:
		return ringCentroidArea(g)
	case orb.Polygon:
		return polygonCentroidArea(g)
	case orb.MultiPolygon:
		return multiPolygonCentroidArea(g)
	case orb.Collection:
		return collectionCentroidArea(g)
	case orb.Bound:
		return CentroidArea(g.ToRing())
	}

	panic(fmt.Sprintf("geometry type not supported: %T", g))
}

func multiPointCentroid(mp orb.MultiPoint) orb.Point {
	if len(mp) == 0 {
		return orb.Point{}
	}

	x, y := 0.0, 0.0
	for _, p := range mp {
		x += p[0]
		y += p[1]
	}

	num := float64(len(mp))
	return orb.Point{x / num, y / num}
}

func multiLineStringCentroid(mls orb.MultiLineString) orb.Point {
	point := orb.Point{}
	dist := 0.0

	if len(mls) == 0 {
		return orb.Point{}
	}

	validCount := 0
	for _, ls := range mls {
		c, d := lineStringCentroidDist(ls)
		if d == math.Inf(1) {
			continue
		}

		dist += d
		validCount++

		if d == 0 {
			d = 1.0
		}

		point[0] += c[0] * d
		point[1] += c[1] * d
	}

	if validCount == 0 {
		return orb.Point{}
	}

	if dist == math.Inf(1) || dist == 0.0 {
		point[0] /= float64(validCount)
		point[1] /= float64(validCount)
		return point
	}

	point[0] /= dist
	point[1] /= dist

	return point
}

func lineStringCentroidDist(ls orb.LineString) (orb.Point, float64) {
	dist := 0.0
	point := orb.Point{}

	if len(ls) == 0 {
		return orb.Point{}, math.Inf(1)
	}

	// implicitly move everything to near the origin to help with roundoff
	offset := ls[0]
	for i := 0; i < len(ls)-1; i++ {
		p1 := orb.Point{
			ls[i][0] - offset[0],
			ls[i][1] - offset[1],
		}

		p2 := orb.Point{
			ls[i+1][0] - offset[0],
			ls[i+1][1] - offset[1],
		}

		d := Distance(p1, p2)

		point[0] += (p1[0] + p2[0]) / 2.0 * d
		point[1] += (p1[1] + p2[1]) / 2.0 * d
		dist += d
	}

	if dist == 0 {
		return ls[0], 0
	}

	point[0] /= dist
	point[1] /= dist

	point[0] += ls[0][0]
	point[1] += ls[0][1]
	return point, dist
}

func ringCentroidArea(r orb.Ring) (orb.Point, float64) {
	centroid := orb.Point{}
	area := 0.0

	if len(r) == 0 {
		return orb.Point{}, 0
	}

	// implicitly move everything to near the origin to help with roundoff
	offsetX := r[0][0]
	offsetY := r[0][1]
	for i := 1; i < len(r)-1; i++ {
		a := (r[i][0]-offsetX)*(r[i+1][1]-offsetY) -
			(r[i+1][0]-offsetX)*(r[i][1]-offsetY)
		area += a

		centroid[0] += (r[i][0] + r[i+1][0] - 2*offsetX) * a
		centroid[1] += (r[i][1] + r[i+1][1] - 2*offsetY) * a
	}

	if area == 0 {
		return r[0], 0
	}

	// no need to deal with first and last vertex since we "moved"
	// that point the origin (multiply by 0 == 0)

	area /= 2
	centroid[0] /= 6 * area
	centroid[1] /= 6 * area

	centroid[0] += offsetX
	centroid[1] += offsetY

	return centroid, area
}

func polygonCentroidArea(p orb.Polygon) (orb.Point, float64) {
	if len(p) == 0 {
		return orb.Point{}, 0
	}

	centroid, area := ringCentroidArea(p[0])
	area = math.Abs(area)
	if len(p) == 1 {
		if area == 0 {
			c, _ := lineStringCentroidDist(orb.LineString(p[0]))
			return c, 0
		}
		return centroid, area
	}

	holeArea := 0.0
	weightedHoleCentroid := orb.Point{}
	for i := 1; i < len(p); i++ {
		hc, ha := ringCentroidArea(p[i])
		ha = math.Abs(ha)

		holeArea += ha
		weightedHoleCentroid[0] += hc[0] * ha
		weightedHoleCentroid[1] += hc[1] * ha
	}

	totalArea := area - holeArea
	if totalArea == 0 {
		c, _ := lineStringCentroidDist(orb.LineString(p[0]))
		return c, 0
	}

	centroid[0] = (area*centroid[0] - weightedHoleCentroid[0]) / totalArea
	centroid[1] = (area*centroid[1] - weightedHoleCentroid[1]) / totalArea

	return centroid, totalArea
}

func multiPolygonCentroidArea(mp orb.MultiPolygon) (orb.Point, float64) {
	point := orb.Point{}
	area := 0.0

	for _, p := range mp {
		c, a := polygonCentroidArea(p)

		point[0] += c[0] * a
		point[1] += c[1] * a

		area += a
	}

	if area == 0 {
		return orb.Point{}, 0
	}

	point[0] /= area
	point[1] /= area

	return point, area
}

func collectionCentroidArea(c orb.Collection) (orb.Point, float64) {
	point := orb.Point{}
	area := 0.0

	max := maxDim(c)
	for _, g := range c {
		if g.Dimensions() != max {
			continue
		}

		c, a := CentroidArea(g)

		point[0] += c[0] * a
		point[1] += c[1] * a

		area += a
	}

	if area == 0 {
		return orb.Point{}, 0
	}

	point[0] /= area
	point[1] /= area

	return point, area
}

func maxDim(c orb.Collection) int {
	max := 0
	for _, g := range c {
		if d := g.Dimensions(); d > max {
			max = d
		}
	}

	return max
}