Skip to content

Commit c5e0af7

Browse files
committed
make GeoJSON to bbox handling more robust
1 parent 89c9064 commit c5e0af7

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

pycsw/core/util.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Angelos Tzotsos <tzotsos@gmail.com>
66
# Ricardo Garcia Silva <ricardo.garcia.silva@gmail.com>
77
#
8-
# Copyright (c) 2023 Tom Kralidis
8+
# Copyright (c) 2025 Tom Kralidis
99
# Copyright (c) 2015 Angelos Tzotsos
1010
# Copyright (c) 2017 Ricardo Garcia Silva
1111
#
@@ -47,6 +47,7 @@
4747

4848
from urllib.request import Request, urlopen
4949
from urllib.parse import urlparse
50+
from shapely.geometry import shape
5051
from shapely.wkt import loads
5152
from owslib.util import http_post
5253

@@ -182,15 +183,8 @@ def wktenvelope2bbox(envelope):
182183
def geojson_geometry2bbox(geometry):
183184
"""returns bbox string of GeoJSON geometry"""
184185

185-
geom_type = geometry.get('type')
186-
coords = geometry.get('coordinates')
187-
188-
if geom_type == 'Point':
189-
bbox = '%s,%s,%s,%s' % (coords[0], coords[1], coords[0], coords[1])
190-
elif geom_type == 'Polygon':
191-
bbox = '%s,%s,%s,%s' % (coords[0][0][0], coords[0][0][1], coords[0][2][0], coords[0][2][1])
192-
193-
return bbox
186+
bounds = shape(geometry).bounds
187+
return ','.join([str(b) for b in bounds])
194188

195189

196190
def wkt2geom(ewkt, bounds=True):

tests/unittests/test_util.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from unittest import mock
3838

3939
import pytest
40+
from shapely import from_geojson
4041
from shapely.wkt import loads
4142

4243
from pycsw.core import util
@@ -386,3 +387,53 @@ def test_str2bool():
386387
assert not util.str2bool(False)
387388
assert not util.str2bool('off')
388389
assert not util.str2bool('no')
390+
391+
392+
@pytest.mark.parametrize("geometry,expected", [
393+
({
394+
'type': 'Point',
395+
'coordinates': [102.0, 0.5]
396+
}, '102.0,0.5,102.0,0.5'), ({
397+
'type': 'LineString',
398+
'coordinates': [
399+
[102.0, 0.0],
400+
[103.0, 1.0],
401+
[104.0, 0.0],
402+
[105.0, 1.0]
403+
]
404+
}, '102.0,0.0,105.0,1.0'), ({
405+
'type': 'Polygon',
406+
'coordinates': [[
407+
[100.0, 0.0],
408+
[101.0, 0.0],
409+
[101.0, 1.0],
410+
[100.0, 1.0],
411+
[100.0, 0.0]
412+
]]
413+
}, '100.0,0.0,101.0,1.0'), ({
414+
'type': 'MultiPolygon',
415+
'coordinates': [[[
416+
[30.0, 20.0],
417+
[45.0, 40.0],
418+
[10.0, 40.0],
419+
[30.0, 20.0]
420+
]], [[
421+
[15.0, 5.0],
422+
[40.0, 10.0],
423+
[10.0, 20.0],
424+
[5.0, 10.0],
425+
[15.0, 5.0]
426+
]]]
427+
}, '5.0,5.0,45.0,40.0'), ({
428+
'type': 'MultiPoint',
429+
'coordinates': [
430+
[10.0, 40.0],
431+
[40.0, 30.0],
432+
[20.0, 20.0],
433+
[30.0, 10.0]
434+
]
435+
}, '10.0,10.0,40.0,40.0')
436+
])
437+
def test_geojson_geometry2bbox(geometry, expected):
438+
bounds = util.geojson_geometry2bbox(geometry)
439+
assert bounds == expected

0 commit comments

Comments
 (0)