def test_get_stations_radius(self):
     result = q.get_stations_radius(self.session, 34.6249516667, 25.321855,
                                    0.1)
     self.assertEqual(len(result), 1)
     result = q.get_stations_radius(self.session, 34.6249516667, 25.321855,
                                    1)
     self.assertEqual(len(result), 2)
Exemple #2
0
def observation_point_v1_0(query: str):
    """
    API Format: /api/v1.0/observation/point/<string:query>.json

    <string:query> : List of key=value pairs, seperated by ;
        valid query keys are: start_date, end_date, datatype, platform_type,
            meta_key, meta_value, mindepth, maxdepth, area, radius

    Observational query for points

    **Used in ObservationSelector**
    """
    query_dict = {
        key: value
        for key, value in [q.split('=') for q in query.split(';')]
    }
    data = []
    max_age = 86400
    params = {}
    MAPPING = {
        'start_date': 'starttime',
        'end_date': 'endtime',
        'datatype': 'variable',
        'platform_type': 'platform_types',
        'meta_key': 'meta_key',
        'meta_value': 'meta_value',
        'mindepth': 'mindepth',
        'maxdepth': 'maxdepth',
    }
    for k, v in query_dict.items():
        if k not in MAPPING:
            continue

        if k in ['start_date', 'end_date']:
            params[MAPPING[k]] = dateparse(v)
        elif k in ['datatype', 'meta_key', 'meta_value']:
            if k == 'meta_key' and v == 'Any':
                continue
            if k == 'meta_value' and query_dict.get('meta_key') == 'Any':
                continue

            params[MAPPING[k]] = v
        elif k == 'platform_type':
            params[MAPPING[k]] = v.split(',')
        else:
            params[MAPPING[k]] = float(v)

    checkpoly = False
    with_radius = False
    if 'area' in query_dict:
        area = json.loads(query_dict.get('area'))
        if len(area) > 1:
            lats = [c[0] for c in area]
            lons = [c[1] for c in area]
            params['minlat'] = min(lats)
            params['minlon'] = min(lons)
            params['maxlat'] = max(lats)
            params['maxlon'] = max(lons)
            poly = Polygon(LinearRing(area))
            checkpoly = True
        else:
            params['latitude'] = area[0][0]
            params['longitude'] = area[0][1]
            params['radius'] = float(query_dict.get('radius', 10))
            with_radius = True

    if with_radius:
        stations = ob_queries.get_stations_radius(session=DB.session, **params)
    else:
        stations = ob_queries.get_stations(session=DB.session, **params)

    if len(stations) > 500:
        stations = stations[::round(len(stations) / 500)]

    for s in stations:
        if checkpoly and not poly.contains(Point(s.latitude, s.longitude)):
            continue

        d = {
            'type': "Feature",
            'geometry': {
                'type': "Point",
                'coordinates': [s.longitude, s.latitude]
            },
            'properties': {
                'type': s.platform.type.name,
                'id': s.id,
                'class': 'observation',
            }
        }
        if s.name:
            d['properties']['name'] = s.name

        data.append(d)

    result = {
        'type': "FeatureCollection",
        'features': data,
    }
    resp = jsonify(result)
    resp.cache_control.max_age = max_age
    return resp
Exemple #3
0
def observation_point_v1_0(query: str):
    """
    API Format: /api/v1.0/observation/point/<string:query>.json

    <string:query> : List of key=value pairs, seperated by ;
        valid query keys are: start_date, end_date, datatype, platform_type,
            meta_key, meta_value, mindepth, maxdepth, area, radius

    Observational query for points

    **Used in ObservationSelector**
    """
    query_dict = {key: value for key, value in [q.split("=") for q in query.split(";")]}
    data = []
    max_age = 86400
    params = {}
    MAPPING = {
        "start_date": "starttime",
        "end_date": "endtime",
        "datatype": "variable",
        "platform_type": "platform_types",
        "meta_key": "meta_key",
        "meta_value": "meta_value",
        "mindepth": "mindepth",
        "maxdepth": "maxdepth",
    }
    for k, v in query_dict.items():
        if k not in MAPPING:
            continue

        if k in ["start_date", "end_date"]:
            params[MAPPING[k]] = dateparse(v)
        elif k in ["datatype", "meta_key", "meta_value"]:
            if k == "meta_key" and v == "Any":
                continue
            if k == "meta_value" and query_dict.get("meta_key") == "Any":
                continue

            params[MAPPING[k]] = v
        elif k == "platform_type":
            params[MAPPING[k]] = v.split(",")
        else:
            params[MAPPING[k]] = float(v)

    checkpoly = False
    with_radius = False
    if "area" in query_dict:
        area = json.loads(query_dict.get("area"))
        if len(area) > 1:
            lats = [c[0] for c in area]
            lons = [c[1] for c in area]
            params["minlat"] = min(lats)
            params["minlon"] = min(lons)
            params["maxlat"] = max(lats)
            params["maxlon"] = max(lons)
            poly = Polygon(LinearRing(area))
            checkpoly = True
        else:
            params["latitude"] = area[0][0]
            params["longitude"] = area[0][1]
            params["radius"] = float(query_dict.get("radius", 10))
            with_radius = True

    if with_radius:
        stations = ob_queries.get_stations_radius(session=DB.session, **params)
    else:
        stations = ob_queries.get_stations(session=DB.session, **params)

    if len(stations) > 500:
        stations = stations[:: round(len(stations) / 500)]

    for s in stations:
        if checkpoly and not poly.contains(Point(s.latitude, s.longitude)):
            continue

        d = {
            "type": "Feature",
            "geometry": {"type": "Point", "coordinates": [s.longitude, s.latitude]},
            "properties": {
                "type": s.platform.type.name,
                "id": s.id,
                "class": "observation",
            },
        }
        if s.name:
            d["properties"]["name"] = s.name

        data.append(d)

    result = {
        "type": "FeatureCollection",
        "features": data,
    }
    resp = jsonify(result)
    resp.cache_control.max_age = max_age
    return resp