api_database package
Submodules
api_database.fetch_db_file module
- api_database.fetch_db_file.fetch_file(url: str, file: str)
Fetches stops data from url and writes it to compressed csv file.
- Args:
url (str): url to fetch data from file (str): path of file to write to
api_database.fetch_operators module
- api_database.fetch_operators.fetch_operators_data(conn: Connection) str
Fetches the contents of the operators database in JSON format from the sqlite3 database
- Args:
conn (sqlite3.Connection): Connection to the sqlite3 database
Returns: Data in JSON format
- api_database.fetch_operators.operators_info(conn: Connection) list
Fetches a list of the fields in the operators database
- Args:
conn (sqlite3.Connection): Connection to the sqlite3 database
Returns: List of columns in the operators database
api_database.fetch_stops module
- api_database.fetch_stops.get_stops(conn: Connection, min_lat: float, min_long: float, max_lat: float, max_long: float, **kwargs)
Return all stops in the requested area
- Args:
conn (sqlite3.Connection): Connection to the database min_lat (float): Minimum latitude min_long (float): Minimum longitude max_lat (float): Maximum latitude max_long (float): Maximum longitude **kwargs ():
Returns: Stop data in JSON format
- api_database.fetch_stops.get_stops_code(conn: Connection, codes: list[str])
Return all stops in the requested area
- Args:
conn (sqlite3.Connection): Connection to the database codes (list[str]): List of unique stop codes
Returns: Stop data in JSON format
api_database.initialise_database module
- api_database.initialise_database.setup_database(path: str, reinitialise: bool = False, **kwargs) Connection
Sets up connection to the database, if it exists. Else the database is initialised and the connection created
- Args:
path (str): The (relative) path to the database file. reinitialise (bool, optional): Whether to reinitialise the database regardless of whether it already exists. Defaults to False.
- Returns:
sqlite3.Connection: Connection to the database
api_database.operators_database_setup module
- api_database.operators_database_setup.get_data(tree: Element) DataFrame
Converts the data tree element into a dataframe
- Args:
tree (ElementTree.Element): A element in the element tree
Returns: The data from the data tree as a dataframe
- api_database.operators_database_setup.get_record(tree: Element) dict
Gets the data from a record in the database
- Args:
tree (ElementTree.Element): A element in the element tree
Returns: Dictionary of all data from the record
- api_database.operators_database_setup.initialise_operator_db(conn: Connection, filepath: str, encoding: str)
Initialises the database by downloading the data from the specified url
- Args:
conn (sqlite3.Connection): Connection to the database filepath (str): Path to the operator data file encoding (str): Expected encoding of the data
- api_database.operators_database_setup.read_file(filepath: str) bytes
- api_database.operators_database_setup.setup_operator_database(conn: Connection, operator_filepath: str = 'nocrecords.xml.gz', operator_url: str = 'https://www.travelinedata.org.uk/noc/api/1.0/nocrecords.xml', operator_encoding: str = 'windows-1252', **kwargs)
Sets up the operator part of the database at connection using the provided URL and encoding.
- Args:
conn (sqlite3.Connection): Connection to the database operator_filepath (str): Path to the operator file. Defaults to “nocrecords.xml.gz” operator_url (str, optional): URL for operator XML data. Defaults to “https://www.travelinedata.org.uk/noc/api/1.0/nocrecords.xml”. operator_encoding (str, optional): Encoding for the XML file. Defaults to “windows-1252”.
- api_database.operators_database_setup.setup_table(tree: Element, conn: Connection)
Sets up a table in the database
- Args:
tree (ElementTree.Element): Element tree containing the table data conn (sqlite3.Connection): Connection to the database
api_database.stop_database_setup module
- api_database.stop_database_setup.convert_bng(easting, northing) tuple[float, float]
Converts the BNG format to Longitude/Latitude
- Args:
easting (int): Easting northing (int): Northing
Returns: Longitude and Latitude as a pair
- api_database.stop_database_setup.get_stop_data(stop_file: str, stop_encoding: str, **kwargs) DataFrame
- api_database.stop_database_setup.setup_stop_database(conn: Connection, stop_file: str = 'Stops.csv.gz', stop_url: str = 'https://naptan.api.dft.gov.uk/v1/access-nodes?dataFormat=csv', stop_encoding: str = 'utf8', **kwargs)
Setups up the Stop database using the local file stop_file, if it exists. Else the online version at stop_url is downloaded and saved to stop_file. The stop_file file can be either a CSV file or a GZIP file containing the CSV data.
- Args:
conn (sqlite3.Connection): Connection to the database stop_file (str, optional): Path to the stops file. Defaults to “Stops.csv.gz”. stop_url (str, optional): URL for the Stop data in CSV format. Defaults to “”. stop_encoding (str, optional): Encoding of the CSV file. Defaults to “UTF-8”.