Skip to content

Commit 40726e2

Browse files
committed
Add tests and safety to exterior.
1 parent 40952c4 commit 40726e2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

geojson_pydantic/geometries.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ def check_closure(cls, coordinates: List) -> List:
159159
return coordinates
160160

161161
@property
162-
def exterior(self) -> LinearRing:
162+
def exterior(self) -> Union[LinearRing, None]:
163163
"""Return the exterior Linear Ring of the polygon."""
164-
return self.coordinates[0]
164+
return self.coordinates[0] if self.coordinates else None
165165

166166
@property
167167
def interiors(self) -> Iterator[LinearRing]:
@@ -250,7 +250,11 @@ def _wkt_coordinates(self) -> str:
250250
@property
251251
def wkt(self) -> str:
252252
"""Return the Well Known Text representation."""
253-
return f"{self._wkt_type} ({self._wkt_coordinates})"
253+
return (
254+
self._wkt_type
255+
+ " "
256+
+ (f"({self._wkt_coordinates})" if self._wkt_coordinates else "EMPTY")
257+
)
254258

255259
@property
256260
def __geo_interface__(self) -> Dict[str, Any]:

tests/test_geometries.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def test_polygon_valid_coordinates(coordinates):
175175
assert hasattr(polygon, "__geo_interface__")
176176
if polygon.coordinates:
177177
assert polygon.exterior == coordinates[0]
178+
else:
179+
assert polygon.exterior is None
178180
assert not list(polygon.interiors)
179181
assert_wkt_equivalence(polygon)
180182

@@ -468,3 +470,22 @@ class PointType(Point):
468470
PointType(type="Point", coordinates=(1.01, 2.01)).wkt
469471
== Point(type="Point", coordinates=(1.01, 2.01)).wkt
470472
)
473+
474+
475+
@pytest.mark.parametrize(
476+
"shape",
477+
[
478+
MultiPoint,
479+
MultiLineString,
480+
Polygon,
481+
MultiPolygon,
482+
],
483+
)
484+
def test_wkt_empty(shape):
485+
assert shape(type=shape.__name__, coordinates=[]).wkt.endswith(" EMPTY")
486+
487+
488+
def test_wkt_empty_geometrycollection():
489+
assert GeometryCollection(type="GeometryCollection", geometries=[]).wkt.endswith(
490+
" EMPTY"
491+
)

0 commit comments

Comments
 (0)