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, bus_plate_number): bus_schema = BusSchema_v1() bus = self.session.query(Bus).filter( Bus.bus_plate_number == bus_plate_number).order_by( 'fetched_at desc').first() dump_data = bus_schema.dump(bus).data resp.body = json.dumps(dump_data, ensure_ascii=False)
def on_get(self, req, resp, args): results = {} # create polygon # bbox = left,bottom,right,top # bbox = min Longitude , min Latitude , max Longitude , max Latitude if not (args.get('bbox') or (args.get('center_lat') and args.get('center_lon'))): raise falcon.HTTPBadRequest( title='Missing params', description= 'you must define bbox or (center_lon and center_lat)') if args.get('bbox'): bounding_box = BoundingBox(args.get('bbox')) else: radius = 1.0 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 = self.session.query(Stop).filter() stops = stops.filter(Stop.geom.contained(bbox)) if args.get('include_stop_routes'): if req.path.startswith('/v2'): stop_schema = StopWithStopRoutesSchema_v2() else: stop_schema = StopWithStopRoutesSchema_v1() else: stop_schema = StopSchema_v1() stop_results = stop_schema.dump(stops, many=True).data results['stops'] = stop_results # get bip points bip_spots = None if args.get('include_bip_spots'): BipSpot.add_geometry_column() # @@TODO: fix this bip_spots = self.session.query(BipSpot).filter() bip_spots = bip_spots.filter(BipSpot.geom.contained(bbox)) bip_spot_schema = BipSpotSchema_v1() bip_spot_results = bip_spot_schema.dump(bip_spots, many=True).data results['bip_spots'] = bip_spot_results # get buses buses = None if args.get('include_buses'): buses = self.session.query(Bus).filter() buses = buses.filter(Bus.geom.contained(bbox)) bus_schema = BusSchema_v1() bus_results = bus_schema.dump(buses, many=True).data results['buses'] = bus_results # serialize response body = dict(results=results, ) resp.body = json.dumps(body, ensure_ascii=False)