2021-02-08 17:27:19 +00:00
|
|
|
package locodedb
|
|
|
|
|
|
|
|
import (
|
2021-10-08 16:55:40 +00:00
|
|
|
"fmt"
|
2021-02-08 17:27:19 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
locodecolumn "github.com/nspcc-dev/neofs-node/pkg/util/locode/column"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Point represents a 2D geographic point.
|
2021-02-08 17:27:19 +00:00
|
|
|
type Point struct {
|
|
|
|
lat, lng float64
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// NewPoint creates, initializes and returns a new Point.
|
2021-02-08 17:27:19 +00:00
|
|
|
func NewPoint(lat, lng float64) *Point {
|
|
|
|
return &Point{
|
|
|
|
lat: lat,
|
|
|
|
lng: lng,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Latitude returns the Point's latitude.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (p Point) Latitude() float64 {
|
|
|
|
return p.lat
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Longitude returns the Point's longitude.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (p Point) Longitude() float64 {
|
|
|
|
return p.lng
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PointFromCoordinates converts a UN/LOCODE coordinates to a Point.
|
2021-02-08 17:27:19 +00:00
|
|
|
func PointFromCoordinates(crd *locodecolumn.Coordinates) (*Point, error) {
|
|
|
|
if crd == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cLat := crd.Latitude()
|
|
|
|
cLatDeg := cLat.Degrees()
|
|
|
|
cLatMnt := cLat.Minutes()
|
|
|
|
|
2021-10-08 16:55:40 +00:00
|
|
|
lat, err := toDecimal(cLatDeg[:], cLatMnt[:])
|
2021-02-08 17:27:19 +00:00
|
|
|
if err != nil {
|
2021-10-08 16:55:40 +00:00
|
|
|
return nil, fmt.Errorf("could not parse latitude: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cLat.Hemisphere().North() {
|
|
|
|
lat = -lat
|
|
|
|
}
|
|
|
|
|
|
|
|
cLng := crd.Longitude()
|
|
|
|
cLngDeg := cLng.Degrees()
|
|
|
|
cLngMnt := cLng.Minutes()
|
|
|
|
|
2021-10-08 16:55:40 +00:00
|
|
|
lng, err := toDecimal(cLngDeg[:], cLngMnt[:])
|
2021-02-08 17:27:19 +00:00
|
|
|
if err != nil {
|
2021-10-08 16:55:40 +00:00
|
|
|
return nil, fmt.Errorf("could not parse longitude: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !cLng.Hemisphere().East() {
|
|
|
|
lng = -lng
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Point{
|
|
|
|
lat: lat,
|
|
|
|
lng: lng,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-10-08 16:55:40 +00:00
|
|
|
|
|
|
|
func toDecimal(intRaw, minutesRaw []byte) (float64, error) {
|
|
|
|
integer, err := strconv.ParseFloat(string(intRaw), 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not parse integer part: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
decimal, err := minutesToDegrees(minutesRaw)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not parse decimal part: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return integer + decimal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// minutesToDegrees converts minutes to decimal part of a degree
|
|
|
|
func minutesToDegrees(raw []byte) (float64, error) {
|
|
|
|
minutes, err := strconv.ParseFloat(string(raw), 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return minutes / 60, nil
|
|
|
|
}
|