def __load_routes(self): self.stops = set() # The API offers a paging-like mechanism to fetch routes for page in range(0, 12): line_data = self.line_detail_request(str(page))['data'] if line_data: line_total = len(line_data) logger.info(f"Total lines to process \t\t\t{line_total}") for line_idx, line in enumerate(line_data): logger.info(f"\tprocessing line {line['routeShortname']} \t\t\t [{line_idx}/{line_total}]") route = Route(self.feed_id, str(line['routeId']), self.ctp_cluj.agency_id, self.__parse_route_type(line['routeType']), **{ "route_color": line['routeColor'], "route_text_color": "000000", "route_short_name": line['routeShortname'] }) route.route_long_name = line['routeName'] is_route_supported = self.__process_route(route, line['routeWayCoordinates'], line['routeRoundWayCoordinates'], line['routeWaypoints'], line['routeRoundWaypoints']) if is_route_supported: self.dao.add(route)
def _load_routes(self): self._clear_trips() stops = set() route_data = self.line_request() logger.info(f"Total lines to process \t\t\t{len(route_data['lines'])}") for line_nb, line in enumerate(route_data["lines"]): logger.info( f"\tprocessing line {line['name']} \t\t\t [{line_nb + 1}/{len(route_data['lines'])}]" ) r = Route( self.feed_id, line['id'], line['organization']['id'], self._parse_route_type(line['type']), **{ "route_color": line['color'], "route_text_color": "000000", "route_short_name": line['name'] }) # fetch both directions for direction in [0, 1]: trip_data = self.line_detail_request(r.route_id, direction) r.route_long_name = f"{trip_data['direction_name_tur']} - {trip_data['direction_name_retur']}" trips = [] shape_id = f"shp{r.agency_id}_{r.route_id}_{direction}" shape_count = self.dao.session.query(Shape).filter( Shape.shape_id == shape_id).count() shape_points_count = self.dao.session.query(ShapePoint).filter( ShapePoint.shape_id == shape_id).count() if shape_count == 0 or shape_points_count == 0: shape_points = polyline.decode(trip_data['segment_path']) logger.debug("processing shape") shp = Shape(self.feed_id, f"shp{r.agency_id}_{r.route_id}_{direction}") self.dao.add(shp) for shp_point_index, shape_point in enumerate( shape_points): self.dao.add( ShapePoint(self.feed_id, shp.shape_id, shp_point_index, shape_point[0], shape_point[1], -999999)) else: shp = self.dao.session.query(Shape).get( [self.feed_id, shape_id]) logger.debug( f"total stops to process {len(trip_data['stops'])}") for stop_index, stop in enumerate(trip_data['stops']): logger.debug( f" - processing stop {stop_index + 1} of {len(trip_data['stops'])}" ) s = Stop(self.feed_id, stop['id'], stop['name'], stop['lat'], stop['lng']) if s.stop_id not in stops: stops.add(s.stop_id) self.dao.update(s) result = self._process_route_stop(r, s, shp, direction, stop_index, trips) self.dao.update(r) self.dao.flush()