Skip to content

Commit

Permalink
Merge pull request #167 from developmentseed/patch/allow-antimeridian…
Browse files Browse the repository at this point in the history
…-crossing-bbox

allow antimeridian crossing bbox
  • Loading branch information
vincentsarago authored Oct 22, 2024
2 parents b3cf3f9 + 8fcadad commit 4531c83
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

Note: Minor version `0.X.0` update might break the API, It's recommended to pin geojson-pydantic to minor version: `geojson-pydantic>=0.6,<0.7`

## [1.1.2] - 2024-10-22

* relax `bbox` validation and allow antimeridian crossing bboxes

## [1.1.1] - 2024-08-29

* add python 3.12 support
Expand Down Expand Up @@ -365,7 +369,10 @@ Although the type file was added in `0.2.0` it wasn't included in the distribute
### Added
- Initial Release

[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.2...HEAD
[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.2...HEAD
[1.1.2]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.1...1.1.2
[1.1.1]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.0...1.1.1
[1.1.0]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.2...1.1.0
[1.0.2]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/developmentseed/geojson-pydantic/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/developmentseed/geojson-pydantic/compare/0.6.3...1.0.0
Expand Down
8 changes: 7 additions & 1 deletion geojson_pydantic/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""pydantic BaseModel for GeoJSON objects."""
from __future__ import annotations

import warnings
from typing import Any, Dict, List, Optional, Set

from pydantic import BaseModel, SerializationInfo, field_validator, model_serializer
Expand Down Expand Up @@ -38,10 +39,15 @@ def validate_bbox(cls, bbox: Optional[BBox]) -> Optional[BBox]:

# Check X
if bbox[0] > bbox[offset]:
errors.append(f"Min X ({bbox[0]}) must be <= Max X ({bbox[offset]}).")
warnings.warn(
f"BBOX crossing the Antimeridian line, Min X ({bbox[0]}) > Max X ({bbox[offset]}).",
UserWarning,
)

# Check Y
if bbox[1] > bbox[1 + offset]:
errors.append(f"Min Y ({bbox[1]}) must be <= Max Y ({bbox[1 + offset]}).")

# If 3D, check Z values.
if offset > 2 and bbox[2] > bbox[2 + offset]:
errors.append(f"Min Z ({bbox[2]}) must be <= Max Z ({bbox[2 + offset]}).")
Expand Down
6 changes: 5 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from geojson_pydantic.base import _GeoJsonBase

BBOXES = (
(100, 0, 0, 0), # Incorrect Order
(0, 100, 0, 0),
(0, 0, 100, 0, 0, 0),
(0, "a", 0, 0), # Invalid Type
Expand All @@ -20,6 +19,11 @@ def test_bbox_validation(values: Tuple) -> None:
_GeoJsonBase(bbox=values)


def test_bbox_antimeridian() -> None:
with pytest.warns(UserWarning):
_GeoJsonBase(bbox=(100, 0, 0, 0))


@pytest.mark.parametrize("values", BBOXES)
def test_bbox_validation_subclass(values: Tuple) -> None:
# Ensure validation is happening correctly when subclassed
Expand Down
16 changes: 14 additions & 2 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,26 @@ def test_feature_validation():

with pytest.raises(ValidationError):
# bad bbox2d
Feature(type="Feature", properties=None, bbox=(100, 100, 0, 0), geometry=None)
Feature(type="Feature", properties=None, bbox=(0, 100, 100, 0), geometry=None)

with pytest.raises(ValidationError):
# bad bbox3d
Feature(
type="Feature",
properties=None,
bbox=(100, 100, 100, 0, 0, 0),
bbox=(0, 100, 100, 100, 0, 0),
geometry=None,
)

# Antimeridian
with pytest.warns(UserWarning):
Feature(type="Feature", properties=None, bbox=(100, 0, 0, 100), geometry=None)

with pytest.warns(UserWarning):
Feature(
type="Feature",
properties=None,
bbox=(100, 0, 0, 0, 100, 100),
geometry=None,
)

Expand Down

0 comments on commit 4531c83

Please sign in to comment.