Debian Codegraph @

tests/test_geometries.py

raw
1 import json
2
3 import pytest
4 import shapely
5 from pydantic import ValidationError
6
7 from geojson_pydantic.geometries import (
8 Geometry,
9 GeometryCollection,
10 LineString,
11 MultiLineString,
12 MultiPoint,
13 MultiPolygon,
14 Point,
15 Polygon,
16 parse_geometry_obj,
17 )
18
19
20 @pytest.mark.parametrize(
21 "obj",
22 [
23 GeometryCollection,
24 LineString,
25 MultiLineString,
26 MultiPoint,
27 MultiPolygon,
28 Point,
29 Polygon,
30 ],
31 )
32 def test_pydantic_schema(obj):
33 """Test schema for Pydantic Object."""
34 assert obj.model_json_schema()
35
36
37 @pytest.mark.parametrize("coordinates", [(1.01, 2.01), (1.0, 2.0, 3.0), (1.0, 2.0)])
38 def test_point_valid_coordinates(coordinates):
39 """
40 Two or three number elements as coordinates should be okay
41 """
42 p = Point(type="Point", coordinates=coordinates)
43 assert p.type == "Point"
44 assert p.coordinates == coordinates
45 assert hasattr(p, "__geo_interface__")
46
47
48 @pytest.mark.parametrize(
49 "coordinates",
50 [(1.0,), (1.0, 2.0, 3.0, 4.0), "Foo", (None, 2.0), (1.0, (2.0,)), (), [], None],
51 )
52 def test_point_invalid_coordinates(coordinates):
53 """
54 Too few or to many elements should not, nor weird data types
55 """
56 with pytest.raises(ValidationError):
57 Point(type="Point", coordinates=coordinates)
58
59
60 @pytest.mark.parametrize(
61 "coordinates",
62 [
63 # Empty array
64 [],
65 # No Z
66 [(1.0, 2.0)],
67 [(1.0, 2.0), (1.0, 2.0)],
68 # Has Z
69 [(1.0, 2.0, 3.0), (1.0, 2.0, 3.0)],
70 # Mixed
71 [(1.0, 2.0), (1.0, 2.0, 3.0)],
72 ],
73 )
74 def test_multi_point_valid_coordinates(coordinates):
75 """
76 Two or three number elements as coordinates should be okay, as well as an empty array.
77 """
78 p = MultiPoint(type="MultiPoint", coordinates=coordinates)
79 assert p.type == "MultiPoint"
80 assert p.coordinates == coordinates
81 assert hasattr(p, "__geo_interface__")
82
83
84 @pytest.mark.parametrize(
85 "coordinates",
86 [[(1.0,)], [(1.0, 2.0, 3.0, 4.0)], ["Foo"], [(None, 2.0)], [(1.0, (2.0,))], None],
87 )
88 def test_multi_point_invalid_coordinates(coordinates):
89 """
90 Too few or to many elements should not, nor weird data types
91 """
92 with pytest.raises(ValidationError):
93 MultiPoint(type="MultiPoint", coordinates=coordinates)
94
95
96 @pytest.mark.parametrize(
97 "coordinates",
98 [
99 # Two Points, no Z
100 [(1.0, 2.0), (3.0, 4.0)],
101 # Three Points, no Z
102 [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)],
103 # Two Points, has Z
104 [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
105 # Shapely doesn't like mixed here
106 ],
107 )
108 def test_line_string_valid_coordinates(coordinates):
109 """
110 A list of two coordinates or more should be okay
111 """
112 linestring = LineString(type="LineString", coordinates=coordinates)
113 assert linestring.type == "LineString"
114 assert linestring.coordinates == coordinates
115 assert hasattr(linestring, "__geo_interface__")
116
117
118 @pytest.mark.parametrize("coordinates", [None, "Foo", [], [(1.0, 2.0)], ["Foo", "Bar"]])
119 def test_line_string_invalid_coordinates(coordinates):
120 """
121 But we don't accept non-list inputs, too few coordinates, or bogus coordinates
122 """
123 with pytest.raises(ValidationError):
124 LineString(type="LineString", coordinates=coordinates)
125
126
127 @pytest.mark.parametrize(
128 "coordinates",
129 [
130 # Empty array
131 [],
132 # One line, two points, no Z
133 [[(1.0, 2.0), (3.0, 4.0)]],
134 # One line, two points, has Z
135 [[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]],
136 # One line, three points, no Z
137 [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]],
138 # Two lines, two points each, no Z
139 [[(1.0, 2.0), (3.0, 4.0)], [(0.0, 0.0), (1.0, 1.0)]],
140 # Two lines, two points each, has Z
141 [[(1.0, 2.0, 0.0), (3.0, 4.0, 1.0)], [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]],
142 # Mixed
143 [[(1.0, 2.0), (3.0, 4.0)], [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]],
144 ],
145 )
146 def test_multi_line_string_valid_coordinates(coordinates):
147 """
148 A list of two coordinates or more should be okay
149 """
150 multilinestring = MultiLineString(type="MultiLineString", coordinates=coordinates)
151 assert multilinestring.type == "MultiLineString"
152 assert multilinestring.coordinates == coordinates
153 assert hasattr(multilinestring, "__geo_interface__")
154
155
156 @pytest.mark.parametrize(
157 "coordinates", [None, [None], ["Foo"], [[]], [[(1.0, 2.0)]], [["Foo", "Bar"]]]
158 )
159 def test_multi_line_string_invalid_coordinates(coordinates):
160 """
161 But we don't accept non-list inputs, too few coordinates, or bogus coordinates
162 """
163 with pytest.raises(ValidationError):
164 MultiLineString(type="MultiLineString", coordinates=coordinates)
165
166
167 @pytest.mark.parametrize(
168 "coordinates",
169 [
170 # Empty array
171 [],
172 # Polygon, no Z
173 [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
174 # Polygon, has Z
175 [[(0.0, 0.0, 0.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 0.0)]],
176 ],
177 )
178 def test_polygon_valid_coordinates(coordinates):
179 """
180 Should accept lists of linear rings
181 """
182 polygon = Polygon(type="Polygon", coordinates=coordinates)
183 assert polygon.type == "Polygon"
184 assert polygon.coordinates == coordinates
185 assert hasattr(polygon, "__geo_interface__")
186 if polygon.coordinates:
187 assert polygon.exterior == coordinates[0]
188 else:
189 assert polygon.exterior is None
190 assert not list(polygon.interiors)
191
192
193 @pytest.mark.parametrize(
194 "coordinates",
195 [
196 # Polygon with holes, no Z
197 [
198 [(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0)],
199 [(2.0, 2.0), (2.0, 4.0), (4.0, 4.0), (4.0, 2.0), (2.0, 2.0)],
200 ],
201 # Polygon with holes, has Z
202 [
203 [
204 (0.0, 0.0, 0.0),
205 (0.0, 10.0, 0.0),
206 (10.0, 10.0, 0.0),
207 (10.0, 0.0, 0.0),
208 (0.0, 0.0, 0.0),
209 ],
210 [
211 (2.0, 2.0, 1.0),
212 (2.0, 4.0, 1.0),
213 (4.0, 4.0, 1.0),
214 (4.0, 2.0, 1.0),
215 (2.0, 2.0, 1.0),
216 ],
217 ],
218 # Mixed
219 [
220 [(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0)],
221 [
222 (2.0, 2.0, 2.0),
223 (2.0, 4.0, 0.0),
224 (4.0, 4.0, 0.0),
225 (4.0, 2.0, 0.0),
226 (2.0, 2.0, 2.0),
227 ],
228 ],
229 ],
230 )
231 def test_polygon_with_holes(coordinates):
232 """Check interior and exterior rings."""
233 polygon = Polygon(type="Polygon", coordinates=coordinates)
234 assert polygon.type == "Polygon"
235 assert hasattr(polygon, "__geo_interface__")
236 assert polygon.exterior == polygon.coordinates[0]
237 assert list(polygon.interiors) == [polygon.coordinates[1]]
238
239
240 @pytest.mark.parametrize(
241 "coordinates",
242 [
243 "foo",
244 None,
245 [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)], "foo", None],
246 [[(1.0, 2.0), (3.0, 4.0), (1.0, 2.0)]],
247 [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (7.0, 8.0)]],
248 ],
249 )
250 def test_polygon_invalid_coordinates(coordinates):
251 """
252 Should not accept when:
253 - Coordinates is not a list
254 - Not all elements in coordinates are lists
255 - If not all elements have four or more coordinates
256 - If not all elements are linear rings
257 """
258 with pytest.raises(ValidationError):
259 Polygon(type="Polygon", coordinates=coordinates)
260
261
262 @pytest.mark.parametrize(
263 "coordinates",
264 [
265 # Empty array
266 [],
267 # Multipolygon, no Z
268 [
269 [
270 [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)],
271 [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)],
272 ]
273 ],
274 # Multipolygon, has Z
275 [
276 [
277 [
278 (0.0, 0.0, 4.0),
279 (1.0, 0.0, 4.0),
280 (1.0, 1.0, 4.0),
281 (0.0, 1.0, 4.0),
282 (0.0, 0.0, 4.0),
283 ],
284 [
285 (2.1, 2.1, 4.0),
286 (2.2, 2.1, 4.0),
287 (2.2, 2.2, 4.0),
288 (2.1, 2.2, 4.0),
289 (2.1, 2.1, 4.0),
290 ],
291 ]
292 ],
293 # Mixed
294 [
295 [
296 [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)],
297 [
298 (2.1, 2.1, 2.1),
299 (2.2, 2.1, 2.0),
300 (2.2, 2.2, 2.2),
301 (2.1, 2.2, 2.3),
302 (2.1, 2.1, 2.1),
303 ],
304 ]
305 ],
306 ],
307 )
308 def test_multi_polygon(coordinates):
309 """Should accept sequence of polygons."""
310 multi_polygon = MultiPolygon(type="MultiPolygon", coordinates=coordinates)
311
312 assert multi_polygon.type == "MultiPolygon"
313 assert hasattr(multi_polygon, "__geo_interface__")
314
315
316 @pytest.mark.parametrize(
317 "coordinates",
318 [
319 "foo",
320 None,
321 [
322 [
323 [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)],
324 ],
325 [
326 [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 4.2)],
327 ],
328 ],
329 ],
330 )
331 def test_multipolygon_invalid_coordinates(coordinates):
332 with pytest.raises(ValidationError):
333 MultiPolygon(type="MultiPolygon", coordinates=coordinates)
334
335
336 def test_parse_geometry_obj_point():
337 assert parse_geometry_obj({"type": "Point", "coordinates": [102.0, 0.5]}) == Point(
338 type="Point", coordinates=(102.0, 0.5)
339 )
340
341
342 @pytest.mark.parametrize(
343 "geojson_pydantic_model",
344 (
345 GeometryCollection,
346 LineString,
347 MultiLineString,
348 MultiPoint,
349 MultiPolygon,
350 Point,
351 Polygon,
352 ),
353 )
354 def test_schema_consistency(geojson_pydantic_model):
355 """Test to check that the schema is the same for validation and serialization"""
356 assert geojson_pydantic_model.model_json_schema(
357 mode="validation"
358 ) == geojson_pydantic_model.model_json_schema(mode="serialization")
359
360
361 def test_parse_geometry_obj_multi_point():
362 assert parse_geometry_obj(
363 {"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}
364 ) == MultiPoint(type="MultiPoint", coordinates=[(100.0, 0.0), (101.0, 1.0)])
365
366
367 def test_parse_geometry_obj_line_string():
368 assert parse_geometry_obj(
369 {
370 "type": "LineString",
371 "coordinates": [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]],
372 }
373 ) == LineString(
374 type="LineString",
375 coordinates=[(102.0, 0.0), (103.0, 1.0), (104.0, 0.0), (105.0, 1.0)],
376 )
377
378
379 def test_parse_geometry_obj_multi_line_string():
380 assert parse_geometry_obj(
381 {
382 "type": "MultiLineString",
383 "coordinates": [[[100.0, 0.0], [101.0, 1.0]], [[102.0, 2.0], [103.0, 3.0]]],
384 }
385 ) == MultiLineString(
386 type="MultiLineString",
387 coordinates=[[(100.0, 0.0), (101.0, 1.0)], [(102.0, 2.0), (103.0, 3.0)]],
388 )
389
390
391 def test_parse_geometry_obj_polygon():
392 assert parse_geometry_obj(
393 {
394 "type": "Polygon",
395 "coordinates": [
396 [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]
397 ],
398 }
399 ) == Polygon(
400 type="Polygon",
401 coordinates=[
402 [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]
403 ],
404 )
405
406
407 def test_parse_geometry_obj_multi_polygon():
408 assert parse_geometry_obj(
409 {
410 "type": "MultiPolygon",
411 "coordinates": [
412 [
413 [
414 [102.0, 2.0],
415 [103.0, 2.0],
416 [103.0, 3.0],
417 [102.0, 3.0],
418 [102.0, 2.0],
419 ]
420 ],
421 [
422 [
423 [100.0, 0.0],
424 [101.0, 0.0],
425 [101.0, 1.0],
426 [100.0, 1.0],
427 [100.0, 0.0],
428 ],
429 [
430 [100.2, 0.2],
431 [100.8, 0.2],
432 [100.8, 0.8],
433 [100.2, 0.8],
434 [100.2, 0.2],
435 ],
436 ],
437 ],
438 }
439 ) == MultiPolygon(
440 type="MultiPolygon",
441 coordinates=[
442 [[(102.0, 2.0), (103.0, 2.0), (103.0, 3.0), (102.0, 3.0), (102.0, 2.0)]],
443 [
444 [(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)],
445 [(100.2, 0.2), (100.8, 0.2), (100.8, 0.8), (100.2, 0.8), (100.2, 0.2)],
446 ],
447 ],
448 )
449
450
451 def test_parse_geometry_obj_geometry_collection():
452 assert parse_geometry_obj(
453 {
454 "type": "GeometryCollection",
455 "geometries": [
456 {"type": "Point", "coordinates": [102.0, 0.5]},
457 {"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]},
458 ],
459 }
460 ) == GeometryCollection(
461 type="GeometryCollection",
462 geometries=[
463 Point(type="Point", coordinates=(102.0, 0.5)),
464 MultiPoint(type="MultiPoint", coordinates=[(100.0, 0.0), (101.0, 1.0)]),
465 ],
466 )
467
468
469 def test_parse_geometry_obj_invalid_type():
470 with pytest.raises(ValueError):
471 parse_geometry_obj({"type": "This type", "obviously": "doesn't exist"})
472
473 with pytest.raises(ValueError):
474 parse_geometry_obj({"type": "", "obviously": "doesn't exist"})
475
476 with pytest.raises(ValueError):
477 parse_geometry_obj({})
478
479
480 def test_parse_geometry_obj_invalid_point():
481 """
482 litmus test that invalid geometries don't get parsed
483 """
484 with pytest.raises(ValidationError):
485 parse_geometry_obj(
486 {"type": "Point", "coordinates": ["not", "valid", "coordinates"]}
487 )
488
489
490 @pytest.mark.parametrize(
491 "coordinates", [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]]
492 )
493 def test_geometry_collection_iteration(coordinates):
494 """test if geometry collection is iterable"""
495 polygon = Polygon(type="Polygon", coordinates=coordinates)
496 multipolygon = MultiPolygon(type="MultiPolygon", coordinates=[coordinates])
497 gc = GeometryCollection(
498 type="GeometryCollection", geometries=[polygon, multipolygon]
499 )
500 assert hasattr(gc, "__geo_interface__")
501 assert len(list(gc.iter())) == 2
502 assert list(iter(gc))
503
504
505 @pytest.mark.parametrize(
506 "coordinates", [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]]
507 )
508 def test_len_geometry_collection(coordinates):
509 """test if GeometryCollection return self leng"""
510 polygon = Polygon(type="Polygon", coordinates=coordinates)
511 multipolygon = MultiPolygon(type="MultiPolygon", coordinates=[coordinates])
512 gc = GeometryCollection(
513 type="GeometryCollection", geometries=[polygon, multipolygon]
514 )
515 assert gc.length == 2
516 assert len(list(gc.iter())) == 2
517 assert dict(gc)
518 assert list(iter(gc))
519
520
521 @pytest.mark.parametrize(
522 "coordinates", [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]]
523 )
524 def test_getitem_geometry_collection(coordinates):
525 """test if GeometryCollection is subscriptable"""
526 polygon = Polygon(type="Polygon", coordinates=coordinates)
527 multipolygon = MultiPolygon(type="MultiPolygon", coordinates=[coordinates])
528 gc = GeometryCollection(
529 type="GeometryCollection", geometries=[polygon, multipolygon]
530 )
531 assert polygon == gc.geometries[0]
532 assert multipolygon == gc.geometries[1]
533
534
535 def test_wkt_mixed_geometry_collection():
536 point = Point(type="Point", coordinates=(0.0, 0.0, 0.0))
537 line_string = LineString(
538 type="LineString", coordinates=[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]
539 )
540 assert (
541 GeometryCollection(
542 type="GeometryCollection", geometries=[point, line_string]
543 ).wkt
544 == "GEOMETRYCOLLECTION Z (POINT Z (0.0 0.0 0.0), LINESTRING Z (0.0 0.0 0.0, 1.0 1.0 1.0))"
545 )
546
547
548 def test_wkt_empty_geometry_collection():
549 assert (
550 GeometryCollection(type="GeometryCollection", geometries=[]).wkt
551 == "GEOMETRYCOLLECTION EMPTY"
552 )
553
554
555 def test_geometry_collection_warnings():
556 point = Point(type="Point", coordinates=(0.0, 0.0, 0.0))
557 line_string_z = LineString(
558 type="LineString", coordinates=[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]
559 )
560 line_string = LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)])
561
562 # one geometry
563 with pytest.warns(
564 UserWarning,
565 match="GeometryCollection should not be used for single geometries.",
566 ):
567 GeometryCollection(
568 type="GeometryCollection",
569 geometries=[
570 point,
571 ],
572 )
573
574 # collections of collections
575 with pytest.warns(
576 UserWarning,
577 match="GeometryCollection should not be used for nested GeometryCollections.",
578 ):
579 GeometryCollection(
580 type="GeometryCollection",
581 geometries=[
582 GeometryCollection(
583 type="GeometryCollection", geometries=[point, line_string_z]
584 ),
585 point,
586 ],
587 )
588
589 # homogeneous (Z) geometry
590 with pytest.raises(ValidationError):
591 GeometryCollection(type="GeometryCollection", geometries=[point, line_string])
592
593
594 def test_polygon_from_bounds():
595 """Result from `from_bounds` class method should be the same."""
596 coordinates = [[(1.0, 2.0), (3.0, 2.0), (3.0, 4.0), (1.0, 4.0), (1.0, 2.0)]]
597 assert Polygon(type="Polygon", coordinates=coordinates) == Polygon.from_bounds(
598 1.0, 2.0, 3.0, 4.0
599 )
600
601
602 def test_wkt_name():
603 """Make sure WKT name is derived from geometry Type."""
604
605 class PointType(Point): ...
606
607 assert (
608 PointType(type="Point", coordinates=(1.01, 2.01)).wkt
609 == Point(type="Point", coordinates=(1.01, 2.01)).wkt
610 )
611
612
613 @pytest.mark.parametrize(
614 "coordinates,expected",
615 [
616 ((0, 0), False),
617 ((0, 0, 0), True),
618 ],
619 )
620 def test_point_has_z(coordinates, expected):
621 assert Point(type="Point", coordinates=coordinates).has_z == expected
622
623
624 @pytest.mark.parametrize(
625 "coordinates,expected",
626 [
627 ([(0, 0)], False),
628 ([(0, 0), (1, 1)], False),
629 ([(0, 0), (1, 1, 1)], True),
630 ([(0, 0, 0)], True),
631 ([(0, 0, 0), (1, 1)], True),
632 ],
633 )
634 def test_multipoint_has_z(coordinates, expected):
635 assert MultiPoint(type="MultiPoint", coordinates=coordinates).has_z == expected
636
637
638 @pytest.mark.parametrize(
639 "coordinates,expected",
640 [
641 ([(0, 0), (1, 1)], False),
642 ([(0, 0), (1, 1, 1)], True),
643 ([(0, 0, 0), (1, 1, 1)], True),
644 ([(0, 0, 0), (1, 1)], True),
645 ],
646 )
647 def test_linestring_has_z(coordinates, expected):
648 assert LineString(type="LineString", coordinates=coordinates).has_z == expected
649
650
651 @pytest.mark.parametrize(
652 "coordinates,expected",
653 [
654 ([[(0, 0), (1, 1)]], False),
655 ([[(0, 0), (1, 1)], [(0, 0), (1, 1)]], False),
656 ([[(0, 0), (1, 1)], [(0, 0, 0), (1, 1)]], True),
657 ([[(0, 0), (1, 1)], [(0, 0), (1, 1, 1)]], True),
658 ([[(0, 0), (1, 1, 1)]], True),
659 ([[(0, 0, 0), (1, 1, 1)]], True),
660 ([[(0, 0, 0), (1, 1)]], True),
661 ([[(0, 0, 0), (1, 1, 1)], [(0, 0, 0), (1, 1, 1)]], True),
662 ],
663 )
664 def test_multilinestring_has_z(coordinates, expected):
665 assert (
666 MultiLineString(type="MultiLineString", coordinates=coordinates).has_z
667 == expected
668 )
669
670
671 @pytest.mark.parametrize(
672 "coordinates,expected",
673 [
674 ([[(0, 0), (1, 1), (2, 2), (0, 0)]], False),
675 ([[(0, 0), (1, 1), (2, 2, 2), (0, 0)]], True),
676 ([[(0, 0), (1, 1), (2, 2), (0, 0)], [(0, 0), (1, 1), (2, 2), (0, 0)]], False),
677 (
678 [[(0, 0), (1, 1), (2, 2), (0, 0)], [(0, 0), (1, 1), (2, 2, 2), (0, 0)]],
679 True,
680 ),
681 ([[(0, 0, 0), (1, 1, 1), (2, 2, 2), (0, 0, 0)]], True),
682 (
683 [
684 [(0, 0, 0), (1, 1, 1), (2, 2, 2), (0, 0, 0)],
685 [(0, 0), (1, 1), (2, 2), (0, 0)],
686 ],
687 True,
688 ),
689 ],
690 )
691 def test_polygon_has_z(coordinates, expected):
692 assert Polygon(type="Polygon", coordinates=coordinates).has_z == expected
693
694
695 @pytest.mark.parametrize(
696 "coordinates,expected",
697 [
698 ([[[(0, 0), (1, 1), (2, 2), (0, 0)]]], False),
699 ([[[(0, 0), (1, 1), (2, 2, 2), (0, 0)]]], True),
700 (
701 [[[(0, 0), (1, 1), (2, 2), (0, 0)]], [[(0, 0), (1, 1), (2, 2), (0, 0)]]],
702 False,
703 ),
704 (
705 [
706 [[(0, 0), (1, 1), (2, 2), (0, 0)]],
707 [
708 [(0, 0), (1, 1), (2, 2), (0, 0)],
709 [(0, 0, 0), (1, 1, 1), (2, 2, 2), (0, 0, 0)],
710 ],
711 ],
712 True,
713 ),
714 (
715 [[[(0, 0), (1, 1), (2, 2), (0, 0)]], [[(0, 0), (1, 1), (2, 2, 2), (0, 0)]]],
716 True,
717 ),
718 ([[[(0, 0, 0), (1, 1, 1), (2, 2, 2), (0, 0, 0)]]], True),
719 (
720 [
721 [[(0, 0, 0), (1, 1, 1), (2, 2, 2), (0, 0, 0)]],
722 [[(0, 0), (1, 1), (2, 2), (0, 0)]],
723 ],
724 True,
725 ),
726 ],
727 )
728 def test_multipolygon_has_z(coordinates, expected):
729 assert MultiPolygon(type="MultiPolygon", coordinates=coordinates).has_z == expected
730
731
732 @pytest.mark.parametrize(
733 "shape",
734 [
735 MultiPoint,
736 MultiLineString,
737 Polygon,
738 MultiPolygon,
739 ],
740 )
741 def test_wkt_empty(shape):
742 assert shape(type=shape.__name__, coordinates=[]).wkt.endswith(" EMPTY")
743
744
745 def test_wkt_empty_geometrycollection():
746 assert GeometryCollection(type="GeometryCollection", geometries=[]).wkt.endswith(
747 " EMPTY"
748 )
749
750
751 @pytest.mark.parametrize(
752 "wkt",
753 (
754 "POINT (0.0 0.0)",
755 # "POINT EMPTY" does not result in valid GeoJSON
756 "POINT Z (0.0 0.0 0.0)",
757 "MULTIPOINT ((0.0 0.0))",
758 "MULTIPOINT Z ((0.0 0.0 0.0))",
759 "MULTIPOINT ((0.0 0.0), (1.0 1.0))",
760 "MULTIPOINT Z ((0.0 0.0 0.0), (1.0 1.0 1.0))",
761 "MULTIPOINT EMPTY",
762 "LINESTRING (0.0 0.0, 1.0 1.0, 2.0 2.0)",
763 "LINESTRING Z (0.0 0.0 0.0, 1.0 1.0 1.0, 2.0 2.0 2.0)",
764 # "LINESTRING EMPTY" does not result in valid GeoJSON
765 "MULTILINESTRING ((0.0 0.0, 1.0 1.0))",
766 "MULTILINESTRING ((0.0 0.0, 1.0 1.0), (1.0 1.0, 2.0 2.0))",
767 "MULTILINESTRING Z ((0.0 0.0 0.0, 1.0 1.0 1.0))",
768 "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))",
769 "MULTILINESTRING EMPTY",
770 "POLYGON ((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0))",
771 "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))",
772 "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))",
773 "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))",
774 "POLYGON EMPTY",
775 "MULTIPOLYGON (((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0)))",
776 "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)))",
777 "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)))",
778 "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)))",
779 "MULTIPOLYGON EMPTY",
780 "GEOMETRYCOLLECTION (POINT (0.0 0.0))",
781 "GEOMETRYCOLLECTION (POLYGON EMPTY, MULTIPOLYGON (((0.0 0.0, 1.0 1.0, 2.0 2.0, 3.0 3.0, 0.0 0.0))))",
782 "GEOMETRYCOLLECTION (POINT (0.0 0.0), MULTIPOINT ((0.0 0.0), (1.0 1.0)))",
783 "GEOMETRYCOLLECTION Z (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)), 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))))",
784 "GEOMETRYCOLLECTION EMPTY",
785 ),
786 )
787 def test_wkt(wkt: str):
788 # Use Shapely to parse the input WKT so we know it is parsable by other tools.
789 # Then load it into a Geometry and ensure the output WKT is the same as the input.
790 assert parse_geometry_obj(shapely.from_wkt(wkt).__geo_interface__).wkt == wkt
791
792
793 @pytest.mark.parametrize(
794 "geom",
795 (
796 Point(type="Point", coordinates=[0, 0], bbox=[0, 0, 0, 0]),
797 Point(type="Point", coordinates=[0, 0]),
798 MultiPoint(type="MultiPoint", coordinates=[(0.0, 0.0)], bbox=[0, 0, 0, 0]),
799 MultiPoint(type="MultiPoint", coordinates=[(0.0, 0.0)]),
800 LineString(
801 type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)], bbox=[0, 0, 1, 1]
802 ),
803 LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)]),
804 MultiLineString(
805 type="MultiLineString",
806 coordinates=[[(0.0, 0.0), (1.0, 1.0)]],
807 bbox=[0, 0, 1, 1],
808 ),
809 MultiLineString(type="MultiLineString", coordinates=[[(0.0, 0.0), (1.0, 1.0)]]),
810 Polygon(
811 type="Polygon",
812 coordinates=[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
813 bbox=[1.0, 2.0, 5.0, 6.0],
814 ),
815 Polygon(
816 type="Polygon",
817 coordinates=[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
818 ),
819 MultiPolygon(
820 type="MultiPolygon",
821 coordinates=[[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]],
822 bbox=[1.0, 2.0, 5.0, 6.0],
823 ),
824 MultiPolygon(
825 type="MultiPolygon",
826 coordinates=[[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]],
827 ),
828 ),
829 )
830 def test_geometry_serializer(geom: Geometry):
831 # bbox should always be in the dictionary version of the model
832 # but should only be in the JSON version if not None
833 assert "bbox" in geom.model_dump()
834 if geom.bbox is not None:
835 assert "bbox" in geom.model_dump_json()
836 else:
837 assert "bbox" not in geom.model_dump_json()
838
839
840 def test_geometry_collection_serializer():
841 geom = GeometryCollection(
842 type="GeometryCollection",
843 geometries=[
844 Point(type="Point", coordinates=[0, 0]),
845 LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)]),
846 ],
847 )
848 assert not geom.has_z
849 # bbox will be in the Dict
850 assert "bbox" in geom.model_dump()
851 assert "bbox" in geom.model_dump()["geometries"][0]
852
853 geom = GeometryCollection(
854 type="GeometryCollection",
855 geometries=[
856 Point(type="Point", coordinates=[0, 0, 0]),
857 LineString(
858 type="LineString", coordinates=[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]
859 ),
860 ],
861 )
862 assert geom.has_z
863
864 # bbox should not be in any Geometry nor at the top level
865 geom_ser = json.loads(geom.model_dump_json())
866 assert "bbox" not in geom_ser
867 assert "bbox" not in geom_ser["geometries"][0]
868 assert "bbox" not in geom_ser["geometries"][0]
869
870 geom = GeometryCollection(
871 type="GeometryCollection",
872 geometries=[
873 Point(type="Point", coordinates=[0, 0], bbox=[0, 0, 0, 0]),
874 LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)]),
875 ],
876 )
877 # bbox not in the top level but in the first geometry (point)
878 geom_ser = json.loads(geom.model_dump_json())
879 assert "bbox" not in geom_ser
880 assert "bbox" in geom_ser["geometries"][0]
881 assert "bbox" not in geom_ser["geometries"][1]
882
883 geom = GeometryCollection(
884 type="GeometryCollection",
885 geometries=[
886 Point(type="Point", coordinates=[0, 0], bbox=[0, 0, 0, 0]),
887 LineString(
888 type="LineString",
889 coordinates=[(0.0, 0.0), (1.0, 1.0)],
890 bbox=[0, 0, 1, 1],
891 ),
892 ],
893 )
894 geom_ser = json.loads(geom.model_dump_json())
895 assert "bbox" not in geom_ser
896 assert "bbox" in geom_ser["geometries"][0]
897 assert "bbox" in geom_ser["geometries"][1]
898
899 geom = GeometryCollection(
900 type="GeometryCollection",
901 geometries=[
902 Point(type="Point", coordinates=[0, 0]),
903 LineString(type="LineString", coordinates=[(0.0, 0.0), (1.0, 1.0)]),
904 ],
905 bbox=[0, 0, 1, 1],
906 )
907 geom_ser = json.loads(geom.model_dump_json())
908 assert "bbox" in geom_ser
909 assert "bbox" not in geom_ser["geometries"][0]
910 assert "bbox" not in geom_ser["geometries"][1]
911
912
913 @pytest.mark.parametrize(
914 "obj,kwargs",
915 (
916 (Point, {"coordinates": [0, 0], "bbox": [0, 0, 0, 0]}),
917 (Point, {"coordinates": [0, 0]}),
918 (Point, {"type": "Point", "coordinates": [0, 0]}),
919 (MultiPoint, {"coordinates": [(0.0, 0.0)], "bbox": [0, 0, 0, 0]}),
920 (MultiPoint, {"coordinates": [(0.0, 0.0)]}),
921 (MultiPoint, {"type": "MultiPoint", "coordinates": [(0.0, 0.0)]}),
922 (LineString, {"coordinates": [(0.0, 0.0), (1.0, 1.0)], "bbox": [0, 0, 1, 1]}),
923 (LineString, {"coordinates": [(0.0, 0.0), (1.0, 1.0)]}),
924 (LineString, {"type": "LineString", "coordinates": [(0.0, 0.0), (1.0, 1.0)]}),
925 (MultiLineString, {"coordinates": [[(0.0, 0.0), (1.0, 1.0)]]}),
926 (
927 MultiLineString,
928 {"coordinates": [[(0.0, 0.0), (1.0, 1.0)]], "bbox": [0, 0, 1, 1]},
929 ),
930 (
931 MultiLineString,
932 {
933 "type": "MultiLineString",
934 "coordinates": [[(0.0, 0.0), (1.0, 1.0)]],
935 },
936 ),
937 (
938 Polygon,
939 {
940 "coordinates": [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
941 "bbox": [1.0, 2.0, 5.0, 6.0],
942 },
943 ),
944 (Polygon, {"coordinates": [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]}),
945 (
946 Polygon,
947 {
948 "type": "Polygon",
949 "coordinates": [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
950 },
951 ),
952 (
953 MultiPolygon,
954 {
955 "coordinates": [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]],
956 "bbox": [1.0, 2.0, 5.0, 6.0],
957 },
958 ),
959 (
960 MultiPolygon,
961 {"coordinates": [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]]},
962 ),
963 (
964 MultiPolygon,
965 {
966 "type": "MultiPolygon",
967 "coordinates": [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]],
968 },
969 ),
970 (
971 GeometryCollection,
972 {
973 "geometries": [
974 {"type": "Point", "coordinates": [0, 0]},
975 {"type": "MultiPoint", "coordinates": [[1, 1]]},
976 ]
977 },
978 ),
979 (
980 GeometryCollection,
981 {
982 "type": "GeometryCollection",
983 "geometries": [
984 {"type": "Point", "coordinates": [0, 0]},
985 {"type": "MultiPoint", "coordinates": [[1, 1]]},
986 ],
987 },
988 ),
989 ),
990 )
991 def test_geometry_create(obj, kwargs):
992 """Test Geometry object create with new."""
993 assert obj.create(**kwargs)
994
995
996 @pytest.mark.parametrize(
997 "obj,kwargs",
998 (
999 (Point, {"type": "P", "coordinates": [0, 0]}),
1000 (MultiPoint, {"type": "M", "coordinates": [(0.0, 0.0)]}),
1001 (LineString, {"type": "L", "coordinates": [(0.0, 0.0), (1.0, 1.0)]}),
1002 (
1003 MultiLineString,
1004 {
1005 "type": "M",
1006 "coordinates": [[(0.0, 0.0), (1.0, 1.0)]],
1007 },
1008 ),
1009 (
1010 Polygon,
1011 {
1012 "type": "P",
1013 "coordinates": [[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]],
1014 },
1015 ),
1016 (
1017 MultiPolygon,
1018 {
1019 "type": "M",
1020 "coordinates": [[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]]],
1021 },
1022 ),
1023 (
1024 GeometryCollection,
1025 {
1026 "type": "G",
1027 "geometries": [
1028 {"type": "Point", "coordinates": [0, 0]},
1029 {"type": "MultiPoint", "coordinates": [[1, 1]]},
1030 ],
1031 },
1032 ),
1033 ),
1034 )
1035 def test_geometry_new_invalid(obj, kwargs):
1036 """raise ValidationError with type is invalid."""
1037 with pytest.raises(ValidationError):
1038 obj.create(**kwargs)