def on_get(self, req, resp, args): page = args.get('page', 1) per_page_limit = args.get('limit', PER_PAGE_LIMIT) bip_spots = self.session.query(BipSpot).filter() if args.get('center_lat') and args.get('center_lon'): BipSpot.add_geometry_column() # @@TODO: fix this pt = WKTElement('POINT({0} {1})'.format(args['center_lon'], args['center_lat']), srid=4326) bip_spots = bip_spots.order_by(BipSpot.geom.distance_box(pt)) paginator = pager(bip_spots, page, per_page_limit) # serializer results bip_spot_schema = BipSpotSchema_v1() results = bip_spot_schema.dump(paginator.items, many=True).data # build body body = dict( has_next=paginator.has_next, total_results=paginator.total, total_pages=paginator.pages, results=results, page_size=len(results), page_number=paginator.page, ) resp.body = json.dumps(body, ensure_ascii=False)
def on_get(self, req, resp, args): page = args.get('page', 1) per_page_limit = args.get('limit', PER_PAGE_LIMIT) buses = self.session.query(Bus).filter() if args.get('bbox'): bounding_box = BoundingBox(args.get('bbox')) polygon_text = bounding_box.to_polygon_text() bbox = WKTElement(polygon_text, srid=4326) buses = buses.filter(Bus.geom.contained(bbox)) elif args.get('center_lat') and args.get('center_lon'): if args.get('radius'): radius = float(args.get('radius')) / 1000.0 bounding_box = get_bounding_box( latitude_in_degrees=args.get('center_lat'), longitude_in_degrees=args.get('center_lon'), half_side_in_km=radius) polygon_text = bounding_box.to_polygon_text() bbox = WKTElement(polygon_text, srid=4326) buses = buses.filter(Bus.geom.contained(bbox)) else: pt = WKTElement('POINT({0} {1})'.format( args['center_lon'], args['center_lat']), srid=4326) buses = buses.order_by(Bus.geom.distance_box(pt)) if args.get( 'direction_id' ) != None: # explicit None cast (because direction_id=0 exists) buses = buses.filter(Bus.direction_id == args['direction_id']) if args.get('route_id'): buses = buses.filter( Bus.original_route_id == args['route_id'].upper()) # detect temporary condition of duplicated records if buses.distinct(Bus.fetched_at).count() > 1: last_fetch = self.session.query(func.max( Bus.fetched_at)).filter().scalar() buses = buses.filter(Bus.fetched_at == last_fetch) bus_schema = BusSchema_v1() paginator = pager(buses, page, per_page_limit) # serializer results bus_schema = BusSchema_v1() results = bus_schema.dump(paginator.items, many=True).data # build body body = dict( has_next=paginator.has_next, total_results=paginator.total, total_pages=paginator.pages, results=results, page_size=len(results), page_number=paginator.page, ) resp.body = json.dumps(body, ensure_ascii=False)
def on_get(self, req, resp, args): page = args.get('page', 1) per_page_limit = args.get('limit', PER_PAGE_LIMIT) stops = self.session.query(Stop).filter() if args.get('agency_id'): stops = stops.filter(Stop.agency_id == args.get('agency_id')) if args.get('bbox'): bounding_box = BoundingBox(args.get('bbox')) polygon_text = bounding_box.to_polygon_text() bbox = WKTElement(polygon_text, srid=4326) stops = stops.filter(Stop.geom.contained(bbox)) elif args.get('center_lat') and args.get('center_lon'): if args.get('radius'): radius = float(args.get('radius')) / 1000.0 bounding_box = get_bounding_box( latitude_in_degrees=args.get('center_lat'), longitude_in_degrees=args.get('center_lon'), half_side_in_km=radius) polygon_text = bounding_box.to_polygon_text() bbox = WKTElement(polygon_text, srid=4326) stops = stops.filter(Stop.geom.contained(bbox)) else: pt = WKTElement('POINT({0} {1})'.format( args['center_lon'], args['center_lat']), srid=4326) stops = stops.order_by(Stop.geom.distance_box(pt)) paginator = pager(stops, page, per_page_limit) stop_schema = StopSchema_v1() items = paginator.items if args.get('is_active'): items = filter(lambda x: x.is_active(), items) results = stop_schema.dump(items, many=True).data # build body body = dict( has_next=paginator.has_next, total_results=paginator.total, total_pages=paginator.pages, results=results, page_size=len(results), page_number=paginator.page, ) resp.body = json.dumps(body, ensure_ascii=False)
def on_get(self, req, resp, args): page = args.get('page', 1) per_page_limit = args.get('limit', PER_PAGE_LIMIT) trips = self.session.query(Trip).filter() paginator = pager(trips, page, per_page_limit) # serializer results trip_schema = TripSchema_v1() results = trip_schema.dump(paginator.items, many=True).data # build body body = dict( has_next=paginator.has_next, total_results=paginator.total, total_pages=paginator.pages, results=results, page_size=len(results), page_number=paginator.page, ) resp.body = json.dumps(body, ensure_ascii=False)