-
Notifications
You must be signed in to change notification settings - Fork 38
/
point.go
75 lines (61 loc) · 1.29 KB
/
point.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
package locodedb
import (
"strconv"
"strings"
locodecolumn "github.com/nspcc-dev/neofs-node/pkg/util/locode/column"
)
// Point represents 2D geographic point.
type Point struct {
lat, lng float64
}
// NewPoint creates, initializes and returns new Point.
func NewPoint(lat, lng float64) *Point {
return &Point{
lat: lat,
lng: lng,
}
}
// Latitude returns Point's latitude.
func (p Point) Latitude() float64 {
return p.lat
}
// Longitude returns Point's longitude.
func (p Point) Longitude() float64 {
return p.lng
}
// PointFromCoordinates converts UN/LOCODE coordinates to Point.
func PointFromCoordinates(crd *locodecolumn.Coordinates) (*Point, error) {
if crd == nil {
return nil, nil
}
cLat := crd.Latitude()
cLatDeg := cLat.Degrees()
cLatMnt := cLat.Minutes()
lat, err := strconv.ParseFloat(strings.Join([]string{
string(cLatDeg[:]),
string(cLatMnt[:]),
}, "."), 64)
if err != nil {
return nil, err
}
if !cLat.Hemisphere().North() {
lat = -lat
}
cLng := crd.Longitude()
cLngDeg := cLng.Degrees()
cLngMnt := cLng.Minutes()
lng, err := strconv.ParseFloat(strings.Join([]string{
string(cLngDeg[:]),
string(cLngMnt[:]),
}, "."), 64)
if err != nil {
return nil, err
}
if !cLng.Hemisphere().East() {
lng = -lng
}
return &Point{
lat: lat,
lng: lng,
}, nil
}