Skip to content

Commit 691a1ad

Browse files
committed
Address GeometryCollection WKT and testing.
1 parent 1fddfca commit 691a1ad

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

geojson_pydantic/geometries.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,20 @@ def __getitem__(self, index: int) -> Geometry:
275275
@property
276276
def wkt(self) -> str:
277277
"""Return the Well Known Text representation."""
278-
coordinates = (
278+
# Each geometry will check its own coordinates for Z and include "Z" in the wkt
279+
# if necessary. Rather than looking at the coordinates for each of the geometries
280+
# again, we can just get the wkt from each of them and check if there is a Z
281+
# anywhere in the text.
282+
283+
# Get the wkt from each of the geometries in the collection
284+
geometries = (
279285
f'({", ".join(geom.wkt for geom in self.geometries)})'
280286
if self.geometries
281287
else "EMPTY"
282288
)
283-
return f"{self.type.upper()} {coordinates}"
289+
# If any of them contain `Z` add Z to the output wkt
290+
z = " Z " if "Z" in geometries else " "
291+
return f"{self.type.upper()}{z}{geometries}"
284292

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

tests/test_geometries.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def test_point_invalid_coordinates(coordinates):
5959
[(1.0, 2.0), (1.0, 2.0)],
6060
# Has Z
6161
[(1.0, 2.0, 3.0), (1.0, 2.0, 3.0)],
62+
# Mixed
63+
[(1.0, 2.0), (1.0, 2.0, 3.0)],
6264
],
6365
)
6466
def test_multi_point_valid_coordinates(coordinates):
@@ -93,6 +95,7 @@ def test_multi_point_invalid_coordinates(coordinates):
9395
[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
9496
# Two Points, has Z
9597
[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
98+
# Shapely doesn't like mixed here
9699
],
97100
)
98101
def test_line_string_valid_coordinates(coordinates):
@@ -130,6 +133,8 @@ def test_line_string_invalid_coordinates(coordinates):
130133
[[(1.0, 2.0), (3.0, 4.0)], [(0.0, 0.0), (1.0, 1.0)]],
131134
# Two lines, two points each, has Z
132135
[[(1.0, 2.0, 0.0), (3.0, 4.0, 1.0)], [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]],
136+
# Mixed
137+
[[(1.0, 2.0), (3.0, 4.0)], [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]],
133138
],
134139
)
135140
def test_multi_line_string_valid_coordinates(coordinates):
@@ -206,6 +211,17 @@ def test_polygon_valid_coordinates(coordinates):
206211
(2.0, 2.0, 1.0),
207212
],
208213
],
214+
# Mixed
215+
[
216+
[(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0)],
217+
[
218+
(2.0, 2.0, 2.0),
219+
(2.0, 4.0, 0.0),
220+
(4.0, 4.0, 0.0),
221+
(4.0, 2.0, 0.0),
222+
(2.0, 2.0, 2.0),
223+
],
224+
],
209225
],
210226
)
211227
def test_polygon_with_holes(coordinates):
@@ -271,6 +287,19 @@ def test_polygon_invalid_coordinates(coordinates):
271287
],
272288
]
273289
],
290+
# Mixed
291+
[
292+
[
293+
[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)],
294+
[
295+
(2.1, 2.1, 2.1),
296+
(2.2, 2.1, 2.0),
297+
(2.2, 2.2, 2.2),
298+
(2.1, 2.2, 2.3),
299+
(2.1, 2.1, 2.1),
300+
],
301+
]
302+
],
274303
],
275304
)
276305
def test_multi_polygon(coordinates):
@@ -452,6 +481,18 @@ def test_getitem_geometry_collection(polygon):
452481
assert item == gc[0]
453482

454483

484+
def test_wkt_mixed_geometry_collection():
485+
point = Point(type="Point", coordinates=(0.0, 0.0, 0.0))
486+
line_string = LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)])
487+
gc = GeometryCollection(type="GeometryCollection", geometries=[point, line_string])
488+
assert_wkt_equivalence(gc)
489+
490+
491+
def test_wkt_empty_geometry_collection():
492+
gc = GeometryCollection(type="GeometryCollection", geometries=[])
493+
assert_wkt_equivalence(gc)
494+
495+
455496
def test_polygon_from_bounds():
456497
"""Result from `from_bounds` class method should be the same."""
457498
coordinates = [[(1.0, 2.0), (3.0, 2.0), (3.0, 4.0), (1.0, 4.0), (1.0, 2.0)]]

0 commit comments

Comments
 (0)