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
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') trip = tr.add_trip() trip.trip_id = int(trip_id) for trip_node in tree.getroot().findall('Trip'): trip_id = trip_node.get('id', Trip.new_id()) gtfs_id = trip_node.get('gtfs_id', None) name = trip_node.findtext('name') calendar_id = trip_node.findtext('calendar_id') route_id = trip_node.findtext('route_id') route = Route.get(int(route_id)) trip = Trip.get(int(trip_id)) trip.name = name trip.gtfs_id = gtfs_id