The file parser.py
contains the ZTMReader
class which is used to read and parse the ZTM timetable data.
After reading (invoking the .read()
method) the class has the following important properties:
stops
- adict
ofdict
s describing stops, they keys are the 6-digit bus stop codes (stored asstr
, notint
). The keys and values of each innerdict
are:id: str
- the 6-digit bus stop codegroup_name: str
- the name of the group to which the bus stop belongs (these are the usual known bus stop names)street: str
- the name of the street on which the bus stop is located.lat: float
- the lattitude of the bus stop (in degrees north)long: float
- the longitude of the bus stop (in degrees east)lines: list
- a list of lines that stop at that stoptype: list
- a list of modes of transport that stop at the stop. Possible values are"bus"
,"tram"
,"train"
(metro is not present in the data)
edges
- alist
ofdict
s describing connections between bus stops. The keys and values of each dict are:route: str
- a unique identifier of the routefrom: str
- the id of the bus stop from which the edge startsto: str
- the id of the bus sto at which the edge endsstart_time: int
- the time of departure from the starting bus stop in minutes from midnightend_time: int
- the time of departure from the ending bus stop in minutes from midnighttime_between: int
- time required to travel this edge in minutestype: str
- route type, I don't know what this is but it was in the data.
Important: the edge between the same two bus stops appears multiple times in the edges
list - once for each bus that travels between two stops. You need to remove them before drawing the graph. The create_simple_edgelist()
method creates an edge list without duplicates.
from parser import ZTMReader
import networkx as nx
reader = ZTMReader("path/to/timetable/file.TXT")
reader.read()
edgelist = reader.create_simple_edgelist()
G = nx.DiGraph()
G.add_edges_from(edgelist)