Пример #1
0
    def import_agencies(cls, directory):
        try:
            f = open(os.path.join(directory, 'agency.txt'), 'rb')
            reader = csv.reader(f)

            mappings = {'agency_name': 'name',
                        'agency_url': 'url',
                        'agency_timezone': 'timezone',
                        'agency_lang': 'language',
                        'agency_phone': 'phone',
                        'agency_fare_url': 'fare_url'}

            # 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]] = BaseObject.unquote(a)
                # create the agency
                agency = Agency(**kw)
                # set the id
                agency.gtfs_id = BaseObject.unquote(l2[r_headers['agency_id']])

        except IOError, e:
            print >> sys.stderr, 'Unable to open agency.txt:', e
Пример #2
0
    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
Пример #3
0
    def import_calendars(cls, directory):
        try:
            f = open(os.path.join(directory, 'calendar.txt'), 'rb')
            reader = csv.reader(f)

            mappings = {'service_id': 'service_name',
                        'monday': 'monday',
                        'tuesday': 'tuesday',
                        'wednesday': 'wednesday',
                        'thursday': 'thursday',
                        'friday': 'friday',
                        'saturday': 'saturday',
                        'sunday': 'sunday',
                        'start_date': 'start_date',
                        'end_date': 'end_date',
                    }
            transforms = {'service_id': lambda x: x,
                          'monday': lambda x: int(x),
                          'tuesday': lambda x: int(x),
                          'wednesday': lambda x: int(x),
                          'thursday': lambda x: int(x),
                          'friday': lambda x: int(x),
                          'saturday': lambda x: int(x),
                          'sunday': lambda x: int(x),
                          'start_date': lambda x: x,
                          'end_date': lambda x: 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]] = transforms[key](BaseObject.unquote(a))
                # create the calendar
                calendar = Calendar(**kw)
                # set the id
                calendar.gtfs_id = BaseObject.unquote(l2[r_headers['service_id']])

        except IOError, e:
            print >> sys.stderr, 'Unable to open calendar.txt:', e
Пример #4
0
    def import_frequencies(cls, directory):
        from Trip import Trip

        try:
            f = open(os.path.join(directory, 'frequencies.txt'), 'rb')
            reader = csv.reader(f)

            mappings = {'trip_id': ('trip_route', lambda x: Trip.get_by_gtfs_id(x).trip_route),
                        'start_time': ('start', lambda x: x),
                        'end_time': ('end', lambda x: x),
                        'headway_secs': ('headway', lambda x: 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 frequency
                frequency = Frequency(**kw)
                trip_route = frequency.trip_route.frequencies.append(frequency)

        except IOError, e:
            print >> sys.stderr, 'Unable to open frequencies.txt:', e
Пример #5
0
    def import_stops(cls, directory):
        try:
            f = open(os.path.join(directory, 'stops.txt'), 'rb')
            reader = csv.reader(f)

            mappings = {'stop_code': ('code', lambda x: x),
                        'stop_name': ('name', lambda x: x),
                        'stop_desc': ('description', lambda x: x),
                        'stop_lat': ('latitude', lambda x: float(x)),
                        'stop_lon': ('longitude', lambda x: float(x)),
                        'zone_id': ('zone_id', lambda x: x),
                        'stop_url': ('url', lambda x: x),
                        'location_type': ('location_type', lambda x: int(x) if x else 0),
                        'parent_station': ('parent_station', lambda x: 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 stop
                stop = Stop(**kw)
                # set the id
                stop.gtfs_id = BaseObject.unquote(l2[r_headers['stop_id']])

        except IOError, e:
            print >> sys.stderr, 'Unable to open stops.txt:', e
Пример #6
0
    def import_routes(cls, directory):
        try:
            f = open(os.path.join(directory, 'routes.txt'), 'rb')
            reader = csv.reader(f)

            mappings = {'agency_id': ('agency', lambda x: Agency.get_by_gtfs_id(x)),
                        'route_short_name': ('short_name', lambda x: x),
                        'route_long_name': ('long_name', lambda x: x),
                        'route_desc': ('description', lambda x: x),
                        'route_type': ('route_type', lambda x: int(x)),
                        'route_url': ('url', lambda x: x),
                        'route_color': ('color', lambda x: x),
                        'route_text_color': ('text_color', lambda x: 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 route
                route = Route(**kw)
                # set the id
                route.gtfs_id = BaseObject.unquote(l2[r_headers['route_id']])

        except IOError, e:
            print >> sys.stderr, 'Unable to open routes.txt:', e
Пример #7
0
            }

            # 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))

                # find the corresponding trip
                trip_id = BaseObject.unquote(l2[r_headers['trip_id']])
                trip = Trip.get_by_gtfs_id(trip_id)
                if trip is None:
                    print >> sys.stderr, 'no trip for id', trip_id

                # add the trip stop
                stop_id = BaseObject.unquote(l2[r_headers['stop_id']])
                stop = Stop.get_by_gtfs_id(stop_id)
                trip.trip_route.add_stop(stop)

                trip_stop = trip.stops[-1]
                trip_stop.arrival = BaseObject.unquote(l2[r_headers['arrival_time']])
                trip_stop.departure = BaseObject.unquote(l2[r_headers['departure_time']])