def import_trips(cls, directory): from Route import Route from Calendar import Calendar from TripRoute import TripRoute from Path import Path from Stop import Stop try: f = open(os.path.join(directory, 'trips.txt'), 'rb') reader = csv.reader(f) mappings = {'route_id': ('route', lambda x: Route.get_by_gtfs_id(x)), 'service_id': ('calendar', lambda x: Calendar.get_by_gtfs_id(x)), 'trip_id': ('name', lambda x: x), 'trip_headsign': ('headsign', lambda x: x), 'direction_id': ('direction', lambda x: int(x) if x else 0), 'shape_id': ('path', lambda x: Path.get_by_gtfs_id(x)), } # create a headers with an index headers = reader.next() r_headers = dict([(x, i) for i, x in enumerate(headers)]) for l2 in reader: if len(l2) != len(headers): print >> sys.stderr, 'Invalid line', l2, headers continue kw = {} for i, a in enumerate(l2): key = headers[i] if key in mappings: kw[mappings[key][0]] = mappings[key][1](BaseObject.unquote(a)) # create the trip route trip_route = TripRoute(**kw) # set the id trip_route.gtfs_id = BaseObject.unquote(l2[r_headers['trip_id']]) # create a trip trip = trip_route.add_trip() trip.gtfs_id = BaseObject.unquote(l2[r_headers['trip_id']]) # go through the list again and set block ids #!mwd - I'm not sure how to do this. We link # blocks by trip ids, but block ids are # random in gtfs, so we have no way to link # them back except IOError, e: print >> sys.stderr, 'Unable to open trips.txt:', e
name = trip_route_node.findtext('name') route_id = trip_route_node.findtext('route_id') calendar_id = trip_route_node.findtext('calendar_id') headsign = trip_route_node.findtext('headsign') direction = trip_route_node.findtext('direction') path_id = trip_route_node.findtext('path_id') route = Route.get(int(route_id)) calendar = Calendar.get(int(calendar_id)) path = None if path_id != '': path = Path.get(int(path_id)) tr = TripRoute(name, route, calendar, headsign, int(direction), path) tr.trip_route_id = int(trip_route_id) tr.gtfs_id = gtfs_id route.add_trip_route(tr) # stops stops_node = trip_route_node.find('Stops') for stop_node in stops_node.findall('Stop'): stop_id = stop_node.get('id') stop = Stop.get(int(stop_id)) tr.add_stop(stop) # trips trips_node = trip_route_node.find('Trips') for trip_node in trips_node.findall('Trip'): trip_id = trip_node.get('id')