# Path: mta_data/utils.py
# def st_line_locate_point(linestring, p):
# 
#     coords = linestring.coords
#     before_length = after_length = seg_length = 0
#     best_dist = 1000000000
# 
#     found = None
#     for i in range(len(coords) - 1):
#         p0, p1 = coords[i:i+2]
#         this_length = sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) **2)
#         # next four lines from comp.graphics.algorithms Frequently
#         # Asked Questions  via GEOS
# 
#         dx=p1[0]-p0[0]
#         dy=p1[1]-p0[1]
#         len2=dx*dx+dy*dy
#         r=((p[0]-p0[0])*dx+(p[1]-p0[1])*dy)/len2
# 
#         if r < 0:
#             r = 0
#             dist = (p0[0] - p[0]) ** 2 + (p0[1] - p[1]) ** 2
#         elif r > 1:
#             r = 1
#             dist = (p1[0] - p[0]) ** 2 + (p1[1] - p[1]) ** 2
# 
#         else:
#             dist = dist_line_point(p0, p1, p)
#             
#         if dist < best_dist:
#             best_dist = dist
# 
#             found = r
#             before_length += after_length + seg_length
#             after_length = 0
#             seg_length = this_length
#         else:
#             if found is None:
#                 before_length += this_length
#             else:
#                 after_length += this_length
# 
#     total_length = before_length + seg_length + after_length
#     before_r = before_length / total_length
#     seg_r = seg_length / total_length
# 
#     return before_r + found * seg_r


from django.contrib.gis.geos import LineString, Point
from django.core.management.base import BaseCommand
from mta_data_parser import parse_schedule_dir
from mta_data.models import *
from mta_data.utils import st_line_locate_point
from subway_stop_to_gid import stop_id_to_gid, end_of_line
from zipfile import ZipFile
import os
import re
import transitfeed
import zipfile
                import pdb;pdb.set_trace()
            import traceback
            import pdb;pdb.set_trace()
    best_dist = 100000000000000

    #routes are sorted by length, because we want to use the shortest
    #route that matches the points.
    for route in sorted(candidate_routes, key=lambda route:route.the_geom.length):
        total_dist = 0
        for stop in stops:
            total_dist += route.the_geom.distance(Point(stop.stop_lon, stop.stop_lat))
        if total_dist < best_dist:
            best_dist = total_dist
            best_route = route

    if candidate_routes[0].route == 'S55':
        print "The MTA's route shape for S55 is from 2007.  So we're skipping it."
        return None

    if candidate_routes[0].route == 'Q48':
        #this is a total hack; the Q48 is in general a total hack
        if len(stops) == 22:
            for route in candidate_routes:
                if route.gid == 10707:
                    best_route = route


    #figure out if the set of stops is shorter than the best route
    #(the bus stops or ends in the middle of the route) and if so,
    #cut the route down.

    start_location = st_line_locate_point(best_route.the_geom, (stops[0].stop_lon, stops[0].stop_lat))


# Path: mta_data/utils.py
# def st_line_locate_point(linestring, p):
# 
#     coords = linestring.coords
#     before_length = after_length = seg_length = 0
#     best_dist = 1000000000
# 
#     found = None
#     for i in range(len(coords) - 1):
#         p0, p1 = coords[i:i+2]
#         this_length = sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) **2)
#         # next four lines from comp.graphics.algorithms Frequently
#         # Asked Questions  via GEOS
# 
#         dx=p1[0]-p0[0]
#         dy=p1[1]-p0[1]
#         len2=dx*dx+dy*dy
#         r=((p[0]-p0[0])*dx+(p[1]-p0[1])*dy)/len2
# 
#         if r < 0:
#             r = 0
#             dist = (p0[0] - p[0]) ** 2 + (p0[1] - p[1]) ** 2
#         elif r > 1:
#             r = 1
#             dist = (p1[0] - p[0]) ** 2 + (p1[1] - p[1]) ** 2
# 
#         else:
#             dist = dist_line_point(p0, p1, p)
#             
#         if dist < best_dist:
#             best_dist = dist
# 
#             found = r
#             before_length += after_length + seg_length
#             after_length = 0
#             seg_length = this_length
#         else:
#             if found is None:
#                 before_length += this_length
#             else:
#                 after_length += this_length
# 
#     total_length = before_length + seg_length + after_length
#     before_r = before_length / total_length
#     seg_r = seg_length / total_length
# 
#     return before_r + found * seg_r


# Path: mta_data/management/commands/mta_to_gtfs.py
from django.contrib.gis.geos import LineString, Point
from django.core.management.base import BaseCommand
from mta_data_parser import parse_schedule_dir
from mta_data.models import *
from mta_data.utils import st_line_locate_point
from subway_stop_to_gid import stop_id_to_gid, end_of_line
from zipfile import ZipFile
import os
import re
import transitfeed
import zipfile
                import pdb;pdb.set_trace()
            import traceback
            import pdb;pdb.set_trace()

    best_dist = 100000000000000

    #routes are sorted by length, because we want to use the shortest
    #route that matches the points.
    for route in sorted(candidate_routes, key=lambda route:route.the_geom.length):
        total_dist = 0
        for stop in stops:
            total_dist += route.the_geom.distance(Point(stop.stop_lon, stop.stop_lat))
        if total_dist < best_dist:
            best_dist = total_dist
            best_route = route

    if candidate_routes[0].route == 'S55':
        print "The MTA's route shape for S55 is from 2007.  So we're skipping it."
        return None

    if candidate_routes[0].route == 'Q48':
        #this is a total hack; the Q48 is in general a total hack
        if len(stops) == 22:
            for route in candidate_routes:
                if route.gid == 10707:
                    best_route = route


    #figure out if the set of stops is shorter than the best route
    #(the bus stops or ends in the middle of the route) and if so,
    #cut the route down.

    start_location = st_line_locate_point(best_route.the_geom, (stops[0].stop_lon, stops[0].stop_lat))


    end_location = st_line_locate_point(best_route.the_geom, (stops[-1].stop_lon, stops[-1].stop_lat))