Skip to content

Fix bbox crs for OAMaps #1999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pygeoapi/api/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from pygeoapi.provider.base import ProviderGenericError
from pygeoapi.util import (
get_provider_by_type, to_json, filter_providers_by_type,
filter_dict_by_key_value
filter_dict_by_key_value, transform_bbox
)

from . import APIRequest, API, validate_datetime
Expand All @@ -60,6 +60,9 @@
]


DEFAULT_CRS = 'http://www.opengis.net/def/crs/EPSG/0/4326'


def get_collection_map(api: API, request: APIRequest,
dataset, style=None) -> Tuple[dict, int, str]:
"""
Expand Down Expand Up @@ -102,7 +105,10 @@ def get_collection_map(api: API, request: APIRequest,

query_args['format_'] = request.params.get('f', 'png')
query_args['style'] = style
query_args['crs'] = request.params.get('bbox-crs', 4326)
query_args['crs'] = collection_def.get('crs', DEFAULT_CRS)
query_args['bbox_crs'] = request.params.get(
'bbox-crs', DEFAULT_CRS
)
query_args['transparent'] = request.params.get('transparent', True)

try:
Expand Down Expand Up @@ -133,7 +139,7 @@ def get_collection_map(api: API, request: APIRequest,
except AttributeError:
bbox = api.config['resources'][dataset]['extents']['spatial']['bbox'] # noqa
try:
query_args['bbox'] = [float(c) for c in bbox]
bbox = [float(c) for c in bbox]
except ValueError:
exception = {
'code': 'InvalidParameterValue',
Expand All @@ -144,6 +150,12 @@ def get_collection_map(api: API, request: APIRequest,
return headers, HTTPStatus.BAD_REQUEST, to_json(
exception, api.pretty_print)

if query_args['bbox_crs'] != query_args['crs']:
LOGGER.debug(f'Reprojecting bbox CRS: {query_args["crs"]}')
bbox = transform_bbox(bbox, query_args['bbox_crs'], query_args['crs'])

query_args['bbox'] = bbox

LOGGER.debug('Processing datetime parameter')
datetime_ = request.params.get('datetime')
try:
Expand Down
25 changes: 5 additions & 20 deletions pygeoapi/provider/wms_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

CRS_CODES = {
4326: 'EPSG:4326',
'http://www.opengis.net/def/crs/EPSG/0/4326': 'EPSG:4326',
'http://www.opengis.net/def/crs/EPSG/0/3857': 'EPSG:3857'
}

Expand All @@ -65,7 +66,7 @@ def __init__(self, provider_def):

def query(self, style=None, bbox=[-180, -90, 180, 90], width=500,
height=300, crs=4326, datetime_=None, transparent=True,
format_='png'):
bbox_crs=4326, format_='png'):
"""
Generate map

Expand All @@ -86,28 +87,12 @@ def query(self, style=None, bbox=[-180, -90, 180, 90], width=500,

version = self.options.get('version', '1.3.0')

if crs in [4326, 'CRS;84'] and version == '1.3.0':
LOGGER.debug('Swapping 4326 axis order to WMS 1.3 mode (yx)')
bbox2 = ','.join(str(c) for c in
[bbox[1], bbox[0], bbox[3], bbox[2]])
else:
LOGGER.debug('Reprojecting coordinates')
LOGGER.debug(f'Output CRS: {CRS_CODES[crs]}')

src_crs = pyproj.CRS.from_string('epsg:4326')
dest_crs = pyproj.CRS.from_string(CRS_CODES[crs])

transformer = pyproj.Transformer.from_crs(src_crs, dest_crs,
always_xy=True)

minx, miny = transformer.transform(bbox[0], bbox[1])
maxx, maxy = transformer.transform(bbox[2], bbox[3])

bbox2 = ','.join(str(c) for c in [minx, miny, maxx, maxy])
if version == '1.3.0' and CRS_CODES[bbox_crs] == 'EPSG:4326':
bbox = [bbox[1], bbox[0], bbox[3], bbox[2]]
bbox2 = ','.join(map(str, bbox))

if not transparent:
self._transparent = 'FALSE'

crs_param = 'crs' if version == '1.3.0' else 'srs'

params = {
Expand Down
37 changes: 37 additions & 0 deletions tests/api/test_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,40 @@ def test_get_collection_map(config, api_):
assert code == HTTPStatus.OK
assert isinstance(response, bytes)
assert response[1:4] == b'PNG'


def test_map_crs_transform(config, api_):
# Florida in EPSG:4326
params = {
'bbox': '-88.374023,24.826625,-78.112793,31.015279',
# crs is 4326 by implicit since it is the default
}
req = mock_api_request(params)
_, code, floridaIn4326 = get_collection_map(
api_, req, 'mapserver_world_map')
assert code == HTTPStatus.OK

# Area that isn't florida in the ocean; used to make sure
# the same coords with different crs are not the same
params = {
'bbox': '-88.374023,24.826625,-78.112793,31.015279',
'bbox-crs': 'http://www.opengis.net/def/crs/EPSG/0/3857',
}

req = mock_api_request(params)
_, code, florida4326InWrongCRS = get_collection_map(
api_, req, 'mapserver_world_map')
assert code == HTTPStatus.OK

assert florida4326InWrongCRS != floridaIn4326

# Florida again, but this time in EPSG:3857
params = {
'bbox': '-9837751.2884,2854464.3843,-8695476.3377,3634733.5690',
'bbox-crs': 'http://www.opengis.net/def/crs/EPSG/0/3857'
}
req = mock_api_request(params)
_, code, floridaProjectedIn3857 = get_collection_map(
api_, req, 'mapserver_world_map')
assert code == HTTPStatus.OK
assert floridaIn4326 == floridaProjectedIn3857