# 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
        min_stop_I = None
        rows = cur.execute("SELECT stop_I, lat, lon FROM stops")
        for stop_I, lat_s, lon_s in rows:
            dist_now = wgs84_distance(lat, lon, lat_s, lon_s)
            if dist_now < min_dist:
                min_dist = dist_now
                min_stop_I = stop_I
        return min_stop_I

    def get_stop_coordinates(self, stop_I):
        cur = self.conn.cursor()
        results = cur.execute("SELECT lat, lon FROM stops WHERE stop_I={stop_I}".format(stop_I=stop_I))
        lat, lon = results.fetchone()
        return lat, lon

    def get_bounding_box_by_stops(self, stop_Is, buffer_ratio=None):
        lats = []
        lons = []
        for stop_I in stop_Is:
            lat, lon = self.get_stop_coordinates(stop_I)
            lats.append(lat)
            lons.append(lon)
        min_lat = min(lats)
        max_lat = max(lats)
        min_lon = min(lons)
        max_lon = max(lons)
        lon_diff = 0
        lat_diff = 0

        if buffer_ratio:

# 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

        min_stop_I = None
        rows = cur.execute("SELECT stop_I, lat, lon FROM stops")
        for stop_I, lat_s, lon_s in rows:
            dist_now = wgs84_distance(lat, lon, lat_s, lon_s)
            if dist_now < min_dist:
                min_dist = dist_now
                min_stop_I = stop_I
        return min_stop_I

    def get_stop_coordinates(self, stop_I):
        cur = self.conn.cursor()
        results = cur.execute("SELECT lat, lon FROM stops WHERE stop_I={stop_I}".format(stop_I=stop_I))
        lat, lon = results.fetchone()
        return lat, lon

    def get_bounding_box_by_stops(self, stop_Is, buffer_ratio=None):
        lats = []
        lons = []
        for stop_I in stop_Is:
            lat, lon = self.get_stop_coordinates(stop_I)
            lats.append(lat)
            lons.append(lon)
        min_lat = min(lats)
        max_lat = max(lats)
        min_lon = min(lons)
        max_lon = max(lons)
        lon_diff = 0
        lat_diff = 0

        if buffer_ratio:

            distance = buffer_ratio * wgs84_distance(min_lat, min_lon, max_lat, max_lon)