locodedb

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 12 Imported by: 3

Documentation

Overview

Package locodedb implements a UN LOCODE database.

It contains all the data internally and provides simple Get API to retrieve records based on short LOCODE strings. The DB is stored compressed before the first use (~1MB) and is unpacked automatically on the first access (which takes ~100-200ms). Unpacked it needs ~4MB of RAM.

Index

Constants

View Source
const (
	// ContinentUnknown is an undefined Continent value.
	ContinentUnknown = iota

	// ContinentEurope corresponds to Europe.
	ContinentEurope

	// ContinentAfrica corresponds to Africa.
	ContinentAfrica

	// ContinentNorthAmerica corresponds to North America.
	ContinentNorthAmerica

	// ContinentSouthAmerica corresponds to South America.
	ContinentSouthAmerica

	// ContinentAsia corresponds to Asia.
	ContinentAsia

	// ContinentAntarctica corresponds to Antarctica.
	ContinentAntarctica

	// ContinentOceania corresponds to Oceania.
	ContinentOceania
)
View Source
const CountryCodeLen = 2

CountryCodeLen is the length of the country code.

View Source
const LocationCodeLen = 3

LocationCodeLen is the length of the location code.

Variables

View Source
var ErrInvalidString = errors.New("invalid string format in UN/Locode")

ErrInvalidString is returned when the string is not a valid location code.

View Source
var ErrNotFound = errors.New("record not found")

ErrNotFound is returned when the record is not found in the location database.

Functions

This section is empty.

Types

type Continent

type Continent uint8

Continent is an enumeration of Earth's continent.

func ContinentFromString

func ContinentFromString(str string) Continent

ContinentFromString returns Continent value corresponding to the passed string representation.

func (Continent) String

func (c Continent) String() string

String returns a string representation of the Continent. If the Continent is unknown, the string "Unknown" is returned.

type CountryCode

type CountryCode [CountryCodeLen]uint8

CountryCode represents ISO 3166 alpha-2 Country Code.

func CountryCodeFromString

func CountryCodeFromString(s string) (*CountryCode, error)

CountryCodeFromString parses a string and returns the country code.

func (*CountryCode) String

func (c *CountryCode) String() string

String returns a string representation of the country code.

func (*CountryCode) Symbols

func (c *CountryCode) Symbols() [CountryCodeLen]uint8

Symbols returns the country code as a slice of symbols.

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key represents the key in location database. It contains the country code and the location code.

func NewKey

func NewKey(country, location string) (*Key, error)

NewKey returns a new Key from a country code and a location code string pair (e.g. "USNYC") or an error if the string is invalid. The country code must be 2 letters long and the location code 3 letters long.

func (*Key) CountryCode

func (k *Key) CountryCode() *CountryCode

CountryCode returns the location's country code.

func (*Key) LocationCode

func (k *Key) LocationCode() *LocationCode

LocationCode returns the location code.

type LocationCode

type LocationCode [LocationCodeLen]uint8

LocationCode represents a location code for the storage in the location database.

func LocationCodeFromString

func LocationCodeFromString(s string) (*LocationCode, error)

LocationCodeFromString parses a string and returns the location code.

func (*LocationCode) String

func (l *LocationCode) String() string

String returns a string representation of the location code.

func (*LocationCode) Symbols

func (l *LocationCode) Symbols() [LocationCodeLen]uint8

Symbols returns the location code as a slice of symbols.

type Point

type Point struct {
	Latitude  float32
	Longitude float32
}

Point represents a 2D geographic point.

type Record

type Record struct {
	Country    string
	Location   string
	SubDivName string
	SubDivCode string
	Point      Point
	Cont       Continent
}

Record represents a record in the location database (resulting CSV files). It contains all the information about the location. It is used to fill the database. Country, Location are full names, codes are in Key.

func Get

func Get(locodeStr string) (Record, error)

Get returns a record for a given locode string. The string must be 5 or 6 letters long. The first 2 letters are country code followed by an optional space separator and 3 letters of the location code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL