def calculate_trip_shape_breakpoints(conn):
from gtfspy import shapes<EOL>cur = conn.cursor()<EOL>breakpoints_cache = {}<EOL>count_bad_shape_ordering = <NUM_LIT:0><EOL>count_bad_shape_fit = <NUM_LIT:0><EOL>count_no_shape_fit = <NUM_LIT:0><EOL>trip_Is = [x[<NUM_LIT:0>] for x in<EOL>cur.execute('<STR_LIT>').fetchall()]<EOL>for trip_I in trip_Is:<EOL><INDENT>row = cur.execute('''<STR_LIT>''', (trip_I,)).fetchone()<EOL>if row is None:<EOL><INDENT>continue<EOL><DEDENT>shape_id = row[<NUM_LIT:0>]<EOL>if shape_id is None or shape_id == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>cur.execute('''<STR_LIT>''',<EOL>(trip_I,))<EOL>stop_points = [dict(seq=row[<NUM_LIT:0>],<EOL>lat=row[<NUM_LIT:1>],<EOL>lon=row[<NUM_LIT:2>],<EOL>stop_I=row[<NUM_LIT:3>])<EOL>for row in cur if row[<NUM_LIT:1>] and row[<NUM_LIT:2>]]<EOL>cache_key = (shape_id, tuple(x['<STR_LIT>'] for x in stop_points))<EOL>if cache_key in breakpoints_cache:<EOL><INDENT>breakpoints = breakpoints_cache[cache_key]<EOL><DEDENT>else:<EOL><INDENT>shape_points = shapes.get_shape_points(cur, shape_id)<EOL>breakpoints, badness= shapes.find_segments(stop_points, shape_points)<EOL>if breakpoints != sorted(breakpoints):<EOL><INDENT>count_bad_shape_ordering += <NUM_LIT:1><EOL>breakpoints_cache[cache_key] = None<EOL>continue  <EOL><DEDENT>breakpoints_cache[cache_key] = breakpoints<EOL>if badness > <NUM_LIT:30> * len(breakpoints):<EOL><INDENT>count_bad_shape_fit += <NUM_LIT:1><EOL><DEDENT><DEDENT>if breakpoints is None:<EOL><INDENT>continue<EOL><DEDENT>if len(breakpoints) == <NUM_LIT:0>:<EOL><INDENT>count_no_shape_fit += <NUM_LIT:1><EOL>continue<EOL><DEDENT>assert len(breakpoints) == len(stop_points)<EOL>cur.executemany('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>((int(bkpt), int(trip_I), int(stpt['<STR_LIT>']))<EOL>for bkpt, stpt in zip(breakpoints, stop_points)))<EOL><DEDENT>if count_bad_shape_fit > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % count_bad_shape_fit)<EOL><DEDENT>if count_bad_shape_ordering > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % count_bad_shape_ordering)<EOL><DEDENT>if count_no_shape_fit > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % count_no_shape_fit)<EOL><DEDENT>conn.commit()<EOL>
Pre-compute the shape points corresponding to each trip's stop.

    Depends: shapes