def read_coordinates():
    try:
        with open("sgtfs/coordinates.txt", 'r', encoding="utf-8") as file:
            lines = file.readlines()

        stop_counter = 0
        coordinates_counter = 0

        list_of_coordinates = list()
        for i, line in enumerate(lines):
            if i == 0:
                if ord(line[0]) > 1000:
                    line = line[1:]
            stop_counter += 1

            # Here we parse the line
            parsed_line = Other.split_by(line, "\t")

            # We add it to the list if it contains coordinates
            if len(parsed_line) > 1:

                # Parsing the coordinates
                coordinates = re.split(", ", parsed_line[1])

                list_of_coordinates.append([parsed_line[0], coordinates[0], coordinates[1]])
                coordinates_counter += 1

        print(str(stop_counter) + " stops were in the file")
        print(str(coordinates_counter) + " coordinates were in the file")
        return list_of_coordinates

    except FileNotFoundError:
        print("There is no sgtfs/coordinates.txt file")
    def __init__(self):

        self.list_differentiation = list()
        self.list_of_list_of_identical_stops = list()
        self.threshold = None

        try:
            with io.open("sgtfs/same_stops.txt", "r", encoding="utf-8") as f:
                equalities = f.readlines()
        except FileNotFoundError:
            print("same_stops.txt not found")
            equalities = list()
        try:
            with io.open("sgtfs/different_stops.txt", "r", encoding="utf-8") as f:
                differences = f.readlines()
        except FileNotFoundError:
            print("different_stops.txt not found")
            differences = list()

        for line in equalities:
            list_of_same_names = Other.split_by(line, "=")
            self.list_of_list_of_identical_stops.append(list_of_same_names)

        for line in differences:
            different_stops = Other.split_by(line, "!")
            self.list_differentiation.append(different_stops)

        try:
            with io.open("threshold.txt", encoding="utf-8") as f:
                line = f.readline()
                threshold = re.sub("\\n", "", line)
                threshold = threshold[1:]
        except FileNotFoundError:
            print("threshold.txt not found")
            threshold = "0.6"

        self.threshold = float(threshold)
    def init_from_line(self, line):
        # Initialise values from the line

        splited_line = Other.split_by(line, "\t")

        # We get the name
        self.name = splited_line[0]

        # Here we get the directions the bus can go
        # No number means both directions are possible
        try:
            if splited_line[1] == '1':
                self.link_up = False
            elif splited_line[1] == '2':
                self.link_down = False
        except IndexError:
            pass
    def add_timetable(self, file_path="sgtfs/timetable.txt"):
        # This function takes a timetable and convert it to a list of trips

        try:
            with io.open(file_path, encoding="utf-8") as f:
                lines = f.readlines()
        except FileNotFoundError:
            print("No timetable file")
            return None

        for line in lines:
            line.strip()

        # Here we get the name of the line and the service
        metadata = Other.split_by(lines[0], "\t")
        route_name = metadata[0]
        trips_service = metadata[1]


        try:
            empty_time = metadata[2]
        except IndexError:
            empty_time = "-"
        try:
            separator = metadata[3]
        except IndexError:
            separator = ":"

        lines.pop(0)
        list_stops_names = list()
        list_times = list()
        for line in lines:
            name, times_list = Agency.get_list_of_times_and_stop_name(line, separator, empty_time)
            list_stops_names.append(name)
            list_times.append(times_list)

        # Here we perform a check to ensure that

        # Right now, we have a table of horizontal lines.
        # We have to get vertical lines instead.

        count = 0

        # The if is here in case there is actually no times in the timetable.txt
        if len(list_times) > 0:

            # It's time for a little check.
            # It would be inconvenient that a bug allows to write anything in the files
            lenght = len(list_times[0])
            i = 0
            for times in list_times:
                i += 1
                lenght2 = len(times)
                if lenght != lenght2:
                    print("issue of lenght at the list " + list_stops_names[i])
                    print("The lenght is " + str(lenght2) + " instead of " + str(lenght))
                    assert lenght == lenght2

            # Transposed is the transposition of the table list_times
            transposed = list(map(list, zip(*list_times)))



            # Now we find the bus line in memory
            route1 = None
            for route in self.routes:
                if route.id == route_name:
                    route1 = route
                    break

            if route1 is None:
                # Then we have to create the bus line (add a route to the agency)
                route1 = Route.Route.from_stops_list(route_name, list_stops_names)
                self.routes.append(route1)

            # Here we initialise the graph (just getting the stops names in the file)
            route1.graph = LinkedStops.LinkedStops(route1.id)

            # We make comparison with the list of stops in memory to avoid to have sames
            # stops with different names

            with Comparator.Comparator() as comparator:
                self.update_stops(route1.graph.list_stops_of_graph, comparator)

                # Now that we made sure that the list of stops has nothing unusual, we can actually draw the graph
                route1.graph.create_from_file()



                # Here we check that the stops of the timetable correspond to the stops of the graph
                list_stops_names = route1.graph.check_stops(list_stops_names, comparator)


            for times in transposed:
                route1.add_trip_from_times(list_stops_names, times, trips_service)
                count += 1
        print(str(count) + " trips have been added to the line " + route_name)