Skip to content

Commit a383467

Browse files
Merge pull request #114 from moradology/feature/validate-bbox
Add validation for bounding boxes
2 parents 96eb472 + c849ac8 commit a383467

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Validate order of bounding box values. (author @moradology, https://github.com/developmentseed/geojson-pydantic/pull/114)
1213
- Enforce required keys and avoid defaults. This aim to follow the geojson specification to the letter.
1314

1415
```python

geojson_pydantic/features.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ class Config:
2727

2828
use_enum_values = True
2929

30+
@validator("bbox", pre=True)
31+
def check_bbox(cls, bbox: BBox) -> BBox:
32+
"""Check that bbox is valid."""
33+
if len(bbox) == 6:
34+
if bbox[0] > bbox[3] or bbox[1] > bbox[4] or bbox[2] > bbox[5]: # type: ignore
35+
raise ValueError(
36+
"BBox must be in the form [west, south, bottom, east, north, top]"
37+
)
38+
elif len(bbox) == 4:
39+
if bbox[0] > bbox[2] or bbox[1] > bbox[3]:
40+
raise ValueError("BBox must be in the form [west, south, east, north]")
41+
else:
42+
raise ValueError(
43+
"BBox must be in the form [west, south, east, north] or [west, south, bottom, east, north, top]"
44+
)
45+
return bbox
46+
3047
@validator("geometry", pre=True, always=True)
3148
def set_geometry(cls, geometry: Any) -> Any:
3249
"""set geometry from geo interface or input"""

tests/test_features.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,11 @@ def test_feature_validation():
229229
with pytest.raises(ValidationError):
230230
# missing geometry
231231
Feature(type="Feature", properties=None)
232+
233+
with pytest.raises(ValidationError):
234+
# bad bbox2d
235+
Feature(type="Feature", properties=None, bbox=[100, 100, 0, 0])
236+
237+
with pytest.raises(ValidationError):
238+
# bad bbox3d
239+
Feature(type="Feature", properties=None, bbox=[100, 100, 100, 0, 0, 0])

0 commit comments

Comments
 (0)