1
- import re
2
- from typing import Union
3
-
4
1
import pytest
2
+ import shapely
5
3
from pydantic import ValidationError
6
- from shapely .geometry import shape
7
4
8
5
from geojson_pydantic .geometries import (
9
- Geometry ,
10
6
GeometryCollection ,
11
7
LineString ,
12
8
MultiLineString ,
@@ -35,13 +31,6 @@ def test_pydantic_schema(obj):
35
31
assert obj .model_json_schema ()
36
32
37
33
38
- def assert_wkt_equivalence (geom : Union [Geometry , GeometryCollection ]):
39
- """Assert WKT equivalence with Shapely."""
40
- # Remove any trailing `.0` to match Shapely format
41
- clean_wkt = re .sub (r"\.0(\D)" , r"\1" , geom .wkt )
42
- assert shape (geom ).wkt == clean_wkt
43
-
44
-
45
34
@pytest .mark .parametrize ("coordinates" , [(1.01 , 2.01 ), (1.0 , 2.0 , 3.0 ), (1.0 , 2.0 )])
46
35
def test_point_valid_coordinates (coordinates ):
47
36
"""
@@ -51,7 +40,6 @@ def test_point_valid_coordinates(coordinates):
51
40
assert p .type == "Point"
52
41
assert p .coordinates == coordinates
53
42
assert hasattr (p , "__geo_interface__" )
54
- assert_wkt_equivalence (p )
55
43
56
44
57
45
@pytest .mark .parametrize (
@@ -88,7 +76,6 @@ def test_multi_point_valid_coordinates(coordinates):
88
76
assert p .type == "MultiPoint"
89
77
assert p .coordinates == coordinates
90
78
assert hasattr (p , "__geo_interface__" )
91
- assert_wkt_equivalence (p )
92
79
93
80
94
81
@pytest .mark .parametrize (
@@ -123,7 +110,6 @@ def test_line_string_valid_coordinates(coordinates):
123
110
assert linestring .type == "LineString"
124
111
assert linestring .coordinates == coordinates
125
112
assert hasattr (linestring , "__geo_interface__" )
126
- assert_wkt_equivalence (linestring )
127
113
128
114
129
115
@pytest .mark .parametrize ("coordinates" , [None , "Foo" , [], [(1.0 , 2.0 )], ["Foo" , "Bar" ]])
@@ -162,7 +148,6 @@ def test_multi_line_string_valid_coordinates(coordinates):
162
148
assert multilinestring .type == "MultiLineString"
163
149
assert multilinestring .coordinates == coordinates
164
150
assert hasattr (multilinestring , "__geo_interface__" )
165
- assert_wkt_equivalence (multilinestring )
166
151
167
152
168
153
@pytest .mark .parametrize (
@@ -200,7 +185,6 @@ def test_polygon_valid_coordinates(coordinates):
200
185
else :
201
186
assert polygon .exterior is None
202
187
assert not list (polygon .interiors )
203
- assert_wkt_equivalence (polygon )
204
188
205
189
206
190
@pytest .mark .parametrize (
@@ -248,7 +232,6 @@ def test_polygon_with_holes(coordinates):
248
232
assert hasattr (polygon , "__geo_interface__" )
249
233
assert polygon .exterior == polygon .coordinates [0 ]
250
234
assert list (polygon .interiors ) == [polygon .coordinates [1 ]]
251
- assert_wkt_equivalence (polygon )
252
235
253
236
254
237
@pytest .mark .parametrize (
@@ -325,7 +308,6 @@ def test_multi_polygon(coordinates):
325
308
326
309
assert multi_polygon .type == "MultiPolygon"
327
310
assert hasattr (multi_polygon , "__geo_interface__" )
328
- assert_wkt_equivalence (multi_polygon )
329
311
330
312
331
313
@pytest .mark .parametrize (
@@ -494,7 +476,6 @@ def test_geometry_collection_iteration(coordinates):
494
476
type = "GeometryCollection" , geometries = [polygon , multipolygon ]
495
477
)
496
478
assert hasattr (gc , "__geo_interface__" )
497
- assert_wkt_equivalence (gc )
498
479
iter (gc )
499
480
500
481
@@ -508,7 +489,6 @@ def test_len_geometry_collection(coordinates):
508
489
gc = GeometryCollection (
509
490
type = "GeometryCollection" , geometries = [polygon , multipolygon ]
510
491
)
511
- assert_wkt_equivalence (gc )
512
492
assert len (gc ) == 2
513
493
514
494
@@ -522,21 +502,26 @@ def test_getitem_geometry_collection(coordinates):
522
502
gc = GeometryCollection (
523
503
type = "GeometryCollection" , geometries = [polygon , multipolygon ]
524
504
)
525
- assert_wkt_equivalence (gc )
526
505
assert polygon == gc [0 ]
527
506
assert multipolygon == gc [1 ]
528
507
529
508
530
509
def test_wkt_mixed_geometry_collection ():
531
510
point = Point (type = "Point" , coordinates = (0.0 , 0.0 , 0.0 ))
532
511
line_string = LineString (type = "LineString" , coordinates = [(0.0 , 0.0 ), (1.0 , 1.0 )])
533
- gc = GeometryCollection (type = "GeometryCollection" , geometries = [point , line_string ])
534
- assert_wkt_equivalence (gc )
512
+ assert (
513
+ GeometryCollection (
514
+ type = "GeometryCollection" , geometries = [point , line_string ]
515
+ ).wkt
516
+ == "GEOMETRYCOLLECTION Z (POINT Z (0.0 0.0 0.0), LINESTRING (0.0 0.0, 1.0 1.0))"
517
+ )
535
518
536
519
537
520
def test_wkt_empty_geometry_collection ():
538
- gc = GeometryCollection (type = "GeometryCollection" , geometries = [])
539
- assert_wkt_equivalence (gc )
521
+ assert (
522
+ GeometryCollection (type = "GeometryCollection" , geometries = []).wkt
523
+ == "GEOMETRYCOLLECTION EMPTY"
524
+ )
540
525
541
526
542
527
def test_geometry_collection_warnings ():
@@ -734,3 +719,45 @@ def test_wkt_empty_geometrycollection():
734
719
assert GeometryCollection (type = "GeometryCollection" , geometries = []).wkt .endswith (
735
720
" EMPTY"
736
721
)
722
+
723
+
724
+ @pytest .mark .parametrize (
725
+ "wkt" ,
726
+ (
727
+ "POINT (0.0 0.0)" ,
728
+ # "POINT EMPTY" does not result in valid GeoJSON
729
+ "POINT Z (0.0 0.0 0.0)" ,
730
+ "MULTIPOINT ((0.0 0.0))" ,
731
+ "MULTIPOINT Z ((0.0 0.0 0.0))" ,
732
+ "MULTIPOINT ((0.0 0.0), (1.0 1.0))" ,
733
+ "MULTIPOINT Z ((0.0 0.0 0.0), (1.0 1.0 1.0))" ,
734
+ "MULTIPOINT EMPTY" ,
735
+ "LINESTRING (0.0 0.0, 1.0 1.0, 2.0 2.0)" ,
736
+ "LINESTRING Z (0.0 0.0 0.0, 1.0 1.0 1.0, 2.0 2.0 2.0)" ,
737
+ # "LINESTRING EMPTY" does not result in valid GeoJSON
738
+ "MULTILINESTRING ((0.0 0.0, 1.0 1.0))" ,
739
+ "MULTILINESTRING ((0.0 0.0, 1.0 1.0), (1.0 1.0, 2.0 2.0))" ,
740
+ "MULTILINESTRING Z ((0.0 0.0 0.0, 1.0 1.0 1.0))" ,
741
+ "MULTILINESTRING Z ((0.0 0.0 0.0, 1.0 1.0 1.0), (1.0 1.0 1.0, 2.0 2.0 2.0))" ,
742
+ "MULTILINESTRING EMPTY" ,
743
+ "POLYGON ((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0))" ,
744
+ "POLYGON ((0.0 0.0, 4.0 0.0, 4.0 4.0, 0.0 4.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0))" ,
745
+ "POLYGON Z ((0.0 0.0 0.0, 1.0 1.0 0.0, 2.0 2.0 0.0, 3.0 3.0 0.0, 0.0 0.0 0.0))" ,
746
+ "POLYGON Z ((0.0 0.0 0.0, 4.0 0.0 0.0, 4.0 4.0 0.0, 0.0 4.0 0.0, 0.0 0.0 0.0), (1.0 1.0 0.0, 1.0 2.0 0.0, 2.0 2.0 0.0, 2.0 1.0 0.0, 1.0 1.0 0.0))" ,
747
+ "POLYGON EMPTY" ,
748
+ "MULTIPOLYGON (((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0)))" ,
749
+ "MULTIPOLYGON (((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0)), ((1.0 1.0, 2.0 2.0, 3.0 3.0, 4.0 4.0, 1.0 1.0)))" ,
750
+ "MULTIPOLYGON Z (((0.0 0.0 0.0, 1.0 1.0 0.0, 2.0 2.0 0.0, 3.0 3.0 0.0, 0.0 0.0 0.0)))" ,
751
+ "MULTIPOLYGON Z (((0.0 0.0 0.0, 1.0 1.0 0.0, 2.0 2.0 0.0, 3.0 3.0 0.0, 0.0 0.0 0.0)), ((1.0 1.0 0.0, 2.0 2.0 0.0, 3.0 3.0 0.0, 4.0 4.0 0.0, 1.0 1.0 0.0)))" ,
752
+ "MULTIPOLYGON EMPTY" ,
753
+ "GEOMETRYCOLLECTION (POINT (0.0 0.0))" ,
754
+ "GEOMETRYCOLLECTION (POINT (0.0 0.0), MULTIPOINT ((0.0 0.0), (1.0 1.0)))" ,
755
+ "GEOMETRYCOLLECTION Z (POLYGON EMPTY, MULTIPOLYGON Z (((0.0 0.0 0.0, 1.0 1.0 0.0, 2.0 2.0 0.0, 3.0 3.0 0.0, 0.0 0.0 0.0))))" ,
756
+ "GEOMETRYCOLLECTION Z (LINESTRING Z (0.0 0.0 0.0, 1.0 1.0 1.0, 2.0 2.0 2.0), MULTILINESTRING ((0.0 0.0, 1.0 1.0), (1.0 1.0, 2.0 2.0)))" ,
757
+ "GEOMETRYCOLLECTION EMPTY" ,
758
+ ),
759
+ )
760
+ def test_wkt (wkt : str ):
761
+ # Use Shapely to parse the input WKT so we know it is parsable by other tools.
762
+ # Then load it into a Geometry and ensure the output WKT is the same as the input.
763
+ assert parse_geometry_obj (shapely .from_wkt (wkt ).__geo_interface__ ).wkt == wkt
0 commit comments