# Path: gtfspy/shapes.py
# def print_coords(rows, prefix=''):
# def find_segments(stops, shape):
# def find_best_segments(cur, stops, shape_ids, route_id=None,
#                        breakpoints_cache=None):
# def return_segments(shape, break_points):
# def gen_cumulative_distances(stops):
# def get_shape_points(cur, shape_id):
# def get_shape_points2(cur, shape_id):
# def get_route_shape_segments(cur, route_id):
# def get_shape_between_stops(cur, trip_I, seq_stop1=None, seq_stop2=None, shape_breaks=None):
# def get_trip_points(cur, route_id, offset=0, tripid_glob=''):
# def interpolate_shape_times(shape_distances, shape_breaks, stop_times):
# 
# Path: gtfspy/route_types.py
# ALL_ROUTE_TYPES = {WALK, TRAM, SUBWAY, RAIL, BUS, FERRY, CABLE_CAR, GONDOLA, FUNICULAR}
# 
# Path: gtfspy/route_types.py
# WALK = -1
# 
# Path: gtfspy/util.py
# def wgs84_distance(lat1, lon1, lat2, lon2):
#     """Distance (in meters) between two points in WGS84 coord system."""
#     dLat = math.radians(lat2 - lat1)
#     dLon = math.radians(lon2 - lon1)
#     a = (math.sin(dLat / 2) * math.sin(dLat / 2) +
#          math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
#          math.sin(dLon / 2) * math.sin(dLon / 2))
#     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
#     d = EARTH_RADIUS * c
#     return d
# 
# def wgs84_width(meters, lat):
#     R2 = EARTH_RADIUS * cos(lat * TORADIANS)
#     return meters / (R2 * TORADIANS)
# 
# def wgs84_height(meters):
#     return meters / (EARTH_RADIUS * TORADIANS)
# 
# def set_process_timezone(TZ):
#     """
#     Parameters
#     ----------
#     TZ: string
#     """
#     try:
#         prev_timezone = os.environ['TZ']
#     except KeyError:
#         prev_timezone = None
#     os.environ['TZ'] = TZ
# 
#     if sys.platform == 'win32': # tzset() does not work on Windows
#         system_time = SystemTime()
#         lpSystemTime = ctypes.pointer(system_time)
#         ctypes.windll.kernel32.GetLocalTime(lpSystemTime)
#     else:
#         time.tzset()  # Cause C-library functions to notice the update.
# 
#     return prev_timezone


import calendar
import datetime
import logging
import os
import sqlite3
import sys
import time
import warnings
import numpy
import pandas as pd
import pytz
        import json
        import IPython
