2021-02-08 17:27:19 +00:00
|
|
|
package locodedb
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-02-08 17:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/locode"
|
|
|
|
locodecolumn "github.com/nspcc-dev/neofs-node/pkg/util/locode/column"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key represents the key in NeoFS location database.
|
|
|
|
type Key struct {
|
|
|
|
cc *CountryCode
|
|
|
|
|
|
|
|
lc *LocationCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKey calculates Key from LOCODE.
|
|
|
|
func NewKey(lc locode.LOCODE) (*Key, error) {
|
|
|
|
country, err := CountryCodeFromString(lc.CountryCode())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse country: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location, err := LocationCodeFromString(lc.LocationCode())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse location: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Key{
|
|
|
|
cc: country,
|
|
|
|
lc: location,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountryCode returns location's country code.
|
|
|
|
func (k *Key) CountryCode() *CountryCode {
|
|
|
|
return k.cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocationCode returns location code.
|
|
|
|
func (k *Key) LocationCode() *LocationCode {
|
|
|
|
return k.lc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record represents the entry in NeoFS location database.
|
|
|
|
type Record struct {
|
|
|
|
countryName string
|
|
|
|
|
2021-02-10 10:20:39 +00:00
|
|
|
locationName string
|
2021-02-08 17:27:19 +00:00
|
|
|
|
|
|
|
subDivName string
|
|
|
|
|
|
|
|
subDivCode string
|
|
|
|
|
|
|
|
p *Point
|
|
|
|
|
|
|
|
cont *Continent
|
|
|
|
}
|
|
|
|
|
|
|
|
var errParseCoordinates = errors.New("invalid coordinates")
|
|
|
|
|
|
|
|
// NewRecord calculates Record from UN/LOCODE table record.
|
|
|
|
func NewRecord(r locode.Record) (*Record, error) {
|
|
|
|
crd, err := locodecolumn.CoordinatesFromString(r.Coordinates)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("%w: %v", errParseCoordinates, err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
point, err := PointFromCoordinates(crd)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse geo point: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Record{
|
2021-02-10 10:20:39 +00:00
|
|
|
locationName: r.NameWoDiacritics,
|
|
|
|
subDivCode: r.SubDiv,
|
|
|
|
p: point,
|
2021-02-08 17:27:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountryName returns country name.
|
|
|
|
func (r *Record) CountryName() string {
|
|
|
|
return r.countryName
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCountryName sets country name.
|
|
|
|
func (r *Record) SetCountryName(name string) {
|
|
|
|
r.countryName = name
|
|
|
|
}
|
|
|
|
|
2021-02-10 10:20:39 +00:00
|
|
|
// LocationName returns location name.
|
|
|
|
func (r *Record) LocationName() string {
|
|
|
|
return r.locationName
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 10:20:39 +00:00
|
|
|
// SetLocationName sets location name.
|
|
|
|
func (r *Record) SetLocationName(name string) {
|
|
|
|
r.locationName = name
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubDivCode returns subdivision code.
|
|
|
|
func (r *Record) SubDivCode() string {
|
|
|
|
return r.subDivCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSubDivCode sets subdivision code.
|
|
|
|
func (r *Record) SetSubDivCode(name string) {
|
|
|
|
r.subDivCode = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubDivName returns subdivision name.
|
|
|
|
func (r *Record) SubDivName() string {
|
|
|
|
return r.subDivName
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSubDivName sets subdivision name.
|
|
|
|
func (r *Record) SetSubDivName(name string) {
|
|
|
|
r.subDivName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeoPoint returns geo point of the location.
|
|
|
|
func (r *Record) GeoPoint() *Point {
|
|
|
|
return r.p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGeoPoint sets geo point of the location.
|
|
|
|
func (r *Record) SetGeoPoint(p *Point) {
|
|
|
|
r.p = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continent returns location continent.
|
|
|
|
func (r *Record) Continent() *Continent {
|
|
|
|
return r.cont
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContinent sets location continent.
|
|
|
|
func (r *Record) SetContinent(c *Continent) {
|
|
|
|
r.cont = c
|
|
|
|
}
|