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
|
package length
import (
"fmt"
"github.com/paulmach/orb"
)
// Length returns the length of the boundary of the geometry
// using 2d euclidean geometry.
func Length(g orb.Geometry, df orb.DistanceFunc) float64 {
if g == nil {
return 0
}
switch g := g.(type) {
case orb.Point:
return 0
case orb.MultiPoint:
return 0
case orb.LineString:
return lineStringLength(g, df)
case orb.MultiLineString:
sum := 0.0
for _, ls := range g {
sum += lineStringLength(ls, df)
}
return sum
case orb.Ring:
return lineStringLength(orb.LineString(g), df)
case orb.Polygon:
return polygonLength(g, df)
case orb.MultiPolygon:
sum := 0.0
for _, p := range g {
sum += polygonLength(p, df)
}
return sum
case orb.Collection:
sum := 0.0
for _, c := range g {
sum += Length(c, df)
}
return sum
case orb.Bound:
return Length(g.ToRing(), df)
}
panic(fmt.Sprintf("geometry type not supported: %T", g))
}
func lineStringLength(ls orb.LineString, df orb.DistanceFunc) float64 {
sum := 0.0
for i := 1; i < len(ls); i++ {
sum += df(ls[i], ls[i-1])
}
return sum
}
func polygonLength(p orb.Polygon, df orb.DistanceFunc) float64 {
sum := 0.0
for _, r := range p {
sum += lineStringLength(orb.LineString(r), df)
}
return sum
}
|