Example #1
0
    def load(self, fname):
        try:
            tree = ElementTree.parse(fname)

            for agency_node in tree.getroot().findall('Agency'):
                agency_id = agency_node.get('id', Agency.new_id())
                gtfs_id = agency_node.get('gtfs_id', None)
                name = agency_node.findtext('name')
                url = agency_node.findtext('url')
                timezone = agency_node.findtext('timezone')
                language = agency_node.findtext('language')
                phone = agency_node.findtext('phone')
                fare_url = agency_node.findtext('fare_url')

                a = Agency(name = name, url = url, timezone = timezone, language = language,
                           phone = phone, fare_url = fare_url)
                a.agency_id = int(agency_id)
                a.gtfs_id = gtfs_id
            
            for calendar_node in tree.getroot().findall('Calendar'):
                calendar_id = calendar_node.get('id', Calendar.new_id())
                gtfs_id = calendar_node.get('gtfs_id', None)
                name = calendar_node.findtext('name')
                days = calendar_node.findtext('days')
                start_date = calendar_node.findtext('start_date')
                end_date = calendar_node.findtext('end_date')
                added_excn = calendar_node.findtext('added_excn') or ''
                remov_excn = calendar_node.findtext('remov_excn') or ''

                days = [int(x) for x in days.split()]
                c = Calendar(service_name = name, monday = days[0],
                             tuesday = days[1], wednesday = days[2],
                             thursday = days[3], friday = days[4],
                             saturday = days[5], sunday = days[6],
                             start_date = start_date, end_date = end_date,
                             added_excn = added_excn.split(),
                             remov_excn = remov_excn.split())
                c.calendar_id = int(calendar_id)
                c.gtfs_id = gtfs_id

            for stop_node in tree.getroot().findall('Stop'):
                stop_id = stop_node.get('id', Stop.new_id())
                gtfs_id = stop_node.get('gtfs_id', None)
                code = stop_node.findtext('code')
                name = stop_node.findtext('name')
                description = stop_node.findtext('description')
                latitude = stop_node.findtext('latitude')
                longitude = stop_node.findtext('longitude')
                zone_id = stop_node.findtext('zone_id')
                url = stop_node.findtext('url')
                location_type = stop_node.findtext('location_type')
                parent_station = stop_node.findtext('parent_station')

                try: location_type = int(location_type)
                except: pass

                try:
                    s = Stop(code = code, name = name, description = description,
                             latitude = float(latitude), longitude = float(longitude),
                             zone_id = zone_id, url = url, location_type = location_type,
                             parent_station = parent_station)
                    s.stop_id = int(stop_id)
                    s.gtfs_id = gtfs_id
                except Exception, e:
                    print >> sys.stderr, 'Error loading stop', name, e

            for path_node in tree.getroot().findall('Path'):
                path_id = path_node.get('id', Path.new_id())
                gtfs_id = path_node.get('gtfs_id', None)
                name = path_node.findtext('name')

                coords_node = path_node.find('coordinates')
                coords = []
                for coord_node in coords_node.findall('Coordinate'):
                    try:
                        sequence = int(coord_node.get('sequence', -1))
                        lat = float(coord_node.get('lat', 0.0))
                        lon = float(coord_node.get('lon', 0.0))
                        coords.append((lat, lon))
                    except Exception, e:
                        print >> sys.stderr, 'Invalid coordinate path %s: %s' % (name, e)

                try:
                    p = Path(name = name, coords = coords)
                    p.path_id = int(path_id)
                    p.gtfs_id = gtfs_id
                except Exception, e:
                    print >> sys.stderr, 'Error loading path', name, e