Skip to main content

Python library to manipulate OpenTravelData

Project description

Minimalist GeoBases implementation:

  • no dependencies

  • compatible with Python 3.9+, CPython and PyPy

  • one data source: opentraveldata

  • one Python module for easier distribution on clusters (like Hadoop)

  • faster load time (5x)

  • tested with pytest and tox

>>> from neobase import NeoBase
>>> b = NeoBase()
>>> b.get('ORY', 'city_code_list')
['PAR']
>>> b.get('ORY', 'city_name_list')
['Paris']
>>> b.get('ORY', 'country_code')
'FR'
>>> b.distance('ORY', 'CDG')
34.87...
>>> b.get_location('ORY')
LatLng(lat=48.72..., lng=2.35...)

Installation

Use the Python package:

pip install neobase

Docs

Check out readthedocs for the API.

You can customize the source data when initializing:

with open("file.csv") as f:
    N = NeoBase(f)

Otherwise the loaded file will be the embedded one, unless the OPTD_POR_FILE environment variable is set. In that case, it will load from the path defined in that variable.

You can manually retrieve the latest data source yourself too, but you expose yourself to some breaking changes if they occur in the data.

from io import StringIO
from urllib.request import urlopen

from neobase import NeoBase, OPTD_POR_URL

data = urlopen(OPTD_POR_URL).read().decode('utf8')
N = NeoBase(StringIO(data))
N.get("PAR")

The reference date of validity can be changed as well:

N = NeoBase(date="2000-01-01")
N.get("AIY")  # was decommissioned in 2015

By default, the reference date will be set to today, unless the OPTD_POR_DATE environment variable is set. In that case, it will use that value.

You can customize the behavior regarding duplicates: points sharing the same IATA code, like NCE as airport and NCE as city. By default everything is kept, but you can set it so that only the first point with an IATA code is kept:

N = NeoBase(duplicates=False)
len(N)  # about 10,000 "only"

Note that you can use the OPTD_POR_DUPLICATES environment variable to control this as well: set it to 0 to drop duplicates.

Finally, you can customize fields loaded by subclassing.

class SubNeoBase(NeoBase):
    KEY = 0  # iata_code

    # Those loaded fields are the default ones
    FIELDS = (
        ("name", 6, None),
        ("lat", 8, None),
        ("lng", 9, None),
        ("page_rank", 12, lambda s: float(s) if s else None),
        ("country_code", 16, None),
        ("country_name", 18, None),
        ('continent_name', 19, None),
        ("timezone", 31, None),
        ("city_code_list", 36, lambda s: s.split(",")),
        ('city_name_list', 37, lambda s: s.split('=')),
        ('location_type', 41, None),
        ("currency", 46, None),
    )

N = SubNeoBase()

Command-line interface

You can query the data using:

python -m neobase PAR NCE

Tests

tox

A note about performance

The geographical operations like N.find_near("ORY", 100) or N.find_closest_from("ORY") perform a full scan of the data, and are not optimized (remember that this library has no dependencies, by design).

If you want a more efficient solution, you should use a spatial index like a BallTree, for example using scikit-learn:

import numpy as np
from sklearn.neighbors import BallTree
from neobase import NeoBase

N = NeoBase()

iata_codes = []
coords = []
for key in N:
    lat, lon = N.get_location(key)
    if lat is not None and lon is not None:
        iata_codes.append(N.get(key, "iata_code"))
        coords.append([np.radians(lat), np.radians(lon)])
coords = np.array(coords)

tree = BallTree(coords, metric="haversine")

def find_closest_with_balltree(coord):
    point = np.radians(coord)
    _, idx = tree.query([point], k=1)
    iata_code = iata_codes[idx[0][0]]
    return iata_code

paris = (48.8566, 2.3522)
print(find_closest_with_balltree(paris))  # <0.1ms
print(list(N.find_closest_from_location(paris)))  # ~30ms

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neobase-0.34.23.tar.gz (4.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neobase-0.34.23-py3-none-any.whl (4.2 MB view details)

Uploaded Python 3

File details

Details for the file neobase-0.34.23.tar.gz.

File metadata

  • Download URL: neobase-0.34.23.tar.gz
  • Upload date:
  • Size: 4.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for neobase-0.34.23.tar.gz
Algorithm Hash digest
SHA256 d27cbf3879837e73f027a50fa408c299d0752e59fd80849378e864f63aa7110d
MD5 ffddd6c78400d966f6d0bb00767acac9
BLAKE2b-256 f9d833ed4b80c729e3e86c9e6870fcadb51ba9225181364e36644ba0388613e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for neobase-0.34.23.tar.gz:

Publisher: python-package.yml on alexprengere/neobase

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file neobase-0.34.23-py3-none-any.whl.

File metadata

  • Download URL: neobase-0.34.23-py3-none-any.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for neobase-0.34.23-py3-none-any.whl
Algorithm Hash digest
SHA256 2d1ef8b7b9ccc2714c7b053d9e2d25e3c9df9d96bf577a460a34e0057d77c1c1
MD5 c6275cd083eda8b2c2863c71ea002b4b
BLAKE2b-256 307db3fb3f4f89d5998af4d5cc8b1c2dadffdc4f37a53cb21e7ddd349506b803

See more details on using hashes here.

Provenance

The following attestation bundles were made for neobase-0.34.23-py3-none-any.whl:

Publisher: python-package.yml on alexprengere/neobase

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page