from collections import Counter, defaultdict
from datetime import timedelta
from six import string_types
from gtfspy import shapes
from gtfspy.route_types import ALL_ROUTE_TYPES
from gtfspy.route_types import WALK
from gtfspy.util import wgs84_distance, wgs84_width, wgs84_height, set_process_timezone
        from gtfspy.import_gtfs import import_gtfs
        from gtfspy.spreading.spreader import Spreader
        from gtfspy.networks import temporal_network
        from gtfspy.stats import frequencies_by_generated_route
        from .timetable_validator import TimetableValidator
        from gtfspy import stats
        from gtfspy.calc_transfers import calc_transfers
    from gtfspy import filter
        from gtfspy.util import write_shapefile
    def replace_stop_i_with_stop_pair_i(self):
        cur = self.conn.cursor()
        queries = [
            "UPDATE stop_times SET stop_I = "
            "(SELECT stops.stop_pair_I AS stop_I FROM stops WHERE stops.stop_I = stop_times.stop_I)",
            # Replace stop_distances
            "ALTER TABLE stop_distances RENAME TO stop_distances_old",

            "CREATE TABLE stop_distances (from_stop_I INT, to_stop_I INT, d INT, d_walk INT, min_transfer_time INT, "
            "timed_transfer INT, UNIQUE (from_stop_I, to_stop_I))",

            "INSERT INTO stop_distances(from_stop_I, to_stop_I, d, d_walk, min_transfer_time, timed_transfer) "
            "SELECT f_stop.stop_pair_I AS from_stop_I, t_stop.stop_pair_I AS to_stop_I, d, d_walk, min_transfer_time, "
            "timed_transfer "
            "FROM "
            "(SELECT from_stop_I, to_stop_I, d, d_walk, min_transfer_time, "
            "timed_transfer "
            "FROM stop_distances_old) sd_o "
            "LEFT JOIN "
            "(SELECT stop_I, stop_pair_I FROM stops) f_stop "
            "ON sd_o.from_stop_I = f_stop.stop_I "
            " JOIN "
            "(SELECT stop_I, stop_pair_I FROM stops) t_stop "
            "ON sd_o.to_stop_I = t_stop.stop_I ;",

            "DROP TABLE stop_distances_old",

            # Replace stops table with other
            "ALTER TABLE stops RENAME TO stops_old",


# Path: gtfspy/shapes.py
# def print_coords(rows, prefix=''):
# def find_segments(stops, shape):
# def find_best_segments(cur, stops, shape_ids, route_id=None,
#                        breakpoints_cache=None):
# def return_segments(shape, break_points):
# def gen_cumulative_distances(stops):
# def get_shape_points(cur, shape_id):
# def get_shape_points2(cur, shape_id):
# def get_route_shape_segments(cur, route_id):
# def get_shape_between_stops(cur, trip_I, seq_stop1=None, seq_stop2=None, shape_breaks=None):
# def get_trip_points(cur, route_id, offset=0, tripid_glob=''):
# def interpolate_shape_times(shape_distances, shape_breaks, stop_times):
# 
# Path: gtfspy/route_types.py
# ALL_ROUTE_TYPES = {WALK, TRAM, SUBWAY, RAIL, BUS, FERRY, CABLE_CAR, GONDOLA, FUNICULAR}
# 
# Path: gtfspy/route_types.py
# WALK = -1
# 
# Path: gtfspy/util.py
# def wgs84_distance(lat1, lon1, lat2, lon2):
#     """Distance (in meters) between two points in WGS84 coord system."""
#     dLat = math.radians(lat2 - lat1)
#     dLon = math.radians(lon2 - lon1)
#     a = (math.sin(dLat / 2) * math.sin(dLat / 2) +
#          math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
#          math.sin(dLon / 2) * math.sin(dLon / 2))
#     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
#     d = EARTH_RADIUS * c
#     return d
# 
# def wgs84_width(meters, lat):
#     R2 = EARTH_RADIUS * cos(lat * TORADIANS)
#     return meters / (R2 * TORADIANS)
# 
# def wgs84_height(meters):
#     return meters / (EARTH_RADIUS * TORADIANS)
# 
# def set_process_timezone(TZ):
#     """
#     Parameters
#     ----------
#     TZ: string
#     """
#     try:
#         prev_timezone = os.environ['TZ']
#     except KeyError:
#         prev_timezone = None
#     os.environ['TZ'] = TZ
# 
#     if sys.platform == 'win32': # tzset() does not work on Windows
#         system_time = SystemTime()
#         lpSystemTime = ctypes.pointer(system_time)
#         ctypes.windll.kernel32.GetLocalTime(lpSystemTime)
#     else:
#         time.tzset()  # Cause C-library functions to notice the update.
# 
#     return prev_timezone


# Path: gtfspy/gtfs.py
import calendar
import datetime
import logging
import os
import sqlite3
import sys
import time
import warnings
import numpy
import pandas as pd
import pytz
        import json
        import IPython
from collections import Counter, defaultdict
from datetime import timedelta
from six import string_types
from gtfspy import shapes
from gtfspy.route_types import ALL_ROUTE_TYPES
from gtfspy.route_types import WALK
from gtfspy.util import wgs84_distance, wgs84_width, wgs84_height, set_process_timezone
        from gtfspy.import_gtfs import import_gtfs
        from gtfspy.spreading.spreader import Spreader
        from gtfspy.networks import temporal_network
        from gtfspy.stats import frequencies_by_generated_route
        from .timetable_validator import TimetableValidator
        from gtfspy import stats
        from gtfspy.calc_transfers import calc_transfers
    from gtfspy import filter
        from gtfspy.util import write_shapefile

    def replace_stop_i_with_stop_pair_i(self):
        cur = self.conn.cursor()
        queries = [
            "UPDATE stop_times SET stop_I = "
            "(SELECT stops.stop_pair_I AS stop_I FROM stops WHERE stops.stop_I = stop_times.stop_I)",
            # Replace stop_distances
            "ALTER TABLE stop_distances RENAME TO stop_distances_old",

            "CREATE TABLE stop_distances (from_stop_I INT, to_stop_I INT, d INT, d_walk INT, min_transfer_time INT, "
            "timed_transfer INT, UNIQUE (from_stop_I, to_stop_I))",

            "INSERT INTO stop_distances(from_stop_I, to_stop_I, d, d_walk, min_transfer_time, timed_transfer) "
            "SELECT f_stop.stop_pair_I AS from_stop_I, t_stop.stop_pair_I AS to_stop_I, d, d_walk, min_transfer_time, "
            "timed_transfer "
            "FROM "
            "(SELECT from_stop_I, to_stop_I, d, d_walk, min_transfer_time, "
            "timed_transfer "
            "FROM stop_distances_old) sd_o "
            "LEFT JOIN "
            "(SELECT stop_I, stop_pair_I FROM stops) f_stop "
            "ON sd_o.from_stop_I = f_stop.stop_I "
            " JOIN "
            "(SELECT stop_I, stop_pair_I FROM stops) t_stop "
            "ON sd_o.to_stop_I = t_stop.stop_I ;",

            "DROP TABLE stop_distances_old",

            # Replace stops table with other
            "ALTER TABLE stops RENAME TO stops_old",


            "CREATE TABLE stops (stop_I INTEGER PRIMARY KEY, stop_id TEXT UNIQUE NOT NULL, code TEXT, name TEXT, "