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