[#316] locode: Define the API of location database
Define structure of keys and records of the location database. Define the interfaces of all components necessary for the formation of the database. Implement the function of filling the database. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
0be35859ed
commit
cdd1274e1c
6 changed files with 523 additions and 0 deletions
75
pkg/util/locode/db/point.go
Normal file
75
pkg/util/locode/db/point.go
Normal file
|
@ -0,0 +1,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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue