2021-02-08 17:53:34 +00:00
|
|
|
package airportsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-08 17:53:34 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode"
|
|
|
|
locodedb "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode/db"
|
2021-02-08 17:53:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota - 1
|
|
|
|
|
|
|
|
_ // Airport ID
|
|
|
|
_ // Name
|
|
|
|
airportCity
|
|
|
|
airportCountry
|
|
|
|
airportIATA
|
|
|
|
_ // ICAO
|
|
|
|
airportLatitude
|
|
|
|
airportLongitude
|
|
|
|
_ // Altitude
|
|
|
|
_ // Timezone
|
|
|
|
_ // DST
|
|
|
|
_ // Tz database time zone
|
|
|
|
_ // Type
|
|
|
|
_ // Source
|
|
|
|
|
|
|
|
airportFldNum
|
|
|
|
)
|
|
|
|
|
|
|
|
type record struct {
|
|
|
|
city,
|
|
|
|
country,
|
|
|
|
iata,
|
|
|
|
lat,
|
|
|
|
lng string
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Get scans the records of the OpenFlights Airport to an in-memory table (once),
|
|
|
|
// and returns an entry that matches the passed UN/LOCODE record.
|
2021-02-08 17:53:34 +00:00
|
|
|
//
|
|
|
|
// Records are matched if they have the same country code and either
|
|
|
|
// same IATA code or same city name (location name in UN/LOCODE).
|
|
|
|
//
|
|
|
|
// Returns locodedb.ErrAirportNotFound if no entry matches.
|
2021-02-10 16:52:02 +00:00
|
|
|
func (db *DB) Get(locodeRecord locode.Record) (*locodedb.AirportRecord, error) {
|
|
|
|
if err := db.initAirports(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 17:53:34 +00:00
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
records := db.mAirports[locodeRecord.LOCODE.CountryCode()]
|
|
|
|
|
|
|
|
for i := range records {
|
|
|
|
if locodeRecord.LOCODE.LocationCode() != records[i].iata &&
|
|
|
|
locodeRecord.NameWoDiacritics != records[i].city {
|
|
|
|
continue
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
lat, err := strconv.ParseFloat(records[i].lat, 64)
|
2021-02-08 17:53:34 +00:00
|
|
|
if err != nil {
|
2021-02-10 16:52:02 +00:00
|
|
|
return nil, err
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
lng, err := strconv.ParseFloat(records[i].lng, 64)
|
2021-02-08 17:53:34 +00:00
|
|
|
if err != nil {
|
2021-02-10 16:52:02 +00:00
|
|
|
return nil, err
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
return &locodedb.AirportRecord{
|
|
|
|
CountryName: records[i].country,
|
2021-02-08 17:53:34 +00:00
|
|
|
Point: locodedb.NewPoint(lat, lng),
|
2021-02-10 16:52:02 +00:00
|
|
|
}, nil
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
return nil, locodedb.ErrAirportNotFound
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota - 1
|
|
|
|
|
|
|
|
countryName
|
|
|
|
countryISOCode
|
|
|
|
_ // dafif_code
|
|
|
|
|
|
|
|
countryFldNum
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CountryName scans the records of the OpenFlights Country table to an in-memory table (once),
|
|
|
|
// and returns the name of the country by code.
|
2021-02-08 17:53:34 +00:00
|
|
|
//
|
|
|
|
// Returns locodedb.ErrCountryNotFound if no entry matches.
|
|
|
|
func (db *DB) CountryName(code *locodedb.CountryCode) (name string, err error) {
|
2021-02-10 16:52:02 +00:00
|
|
|
if err = db.initCountries(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-08 17:53:34 +00:00
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
argCode := code.String()
|
2021-02-08 17:53:34 +00:00
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
for cName, cCode := range db.mCountries {
|
|
|
|
if cCode == argCode {
|
|
|
|
name = cName
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
2021-02-08 17:53:34 +00:00
|
|
|
err = locodedb.ErrCountryNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
func (db *DB) initAirports() (err error) {
|
|
|
|
db.airportsOnce.Do(func() {
|
|
|
|
db.mAirports = make(map[string][]record)
|
|
|
|
|
|
|
|
if err = db.initCountries(); err != nil {
|
|
|
|
return
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:52:02 +00:00
|
|
|
err = db.scanWords(db.airports, airportFldNum, func(words []string) error {
|
|
|
|
countryCode := db.mCountries[words[airportCountry]]
|
|
|
|
if countryCode != "" {
|
|
|
|
db.mAirports[countryCode] = append(db.mAirports[countryCode], record{
|
|
|
|
city: words[airportCity],
|
|
|
|
country: words[airportCountry],
|
|
|
|
iata: words[airportIATA],
|
|
|
|
lat: words[airportLatitude],
|
|
|
|
lng: words[airportLongitude],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) initCountries() (err error) {
|
|
|
|
db.countriesOnce.Do(func() {
|
|
|
|
db.mCountries = make(map[string]string)
|
|
|
|
|
|
|
|
err = db.scanWords(db.countries, countryFldNum, func(words []string) error {
|
|
|
|
db.mCountries[words[countryName]] = words[countryISOCode]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2021-02-08 17:53:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var errScanInt = errors.New("interrupt scan")
|
|
|
|
|
|
|
|
func (db *DB) scanWords(pm pathMode, num int, wordsHandler func([]string) error) error {
|
|
|
|
tableFile, err := os.OpenFile(pm.path, os.O_RDONLY, pm.mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer tableFile.Close()
|
|
|
|
|
|
|
|
r := csv.NewReader(tableFile)
|
|
|
|
r.ReuseRecord = true
|
|
|
|
|
|
|
|
for {
|
|
|
|
words, err := r.Read()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
} else if ln := len(words); ln != num {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("unexpected number of words %d", ln)
|
2021-02-08 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := wordsHandler(words); err != nil {
|
|
|
|
if errors.Is(err, errScanInt) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|