This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurement.go
164 lines (143 loc) · 5.03 KB
/
measurement.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
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
package turfgo
import (
"errors"
"fmt"
"math"
"github.com/hammerheadnav/turfgo/turfgoMath"
)
var unitError = "%s is not a valid unit. Allowed units are mi(miles), km(kilometers), d(degrees) and r(radians)"
// Along takes a line and returns a point at a specified distance along the line.
func Along(lineString *LineString, distance float64, unit string) (*Point, error) {
var travelled float64
points := lineString.getPoints()
for i, point := range points {
if distance >= travelled && i == len(points)-1 {
return point, nil
} else if travelled >= distance {
overshot := distance - travelled
if overshot == 0 {
return point, nil
}
direction := Bearing(points[i], points[i-1]) - 180
interpolated, err := Destination(points[i], overshot, direction, unit)
if err != nil {
return nil, err
}
return interpolated, nil
} else {
t, err := Distance(points[i], points[i+1], unit)
if err != nil {
return nil, err
}
travelled += t
}
}
return nil, nil
}
// Bearing takes two points and finds the geographic bearing between them.
func Bearing(point1, point2 *Point) float64 {
lat1 := turfgoMath.DegreeToRad(point1.Lat)
lat2 := turfgoMath.DegreeToRad(point2.Lat)
lon1 := turfgoMath.DegreeToRad(point1.Lng)
lon2 := turfgoMath.DegreeToRad(point2.Lng)
a := math.Sin(lon2-lon1) * math.Cos(lat2)
b := math.Cos(lat1)*math.Sin(lat2) -
math.Sin(lat1)*math.Cos(lat2)*math.Cos(lon2-lon1)
return turfgoMath.RadToDegree(math.Atan2(a, b))
}
// Center takes an array of points and returns the absolute center point of all points.
func Center(shapes ...Geometry) *Point {
bBox := Extent(shapes...)
lng := (bBox[0] + bBox[2]) / 2
lat := (bBox[1] + bBox[3]) / 2
return &Point{lat, lng}
}
// Destination takes a Point and calculates the location of a destination point
// given a distance in degrees, radians, miles, or kilometers; and bearing in
// degrees. This uses the Haversine formula to account for global curvature.
func Destination(startingPoint *Point, distance float64, bearing float64, unit string) (*Point, error) {
radius, ok := R[unit]
if !ok {
return nil, fmt.Errorf(unitError, unit)
}
lat := turfgoMath.DegreeToRad(startingPoint.Lat)
lon := turfgoMath.DegreeToRad(startingPoint.Lng)
bearingRad := turfgoMath.DegreeToRad(bearing)
destLat := math.Asin(math.Sin(lat)*math.Cos(distance/radius) +
math.Cos(lat)*math.Sin(distance/radius)*math.Cos(bearingRad))
destLon := lon + math.Atan2(math.Sin(bearingRad)*math.Sin(distance/radius)*math.Cos(lat),
math.Cos(distance/radius)-math.Sin(lat)*math.Sin(destLat))
return &Point{turfgoMath.RadToDegree(destLat), turfgoMath.RadToDegree(destLon)}, nil
}
// Distance calculates the distance between two points in degress, radians, miles, or
// kilometers. This uses the Haversine formula to account for global curvature.
func Distance(point1 *Point, point2 *Point, unit string) (float64, error) {
radius, ok := R[unit]
if !ok {
return 0, fmt.Errorf(unitError, unit)
}
dLat := turfgoMath.DegreeToRad(point2.Lat - point1.Lat)
dLon := turfgoMath.DegreeToRad(point2.Lng - point1.Lng)
latRad1 := turfgoMath.DegreeToRad(point1.Lat)
latRad2 := turfgoMath.DegreeToRad(point2.Lat)
a := math.Sin(dLat/2)*math.Sin(dLat/2) +
math.Sin(dLon/2)*math.Sin(dLon/2)*math.Cos(latRad1)*math.Cos(latRad2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return radius * c, nil
}
// Extent Takes a set of features, calculates the extent of all input features, and returns a bounding box.
// Returns []float64 the bounding box of input given as an array in WSEN order (west, south, east, north)
func Extent(shapes ...Geometry) []float64 {
extent := []float64{infinity, infinity, -infinity, -infinity}
for _, shape := range shapes {
for _, point := range shape.getPoints() {
if extent[0] > point.Lng {
extent[0] = point.Lng
}
if extent[1] > point.Lat {
extent[1] = point.Lat
}
if extent[2] < point.Lng {
extent[2] = point.Lng
}
if extent[3] < point.Lat {
extent[3] = point.Lat
}
}
}
return extent
}
// Overlap takes two bounding box and returns true if there is an overlap.
// The order of values in array is WSEN(west, south , east, north)
func Overlap(b1 []float64, b2 []float64) (bool, error) {
if len(b1) != 4 || len(b2) != 4 {
return false, errors.New("Invalid bbox")
}
w1, s1, e1, n1 := b1[0], b1[1], b1[2], b1[3]
w2, s2, e2, n2 := b2[0], b2[1], b2[2], b2[3]
// b2 is left of b1
if w1 > e2 {
return false, nil
}
// b2 is right of b1
if e1 < w2 {
return false, nil
}
// b2 is above b1
if n1 < s2 {
return false, nil
}
// b2 is below b1
if s1 > n2 {
return false, nil
}
return true, nil
}
// Surround Takes a point and a width, calculates the bounding box around the point with the given width.
// Returns []float64 the bounding box of input given as an array in WSEN order (west, south, east, north)
func Surround(point *Point, width float64) []float64 {
bottomLeft := translate(point, -width, -width)
topRight := translate(point, width, width)
bbox := []float64{bottomLeft.Lng, bottomLeft.Lat, topRight.Lng, topRight.Lat}
return bbox
}