diff --git a/docs/source/publishing/ogcapi-maps.rst b/docs/source/publishing/ogcapi-maps.rst index ad326afb6..e8fcd0255 100644 --- a/docs/source/publishing/ogcapi-maps.rst +++ b/docs/source/publishing/ogcapi-maps.rst @@ -128,7 +128,8 @@ required. An optional style name can be defined via `options.style`. - `OGC:CRS84` (unsafe) If `crs` is not provided, the server will default to the `storage_crs`; in case it does not exist, the default is `CRS84`. - If `crs-bbox` is not provided, it will default to `CRS84`. If the `bbox` is not provided, it will default to `-180, -90, 180, 90`. + If `crs-bbox` is not provided, it will default to `CRS84`. If the `bbox` is not provided, it will default to + the bbox that is configured as the collection's spatial extent. The response headers will always contain the `Content-Crs` and `Content-Bbox`. Examples: diff --git a/pygeoapi/api/maps.py b/pygeoapi/api/maps.py index 23ce03ca1..3aa060233 100644 --- a/pygeoapi/api/maps.py +++ b/pygeoapi/api/maps.py @@ -62,8 +62,6 @@ 'http://www.opengis.net/spec/ogcapi-maps-1/1.0/conf/core' ] -DEFAULT_BBOX = [-180, -90, 180, 90] # CRS84 - def get_collection_map(api: API, request: APIRequest, dataset: str, style: str | None = None @@ -86,10 +84,9 @@ def get_collection_map(api: API, request: APIRequest, LOGGER.debug('Loading provider') try: - collection_def = get_provider_by_type( - api.config['resources'][dataset]['providers'], 'map') - - p = load_plugin('provider', collection_def) + collection_def = api.config['resources'][dataset] + provider_def = get_provider_by_type(collection_def['providers'], 'map') + p = load_plugin('provider', provider_def) except KeyError: exception = { 'code': 'InvalidParameterValue', @@ -111,8 +108,7 @@ def get_collection_map(api: API, request: APIRequest, # if it does not exist or is not supported, use CRS84. try: if 'crs' not in request.params: - query_args['crs'] = collection_def.get('storage_crs', - DEFAULT_CRS) + query_args['crs'] = provider_def.get('storage_crs', DEFAULT_CRS) else: query_args['crs'] = get_uri(request.params['crs']) @@ -149,20 +145,23 @@ def get_collection_map(api: API, request: APIRequest, LOGGER.debug('Processing bbox parameter') try: - bbox = request.params.get( - 'bbox').split(',') - if len(bbox) != 4: - exception = { - 'code': 'InvalidParameterValue', - 'description': 'bbox values should be minx,miny,maxx,maxy' - } - headers['Content-type'] = 'application/json' - LOGGER.error(exception) - return headers, HTTPStatus.BAD_REQUEST, to_json( - exception, api.pretty_print) - + bbox = request.params.get('bbox').split(',') except AttributeError: - bbox = DEFAULT_BBOX + # in the absence of user-supplied bbox, use collection spatial extent + bbox = collection_def['extents']['spatial']['bbox'] + query_args['bbox-crs'] = collection_def['extents']['spatial'].get( + 'crs', DEFAULT_CRS) + + if len(bbox) != 4: + exception = { + 'code': 'InvalidParameterValue', + 'description': 'bbox values should be minx,miny,maxx,maxy' + } + headers['Content-type'] = 'application/json' + LOGGER.error(exception) + return headers, HTTPStatus.BAD_REQUEST, to_json( + exception, api.pretty_print) + try: bbox = [float(c) for c in bbox] except ValueError: @@ -249,16 +248,16 @@ def get_collection_map(api: API, request: APIRequest, err.http_status_code, headers, request.format, err.ogc_exception_code, err.message) - mt = collection_def['format']['name'] + mt = provider_def['format']['name'] headers['Content-Crs'] = query_args['crs'] headers['Content-Bbox'] = ','.join(map(str, query_args['bbox'])) if format_ == mt: - headers['Content-Type'] = collection_def['format']['mimetype'] + headers['Content-Type'] = provider_def['format']['mimetype'] return headers, HTTPStatus.OK, data elif format_ in [None, 'html']: - headers['Content-Type'] = collection_def['format']['mimetype'] + headers['Content-Type'] = provider_def['format']['mimetype'] return headers, HTTPStatus.OK, data else: exception = { @@ -289,10 +288,9 @@ def get_collection_map_legend(api: API, request: APIRequest, LOGGER.debug('Loading provider') try: - collection_def = get_provider_by_type( - api.config['resources'][dataset]['providers'], 'map') - - p = load_plugin('provider', collection_def) + collection_def = api.config['resources'][dataset] + provider_def = get_provider_by_type(collection_def['providers'], 'map') + p = load_plugin('provider', provider_def) except KeyError: exception = { 'code': 'InvalidParameterValue', @@ -314,10 +312,10 @@ def get_collection_map_legend(api: API, request: APIRequest, err.http_status_code, headers, request.format, err.ogc_exception_code, err.message) - mt = collection_def['format']['name'] + mt = provider_def['format']['name'] if format_ == mt: - headers['Content-Type'] = collection_def['format']['mimetype'] + headers['Content-Type'] = provider_def['format']['mimetype'] return headers, HTTPStatus.OK, data else: exception = { diff --git a/tests/api/test_maps.py b/tests/api/test_maps.py index b13228d2f..45c8e437a 100644 --- a/tests/api/test_maps.py +++ b/tests/api/test_maps.py @@ -34,6 +34,9 @@ from http import HTTPStatus +import pytest + +from pygeoapi.api import API from pygeoapi.api.maps import get_collection_map from tests.util import mock_api_request @@ -104,3 +107,24 @@ def test_map_crs_transform(config, api_): assert code == HTTPStatus.OK assert floridaIn4326 == floridaProjectedIn3857 + + +def test_map_default_bbox_from_collection_extent(config, openapi): + # Florida in EPSG:3857 + config['resources']['mapserver_world_map']['extents']['spatial'] = { + 'bbox': [-9837751.2884, 2854464.3843, -8695476.3377, 3634733.5690], + 'crs': 'http://www.opengis.net/def/crs/EPSG/0/3857' + } + api_ = API(config, openapi) + + # request does not provide bbox, so collection spatial extent is used + req = mock_api_request() + rsp_headers, code, _ = get_collection_map( + api_, req, 'mapserver_world_map') + + assert code == HTTPStatus.OK + assert rsp_headers.get( + 'Content-Crs', 'http://www.opengis.net/def/crs/OGC/1.3/CRS84') + content_bbox = [float(c) for c in rsp_headers['Content-Bbox'].split(',')] + assert content_bbox == pytest.approx( + [-88.374023, 24.826625, -78.112793, 31.015279], abs=1e-5)