Skip to content

Commit

Permalink
Merge pull request #161 from impocode/main
Browse files Browse the repository at this point in the history
Add Position2D and Position3D of type NamedTuple
  • Loading branch information
vincentsarago authored May 10, 2024
2 parents d9e84df + a3c60a3 commit 7ec404c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions geojson_pydantic/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Types for geojson_pydantic models"""

from typing import List, Tuple, Union
from typing import List, NamedTuple, Tuple, Union

from pydantic import Field
from typing_extensions import Annotated
Expand All @@ -10,7 +10,11 @@
Tuple[float, float, float, float, float, float], # 3D bbox
]

Position = Union[Tuple[float, float], Tuple[float, float, float]]
Position2D = NamedTuple("Position2D", [("longitude", float), ("latitude", float)])
Position3D = NamedTuple(
"Position3D", [("longitude", float), ("latitude", float), ("altitude", float)]
)
Position = Union[Position2D, Position3D]

# Coordinate arrays
LineStringCoords = Annotated[List[Position], Field(min_length=2)]
Expand Down
50 changes: 50 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest

from geojson_pydantic.types import Position2D, Position3D


@pytest.mark.parametrize("coordinates", [(1, 2), (1.0, 2.0), (1.01, 2.01)])
def test_position2d_valid_coordinates(coordinates):
"""
Two number elements as coordinates should be okay
"""
p = Position2D(longitude=coordinates[0], latitude=coordinates[1])
assert p[0] == coordinates[0]
assert p[1] == coordinates[1]
assert p.longitude == coordinates[0]
assert p.latitude == coordinates[1]
assert p == coordinates

p = Position2D(*coordinates)
assert p[0] == coordinates[0]
assert p[1] == coordinates[1]
assert p.longitude == coordinates[0]
assert p.latitude == coordinates[1]
assert p == coordinates


@pytest.mark.parametrize(
"coordinates", [(1, 2, 3), (1.0, 2.0, 3.0), (1.01, 2.01, 3.01)]
)
def test_position3d_valid_coordinates(coordinates):
"""
Three number elements as coordinates should be okay
"""
p = Position3D(
longitude=coordinates[0], latitude=coordinates[1], altitude=coordinates[2]
)
assert p[0] == coordinates[0]
assert p[1] == coordinates[1]
assert p[2] == coordinates[2]
assert p.longitude == coordinates[0]
assert p.latitude == coordinates[1]
assert p.altitude == coordinates[2]
assert p == coordinates

p = Position3D(*coordinates)
assert p[0] == coordinates[0]
assert p[1] == coordinates[1]
assert p[2] == coordinates[2]
assert p.longitude == coordinates[0]
assert p.latitude == coordinates[1]
assert p.altitude == coordinates[2]

0 comments on commit 7ec404c

Please sign in to comment.