예제 #1
0
  def bootstrap_gtfs(self, gtfs_feed, feedname='unknown', populate=True):
    # Make sure the GTFS feed is completely loaded.
    gtfs_feed.preload()

    # Set onestopId
    if 'onestopId' not in self.data:
      self.data['onestopId'] = self.make_onestop(
        geohash=geom.geohash_features(gtfs_feed.stops()),
        name=feedname
      )
    feedid = self.onestop()

    # Override operator Onestop IDs
    agency_onestop = {}
    for i in self.operatorsInFeed():
      agency_onestop[i['gtfsAgencyId']] = i['onestopId']

    # Check for agencies.
    gtfs_agencies = []
    for gtfs_agency in gtfs_feed.agencies():
      if populate or gtfs_agency.id() in agency_onestop:
        gtfs_agencies.append(gtfs_agency)
      else:
        # Unknown agency
        pass
    if not gtfs_agencies:
      return

    # Create TL Stops
    stops = {}
    # sort; process all parent stations first.
    order = []
    order += sorted(filter(lambda x:x.location_type()==1, gtfs_feed.stops()), key=lambda x:x.id())
    order += sorted(filter(lambda x:x.location_type()!=1, gtfs_feed.stops()), key=lambda x:x.id())
    for gtfs_stop in order:
      # Create stop from GTFS
      stop = Stop.from_gtfs(gtfs_stop, feedid)
      # Merge into parent station
      parent = gtfs_stop.get('parent_station')
      if parent:
        stop = gtfs_feed.stop(parent)._tl_ref
      # Merge with existing stop
      key = stop.onestop()
      stop = stops.get(key) or stop
      stops[key] = stop
      # Add identifiers and tags
      gtfs_stop._tl_ref = stop
      stop.add_identifier(gtfs_stop.feedid(feedid))

    # Create TL Routes
    for gtfs_route in gtfs_feed.routes():
      if not gtfs_route.stops():
        continue
      # Create route from GTFS
      route = Route.from_gtfs(gtfs_route, feedid)
      # Link to TL Stops
      for gtfs_stop in gtfs_route.stops():
        t = getattr(gtfs_stop, '_tl_ref', None)
        if t:
          route.add_child(t)
      # Maintain reference to GTFS Route
      gtfs_route._tl_ref = route

    # Create TL Agencies
    for gtfs_agency in gtfs_agencies:
      operator = Operator.from_gtfs(
        gtfs_agency,
        feedid,
        onestop_id=agency_onestop.get(gtfs_agency.id())
      )
      for gtfs_route in gtfs_agency.routes():
        t = getattr(gtfs_route, '_tl_ref', None)
        if t:
          operator.add_child(t)
      # Inelegant.
      operator._cache_onestop()
      # Add agency to feed
      self.add_child(operator)
 def test_geohash_features_no_points(self):
   with self.assertRaises(errors.NoPointsError):
     geom.geohash_features([])
예제 #3
0
 def geohash(self):
   return geom.geohash_features(self.stops())
예제 #4
0
 def geohash(self):
     return geom.geohash_features(self.stops())
 def test_geohash_features(self):
   data = [Point(x,y) for x,y in EXPECT]
   assert geom.geohash_features(data) == '9q9'
예제 #6
0
 def geohash(self):
   """Return 10 characters of geohash."""
   return geom.geohash_features(self.stops())
 def test_geohash_features_no_points(self):
     with self.assertRaises(errors.NoPointsError):
         geom.geohash_features([])
 def test_geohash_features(self):
     data = [Point(x, y) for x, y in EXPECT]
     assert geom.geohash_features(data) == '9q9'
예제 #9
0
 def geohash(self):
     """Return 10 characters of geohash."""
     return geom.geohash_features(self.stops())
예제 #10
0
    def bootstrap_gtfs(self, gtfs_feed, feedname='unknown', populate=True):
        # Make sure the GTFS feed is completely loaded.
        gtfs_feed.preload()

        # Set onestopId
        if 'onestopId' not in self.data:
            self.data['onestopId'] = self.make_onestop(
                geohash=geom.geohash_features(gtfs_feed.stops()),
                name=feedname)
        feedid = self.onestop()

        # Override operator Onestop IDs
        agency_onestop = {}
        for i in self.operatorsInFeed():
            agency_onestop[i['gtfsAgencyId']] = i['onestopId']

        # Check for agencies.
        gtfs_agencies = []
        for gtfs_agency in gtfs_feed.agencies():
            if populate or gtfs_agency.id() in agency_onestop:
                gtfs_agencies.append(gtfs_agency)
            else:
                # Unknown agency
                pass
        if not gtfs_agencies:
            return

        # Create TL Stops
        stops = {}
        # sort; process all parent stations first.
        order = []
        order += sorted(filter(lambda x: x.location_type() == 1,
                               gtfs_feed.stops()),
                        key=lambda x: x.id())
        order += sorted(filter(lambda x: x.location_type() != 1,
                               gtfs_feed.stops()),
                        key=lambda x: x.id())
        for gtfs_stop in order:
            # Create stop from GTFS
            stop = Stop.from_gtfs(gtfs_stop, feedid)
            # Merge into parent station
            parent = gtfs_stop.get('parent_station')
            if parent:
                stop = gtfs_feed.stop(parent)._tl_ref
            # Merge with existing stop
            key = stop.onestop()
            stop = stops.get(key) or stop
            stops[key] = stop
            # Add identifiers and tags
            gtfs_stop._tl_ref = stop
            stop.add_identifier(gtfs_stop.feedid(feedid))

        # Create TL Routes
        for gtfs_route in gtfs_feed.routes():
            if not gtfs_route.stops():
                continue
            # Create route from GTFS
            route = Route.from_gtfs(gtfs_route, feedid)
            # Link to TL Stops
            for gtfs_stop in gtfs_route.stops():
                t = getattr(gtfs_stop, '_tl_ref', None)
                if t:
                    route.add_child(t)
            # Maintain reference to GTFS Route
            gtfs_route._tl_ref = route

        # Create TL Agencies
        for gtfs_agency in gtfs_agencies:
            operator = Operator.from_gtfs(gtfs_agency,
                                          feedid,
                                          onestop_id=agency_onestop.get(
                                              gtfs_agency.id()))
            for gtfs_route in gtfs_agency.routes():
                t = getattr(gtfs_route, '_tl_ref', None)
                if t:
                    operator.add_child(t)
            # Inelegant.
            operator._cache_onestop()
            # Add agency to feed
            self.add_child(operator)