-
Notifications
You must be signed in to change notification settings - Fork 0
/
latlon.go
180 lines (157 loc) · 4.98 KB
/
latlon.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package geod
// Pure Go re-implementation of https://github.com/chrisveness/geodesy
/**
* Copyright (c) 2020, Xerra Earth Observation Institute
* All rights reserved. Use is subject to License terms.
* See LICENSE in the root directory of this source tree.
*/
import (
"fmt"
"math"
"strings"
)
// Degrees angle
// Defining it as a type makes it harder to mix Degrees and Radians in your code, you're welcome :)
type Degrees float64
// Valid returns true if the angle is valid. Invalid angles are returned by
// functions when the result cannot be calculated.
func (d Degrees) Valid() bool {
return !math.IsNaN(float64(d))
}
// Radians takes an argument in degrees and returns it in radians
func (d Degrees) Radians() float64 {
return float64(d) * math.Pi / 180.0
}
// RoundTo returns the degrees as a float rounded to `n` decimal points.
func (d Degrees) RoundTo(n int) float64 {
p10 := math.Pow10(n)
return math.Round(p10*float64(d)) / p10
}
// DegreesFromRadians takes an argument in radians and returns it in degrees
func DegreesFromRadians(radians float64) Degrees {
return Degrees(radians * 180.0 / math.Pi)
}
// LatLon represents a point on Earth defined by its Latitude and Longitude
type LatLon struct {
Latitude Degrees
Longitude Degrees
}
// NewLatLon returns a `LatLon` structure with the given latitude and longitude
func NewLatLon(latitude, longitude float64) LatLon {
return LatLon{Latitude: Degrees(latitude), Longitude: Degrees(longitude)}
}
// Valid returns true if the coordinates are valid. Invalid coordinates are returned by
// functions when the result cannot be calculated.
func (ll LatLon) Valid() bool {
if math.IsNaN(float64(ll.Latitude)) || math.IsNaN(float64(ll.Longitude)) {
return false
}
return true
}
// Equals returns true if `ll` and `other` have identical Latitude and Longitude values
func (ll LatLon) Equals(other LatLon) bool {
epsilon := math.Nextafter(1, 2) - 1
if math.Abs(float64(ll.Latitude)-float64(other.Latitude)) > epsilon {
return false
}
if math.Abs(float64(ll.Longitude)-float64(other.Longitude)) > epsilon {
return false
}
return true
}
// ParseLatLon parses a latitude/longitude point from a variety of formats.
//
// Latitude & longitude (in degrees) can be supplied as two separate string parameters or
// as a single comma-separated lat/lon string
//
// The latitude/longitude values may be signed decimal or deg-min-sec (hexagesimal) suffixed by compass direction (NSEW)
// a variety of separators are accepted. Examples: -3.62, '3 37 12W', '3°37′12″W'.
//
// Thousands/decimal separators must be comma/dot
//
// Arguments:
// lat|latlon - Latitude (in degrees), or comma-separated lat/lon
// [lon] - Longitude (in degrees).
//
// Returns Latitude/longitude point on WGS84 (LatLon)
//
// Example:
// p1 := ParseLatLon(51.47788, -0.00147) // numeric pair
// p2 := ParseLatLon("51.47788", "-0.00147") // string pair
// p3 := ParseLatLon("51°28′40″N, 000°00′05″W") // single dms string
// p4 := ParseLatLon("51°28′40″N", "000°00′05″W") // dms lat string, dms lon string
func ParseLatLon(args ...interface{}) (LatLon, error) {
if len(args) == 0 {
return LatLon{}, fmt.Errorf("Invalid (empty) point")
}
// split the arguments into lat, lon
var args2 []interface{}
if len(args) == 1 {
// single string of "lat, lon"
s, ok := args[0].(string)
if !ok {
return LatLon{}, fmt.Errorf("Invalid argument type: %T", args[0])
}
tokens := strings.Split(s, ",")
if len(tokens) > 2 {
return LatLon{}, fmt.Errorf("Failed to parse argument: too many items")
}
if len(tokens) == 1 {
return LatLon{}, fmt.Errorf("Failed to parse argument: latitude and longitude are required")
}
args2 = []interface{}{tokens[0], tokens[1]}
} else if len(args) == 2 {
args2 = args
} else {
return LatLon{}, fmt.Errorf("Too many arguments")
}
var lat, lon Degrees
// we now have 2 values in args2: lat, lon
switch v := args2[0].(type) {
case string:
lat0, err := ParseDMS(v)
if err != nil {
return LatLon{}, fmt.Errorf("Failed to parse latitude: %v", err)
}
lat = lat0
case float64:
if math.IsNaN(v) {
return LatLon{}, fmt.Errorf("Latitude cannot be NaN")
}
lat = Degrees(v)
case float32:
if math.IsNaN(float64(v)) {
return LatLon{}, fmt.Errorf("Latitude cannot be NaN")
}
lat = Degrees(v)
case Degrees:
lat = v
default:
return LatLon{}, fmt.Errorf("Invalid type for latitude: %T", v)
}
lat = Wrap90(lat)
switch v := args2[1].(type) {
case string:
lon0, err := ParseDMS(v)
if err != nil {
return LatLon{}, fmt.Errorf("Failed to parse longitude: %v", err)
}
lon = lon0
case float64:
if math.IsNaN(v) {
return LatLon{}, fmt.Errorf("Longitude cannot be NaN")
}
lon = Degrees(v)
case float32:
if math.IsNaN(float64(v)) {
return LatLon{}, fmt.Errorf("Longitude cannot be NaN")
}
lon = Degrees(v)
case Degrees:
lon = v
default:
return LatLon{}, fmt.Errorf("Invalid type for longitude: %T", v)
}
lon = Wrap180(lon)
return LatLon{Latitude: lat, Longitude: lon}, nil
}