|
| 1 | +# ============================================================================= |
| 2 | +# Copyright (c) 2023 Tom Kralidis |
| 3 | +# |
| 4 | +# Author: Tom Kralidis <tomkralidis@gmail.com> |
| 5 | +# |
| 6 | +# Contact email: tomkralidis@gmail.com |
| 7 | +# ============================================================================= |
| 8 | + |
| 9 | +from io import BytesIO |
| 10 | +import logging |
| 11 | +from typing import BinaryIO |
| 12 | + |
| 13 | +from owslib.ogcapi.features import Features |
| 14 | +from owslib.util import Authentication |
| 15 | + |
| 16 | +LOGGER = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class EnvironmentalDataRetrieval(Features): |
| 20 | + """Abstraction for OGC API - Environmental Data Retrieval""" |
| 21 | + |
| 22 | + def __init__(self, url: str, json_: str = None, timeout: int = 30, |
| 23 | + headers: dict = None, auth: Authentication = None): |
| 24 | + __doc__ = Features.__doc__ # noqa |
| 25 | + super().__init__(url, json_, timeout, headers, auth) |
| 26 | + |
| 27 | + def data(self) -> list: |
| 28 | + """ |
| 29 | + implements /collections filtered on EDR data resources |
| 30 | +
|
| 31 | + @returns: `list` of filtered collections object |
| 32 | + """ |
| 33 | + |
| 34 | + datas = [] |
| 35 | + collections_ = super().collections() |
| 36 | + |
| 37 | + for c_ in collections_['collections']: |
| 38 | + for l_ in c_['links']: |
| 39 | + if 'data' in l_['rel']: |
| 40 | + datas.append(c_['id']) |
| 41 | + break |
| 42 | + |
| 43 | + return datas |
| 44 | + |
| 45 | + def query_data(self, collection_id: str, |
| 46 | + query_type: str, **kwargs: dict) -> BinaryIO: |
| 47 | + """ |
| 48 | + implements /collection/{collectionId}/coverage/ |
| 49 | +
|
| 50 | + @type collection_id: string |
| 51 | + @param collection_id: id of collection |
| 52 | + @type query_type: string |
| 53 | + @param query_type: query type |
| 54 | + @type bbox: list |
| 55 | + @param bbox: list of minx,miny,maxx,maxy |
| 56 | + @type coords: string |
| 57 | + @param coords: well-known text geometry |
| 58 | + @type datetime_: string |
| 59 | + @type datetime_: string |
| 60 | + @param datetime_: time extent or time instant |
| 61 | + @type parameter_names: list |
| 62 | + @param parameter_names: list of parameter names |
| 63 | +
|
| 64 | + @returns: coverage data |
| 65 | + """ |
| 66 | + |
| 67 | + kwargs_ = {} |
| 68 | + |
| 69 | + if 'bbox' in kwargs: |
| 70 | + kwargs_['bbox'] = ','.join(list(map(str, kwargs['bbox']))) |
| 71 | + if 'parameter_names' in kwargs: |
| 72 | + kwargs_['parameter_names'] = ','.join(kwargs['parameter_names']) |
| 73 | + |
| 74 | + query_args_map = { |
| 75 | + 'coords': 'coords', |
| 76 | + 'corridor_width': 'corridor-width', |
| 77 | + 'corridor_height': 'corridor-height', |
| 78 | + 'crs': 'crs', |
| 79 | + 'cube-z': 'cube-z', |
| 80 | + 'datetime_': 'datetime', |
| 81 | + 'height': 'height', |
| 82 | + 'height_units': 'height-units', |
| 83 | + 'resolution_x': 'resolution-x', |
| 84 | + 'resolution_y': 'resolution-y', |
| 85 | + 'resolution_z': 'resolution-z', |
| 86 | + 'width': 'width', |
| 87 | + 'width_units': 'width-units', |
| 88 | + 'within': 'within', |
| 89 | + 'z': 'z' |
| 90 | + } |
| 91 | + |
| 92 | + for key, value in query_args_map.items(): |
| 93 | + if key in kwargs: |
| 94 | + kwargs_[value] = kwargs[key] |
| 95 | + |
| 96 | + path = f'collections/{collection_id}/{query_type}' |
| 97 | + |
| 98 | + return self._request(path=path, kwargs=kwargs_) |
0 commit comments