def stop_area(stop_area_code): """ Show stops in stop area, eg pair of tram platforms. """ area = ( models.StopArea.query .options(db.joinedload(models.StopArea.admin_area, innerjoin=True), db.joinedload(models.StopArea.locality, innerjoin=True) .joinedload(models.Locality.district)) .get(stop_area_code.upper()) ) if area is None: raise NotFound( Markup("Stop area <strong>{}</strong> does not exist.") .format(stop_area_code) ) if area.code != stop_area_code: return redirect(url_for(".stop_area", stop_area_code=area.code), code=302) stops = ( db.session.query(models.StopPoint) .options(db.undefer(models.StopPoint.lines)) .filter(models.StopPoint.stop_area_ref == area.code, models.StopPoint.active) .order_by(models.StopPoint.short_ind) .all() ) groups = _group_lines_stops(stops) return render_template("stop_area.html", stop_area=area, stops=stops, groups=groups)
def list_in_area(area_code): """ Shows list of districts or localities in administrative area - not all administrative areas have districts. """ area = ( models.AdminArea.query .options(db.joinedload(models.AdminArea.region, innerjoin=True), db.joinedload(models.AdminArea.districts)) .get(area_code) ) if area is None: raise NotFound( Markup("Area with code <strong>{}</strong> does not exist.") .format(area_code) ) # Show list of localities with stops if districts do not exist if not area.districts: group_local = _group_objects(area.list_localities(), attr="name", default="Places") else: group_local = {} return render_template("area.html", area=area, localities=group_local)
def test_full_json(db_loaded): stop = ( db.session.query(models.StopPoint) .options(db.joinedload(models.StopPoint.locality) .joinedload(models.Locality.district), db.joinedload(models.StopPoint.admin_area)) .order_by(models.JourneyPattern.id) # consistent order .get("490000015G") ) assert stop.to_full_json() == STOP_POINT_JSON
def test_single_stop(db_loaded): stop = ( db.session.query(models.StopPoint) .options(db.joinedload(models.StopPoint.locality)) .get("490008638S") ) assert stop.to_geojson() == GEOJSON_3
def _query_stop(*, atco_code=None, naptan_code=None): stop = ( models.StopPoint.query .options( db.joinedload(models.StopPoint.admin_area, innerjoin=True), db.joinedload(models.StopPoint.locality, innerjoin=True) .joinedload(models.Locality.district), db.joinedload(models.StopPoint.stop_area), db.joinedload(models.StopPoint.other_stops) ) ) if atco_code is not None and naptan_code is None: stop = stop.filter(models.StopPoint.atco_code == atco_code.upper()) elif atco_code is None and naptan_code is not None: stop = stop.filter(models.StopPoint.naptan_code == naptan_code.lower()) else: raise TypeError("Either keyword argument 'atco_code' or 'naptan_code' " "must be used.") return stop.one_or_none()
def _show_map(service_code=None, reverse=None, atco_code=None, coords=None): """ Shows map. The map, service line, stop being shown and coordinates/zoom are expected to be the same as if the user has navigated there through the map interface. :param service_code: Service code to show the paths on map. If ATCO code is None the service diagram will be shown in panel. :param reverse: Direction of service. Ignored if service ID is None. :param atco_code: Stop point code to show live data in panel. :param coords: Starting map coordinates. If None, the coordinates will default to stop point if specified, service paths if specified or the centre of GB if neither are specified. """ if atco_code is not None: stop = models.StopPoint.query.get(atco_code.upper()) if stop is None: raise NotFound( Markup("Stop with code <strong>{}</strong> does not exist.") .format(atco_code) ) else: stop = None if service_code is not None: sv = ( models.Service.query .options(db.joinedload(models.Service.patterns)) .filter(models.Service.code == service_code) .one_or_none() ) if sv is None: raise NotFound( Markup("Service <strong>{}</strong> does not exist.") .format(service_code) ) is_reverse, _ = sv.has_mirror(reverse) else: sv = None is_reverse = None # TODO: Add redirect for incorrect capitalisation, etc # Quick check to ensure coordinates are within range of Great Britain if coords is not None and location.check_bounds(coords[0], coords[1]): latitude, longitude, zoom = coords else: latitude, longitude, zoom = None, None, None return render_template("map.html", latitude=latitude, longitude=longitude, zoom=zoom, stop=stop, service=sv, reverse=is_reverse)
def list_in_locality(locality_code): """ Shows stops in locality. Query string attributes: 'group': If 'true', stops belonging to stop areas are grouped, otherwise all stops in locality are shown. """ locality = ( models.Locality.query .options(db.undefer_group("coordinates"), db.joinedload(models.Locality.district), db.joinedload(models.Locality.admin_area, innerjoin=True) .joinedload(models.AdminArea.region, innerjoin=True)) .get(locality_code.upper()) ) if locality is None: raise NotFound( Markup("Place with code <strong>{}</strong> does not exist.") .format(locality_code) ) if locality.code != locality_code: code = locality.code return redirect(url_for(".list_in_locality", locality_code=code), code=302) group_areas = request.args.get("group") == "true" stops = locality.list_stops(group_areas=group_areas) # Check if listed stops do have associated stop areas - enable option to # group by stop areas or not if group_areas and not any(s.table_name == "stop_area" for s in stops): group_areas = None if not group_areas and all(s.stop_area_ref is None for s in stops): group_areas = None stops = _group_objects(stops, attr="name", default="Stops") return render_template("place.html", locality=locality, stops=stops, grouped=group_areas)
def test_multiple_stops(db_loaded): stops = ( db.session.query(models.StopPoint) .options(db.joinedload(models.StopPoint.locality)) .filter(models.StopPoint.stop_area_ref == "490G00008638") .order_by(models.StopPoint.atco_code) .all() ) assert _list_geojson(stops) == { "type": "FeatureCollection", "features": [GEOJSON_2, GEOJSON_3] }
def list_in_district(district_code): """ Shows list of localities in district. """ district = ( models.District.query .options(db.joinedload(models.District.admin_area, innerjoin=True) .joinedload(models.AdminArea.region, innerjoin=True)) .get(district_code) ) if district is None: raise NotFound( Markup("District with code <strong>{}</strong> does not exist.") .format(district_code) ) group_local = _group_objects(district.list_localities(), attr="name", default="Places") return render_template("district.html", district=district, localities=group_local)
def from_list(cls, list_naptan_codes): """ Finds all stops from a list of NaPTAN codes, ordered using the same list. :param list_naptan_codes: List of NaPTAN/SMS codes. :returns: Ordered list of StopPoint objects. """ if list_naptan_codes: def _stop_index(stop): return list_naptan_codes.index(stop.naptan_code) stops = ( cls.query .options(db.joinedload(cls.locality, innerjoin=True)) .filter(cls.naptan_code.in_(list_naptan_codes)) .all() ) stops.sort(key=_stop_index) else: stops = [] return stops