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 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
|
import random
import warnings
import numpy as np
import pandas as pd
import shapely
import shapely.affinity
import shapely.geometry
import shapely.wkb
import shapely.wkt
from shapely import MultiPolygon, Polygon, geos_version
from shapely.geometry.base import CAP_STYLE, JOIN_STYLE
import geopandas
from geopandas._compat import GEOS_GE_312, HAS_PYPROJ, SHAPELY_GE_21
from geopandas.array import (
GeometryArray,
_check_crs,
_crs_mismatch_warn,
from_shapely,
from_wkb,
from_wkt,
points_from_xy,
to_wkb,
to_wkt,
)
import pytest
triangle_no_missing = [
shapely.geometry.Polygon([(random.random(), random.random()) for i in range(3)])
for _ in range(10)
]
triangles = triangle_no_missing + [shapely.wkt.loads("POLYGON EMPTY"), None]
T = from_shapely(triangles)
points_no_missing = [
shapely.geometry.Point(random.random(), random.random()) for _ in range(20)
]
points = points_no_missing + [None]
P = from_shapely(points)
def equal_geometries(result, expected):
for r, e in zip(result, expected):
if r is None or e is None:
if not (r is None and e is None):
return False
elif not r.equals(e):
return False
return True
def test_points():
x = np.arange(10).astype(np.float64)
y = np.arange(10).astype(np.float64) ** 2
points = points_from_xy(x, y)
assert isinstance(points, GeometryArray)
for i in range(10):
assert isinstance(points[i], shapely.geometry.Point)
assert points[i].x == x[i]
assert points[i].y == y[i]
def test_points_from_xy():
# testing the top-level interface
# using DataFrame column
df = pd.DataFrame([{"x": x, "y": x, "z": x} for x in range(10)])
gs = [shapely.geometry.Point(x, x) for x in range(10)]
gsz = [shapely.geometry.Point(x, x, x) for x in range(10)]
geometry1 = geopandas.points_from_xy(df["x"], df["y"])
geometry2 = geopandas.points_from_xy(df["x"], df["y"], df["z"])
assert isinstance(geometry1, GeometryArray)
assert isinstance(geometry2, GeometryArray)
assert list(geometry1) == gs
assert list(geometry2) == gsz
# using Series or numpy arrays or lists
for s in [pd.Series(range(10)), np.arange(10), list(range(10))]:
geometry1 = geopandas.points_from_xy(s, s)
geometry2 = geopandas.points_from_xy(s, s, s)
assert isinstance(geometry1, GeometryArray)
assert isinstance(geometry2, GeometryArray)
assert list(geometry1) == gs
assert list(geometry2) == gsz
# using different lengths should throw error
arr_10 = np.arange(10)
arr_20 = np.arange(20)
with pytest.raises(ValueError):
geopandas.points_from_xy(x=arr_10, y=arr_20)
geopandas.points_from_xy(x=arr_10, y=arr_10, z=arr_20)
# Using incomplete arguments should throw error
with pytest.raises(TypeError):
geopandas.points_from_xy(x=s)
geopandas.points_from_xy(y=s)
geopandas.points_from_xy(z=s)
def test_from_shapely():
assert isinstance(T, GeometryArray)
assert equal_geometries(T, triangles)
def test_from_shapely_geo_interface():
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
@property
def __geo_interface__(self):
return {"type": "Point", "coordinates": (self.x, self.y)}
result = from_shapely([Point(1.0, 2.0), Point(3.0, 4.0)])
expected = from_shapely(
[shapely.geometry.Point(1.0, 2.0), shapely.geometry.Point(3.0, 4.0)]
)
assert all(v.equals(t) for v, t in zip(result, expected))
def test_from_wkb():
# list
L_wkb = [p.wkb for p in points_no_missing]
res = from_wkb(L_wkb)
assert isinstance(res, GeometryArray)
assert all(v.equals(t) for v, t in zip(res, points_no_missing))
# array
res = from_wkb(np.array(L_wkb, dtype=object))
assert isinstance(res, GeometryArray)
assert all(v.equals(t) for v, t in zip(res, points_no_missing))
# missing values
# TODO(shapely) does not support empty strings, np.nan, or pd.NA
missing_values = [None]
res = from_wkb(missing_values)
np.testing.assert_array_equal(res, np.full(len(missing_values), None))
# single MultiPolygon
multi_poly = shapely.geometry.MultiPolygon(
[shapely.geometry.box(0, 0, 1, 1), shapely.geometry.box(3, 3, 4, 4)]
)
res = from_wkb([multi_poly.wkb])
assert res[0] == multi_poly
def test_from_wkb_hex():
geometry_hex = ["0101000000CDCCCCCCCCCC1440CDCCCCCCCC0C4A40"]
res = from_wkb(geometry_hex)
assert isinstance(res, GeometryArray)
# array
res = from_wkb(np.array(geometry_hex, dtype=object))
assert isinstance(res, GeometryArray)
def test_from_wkb_on_invalid():
# Single point LineString hex WKB: invalid
invalid_wkb_hex = "01020000000100000000000000000008400000000000000840"
message = "point array must contain 0 or >1 elements"
with pytest.raises(Exception, match=message):
from_wkb([invalid_wkb_hex], on_invalid="raise")
with pytest.warns(Warning, match=message):
res = from_wkb([invalid_wkb_hex], on_invalid="warn")
assert res == [None]
with warnings.catch_warnings():
warnings.simplefilter("error")
res = from_wkb([invalid_wkb_hex], on_invalid="ignore")
assert res == [None]
def test_to_wkb():
P = from_shapely(points_no_missing)
res = to_wkb(P)
exp = np.array([p.wkb for p in points_no_missing], dtype=object)
assert isinstance(res, np.ndarray)
np.testing.assert_array_equal(res, exp)
res = to_wkb(P, hex=True)
exp = np.array([p.wkb_hex for p in points_no_missing], dtype=object)
assert isinstance(res, np.ndarray)
np.testing.assert_array_equal(res, exp)
# missing values
a = from_shapely([None, points_no_missing[0]])
res = to_wkb(a)
assert res[0] is None
@pytest.mark.parametrize("string_type", ["str", "bytes"])
def test_from_wkt(string_type):
if string_type == "str":
f = str
else:
def f(x):
return bytes(x, "utf8")
# list
L_wkt = [f(p.wkt) for p in points_no_missing]
res = from_wkt(L_wkt)
assert isinstance(res, GeometryArray)
tol = 0.5 * 10 ** (-6)
assert all(v.equals_exact(t, tolerance=tol) for v, t in zip(res, points_no_missing))
assert all(v.equals_exact(t, tolerance=tol) for v, t in zip(res, points_no_missing))
# array
res = from_wkt(np.array(L_wkt, dtype=object))
assert isinstance(res, GeometryArray)
assert all(v.equals_exact(t, tolerance=tol) for v, t in zip(res, points_no_missing))
# missing values
# TODO(shapely) does not support empty strings, np.nan, or pd.NA
missing_values = [None]
res = from_wkt(missing_values)
np.testing.assert_array_equal(res, np.full(len(missing_values), None))
# single MultiPolygon
multi_poly = shapely.geometry.MultiPolygon(
[shapely.geometry.box(0, 0, 1, 1), shapely.geometry.box(3, 3, 4, 4)]
)
res = from_wkt([f(multi_poly.wkt)])
assert res[0] == multi_poly
def test_from_wkt_on_invalid():
# Single point LineString WKT: invalid
invalid_wkt = "LINESTRING(0 0)"
message = "point array must contain 0 or >1 elements"
with pytest.raises(Exception, match=message):
from_wkt([invalid_wkt], on_invalid="raise")
with pytest.warns(Warning, match=message):
res = from_wkt([invalid_wkt], on_invalid="warn")
assert res == [None]
with warnings.catch_warnings():
warnings.simplefilter("error")
res = from_wkt([invalid_wkt], on_invalid="ignore")
assert res == [None]
def test_to_wkt():
P = from_shapely(points_no_missing)
res = to_wkt(P, rounding_precision=-1)
exp = np.array([p.wkt for p in points_no_missing], dtype=object)
assert isinstance(res, np.ndarray)
np.testing.assert_array_equal(res, exp)
# missing values
a = from_shapely([None, points_no_missing[0]])
res = to_wkt(a)
assert res[0] is None
def test_as_array():
arr = from_shapely(points_no_missing)
np_arr1 = np.asarray(arr)
np_arr2 = arr.to_numpy()
assert np_arr1[0] == arr[0]
np.testing.assert_array_equal(np_arr1, np_arr2)
@pytest.mark.parametrize(
"attr,args",
[
("contains", ()),
("covers", ()),
("crosses", ()),
("disjoint", ()),
("geom_equals", ()),
("intersects", ()),
("overlaps", ()),
("touches", ()),
("within", ()),
("geom_equals_exact", (0.1,)),
],
)
def test_predicates_vector_scalar(attr, args):
na_value = False
point = points[0]
tri = triangles[0]
for other in [point, tri, shapely.geometry.Polygon()]:
result = getattr(T, attr)(other, *args)
assert isinstance(result, np.ndarray)
assert result.dtype == bool
expected = [
(
getattr(tri, attr if "geom" not in attr else attr[5:])(other, *args)
if tri is not None
else na_value
)
for tri in triangles
]
assert result.tolist() == expected
# TODO other is missing
@pytest.mark.parametrize(
"attr,args",
[
("contains", ()),
("covers", ()),
("crosses", ()),
("disjoint", ()),
("geom_equals", ()),
("intersects", ()),
("overlaps", ()),
("touches", ()),
("within", ()),
("geom_equals_exact", (0.1,)),
],
)
def test_predicates_vector_vector(attr, args):
na_value = False
empty_value = True if attr == "disjoint" else False
A = (
[shapely.geometry.Polygon(), None]
+ [
shapely.geometry.Polygon(
[(random.random(), random.random()) for i in range(3)]
)
for _ in range(100)
]
+ [None]
)
B = [
shapely.geometry.Polygon([(random.random(), random.random()) for i in range(3)])
for _ in range(100)
] + [shapely.geometry.Polygon(), None, None]
vec_A = from_shapely(A)
vec_B = from_shapely(B)
result = getattr(vec_A, attr)(vec_B, *args)
assert isinstance(result, np.ndarray)
assert result.dtype == bool
expected = []
for a, b in zip(A, B):
if a is None or b is None:
expected.append(na_value)
elif a.is_empty or b.is_empty:
expected.append(empty_value)
else:
expected.append(
getattr(a, attr if "geom" not in attr else attr[5:])(b, *args)
)
assert result.tolist() == expected
@pytest.mark.parametrize(
"attr",
[
"boundary",
"centroid",
"convex_hull",
"envelope",
"exterior",
# 'interiors',
],
)
def test_unary_geo(attr):
na_value = None
result = getattr(T, attr)
expected = [getattr(t, attr) if t is not None else na_value for t in triangles]
assert equal_geometries(result, expected)
@pytest.mark.parametrize("attr", ["representative_point"])
def test_unary_geo_callable(attr):
na_value = None
result = getattr(T, attr)()
expected = [getattr(t, attr)() if t is not None else na_value for t in triangles]
assert equal_geometries(result, expected)
@pytest.mark.parametrize(
"attr", ["difference", "symmetric_difference", "union", "intersection"]
)
def test_binary_geo_vector(attr):
na_value = None
quads = [shapely.geometry.Polygon(), None]
while len(quads) < 12:
geom = shapely.geometry.Polygon(
[(random.random(), random.random()) for i in range(4)]
)
if geom.is_valid:
quads.append(geom)
Q = from_shapely(quads)
result = getattr(T, attr)(Q)
expected = [
getattr(t, attr)(q) if t is not None and q is not None else na_value
for t, q in zip(triangles, quads)
]
assert equal_geometries(result, expected)
@pytest.mark.parametrize(
"attr", ["difference", "symmetric_difference", "union", "intersection"]
)
def test_binary_geo_scalar(attr):
na_value = None
quads = []
while len(quads) < 1:
geom = shapely.geometry.Polygon(
[(random.random(), random.random()) for i in range(4)]
)
if geom.is_valid:
quads.append(geom)
q = quads[0]
for other in [q, shapely.geometry.Polygon()]:
result = getattr(T, attr)(other)
expected = [
getattr(t, attr)(other) if t is not None else na_value for t in triangles
]
assert equal_geometries(result, expected)
@pytest.mark.parametrize(
"attr",
[
"is_closed",
"is_valid",
"is_empty",
"is_simple",
"has_z",
# for is_ring we raise a warning about the value for Polygon changing
"is_ring",
],
)
def test_unary_predicates(attr):
na_value = False
if attr == "is_simple" and geos_version < (3, 8):
# poly.is_simple raises an error for empty polygon for GEOS < 3.8
with pytest.raises(Exception): # noqa: B017
T.is_simple
vals = triangle_no_missing
V = from_shapely(vals)
else:
vals = triangles
V = T
result = getattr(V, attr)
if attr == "is_ring":
expected = [
getattr(t, attr) if t is not None and t.exterior is not None else na_value
for t in vals
]
else:
expected = [getattr(t, attr) if t is not None else na_value for t in vals]
assert result.tolist() == expected
def test_is_ring():
g = [
shapely.geometry.LinearRing([(0, 0), (1, 1), (1, -1)]),
shapely.geometry.LineString([(0, 0), (1, 1), (1, -1)]),
shapely.geometry.LineString([(0, 0), (1, 1), (1, -1), (0, 0)]),
shapely.geometry.Polygon([(0, 0), (1, 1), (1, -1)]),
shapely.wkt.loads("POLYGON EMPTY"),
None,
]
expected = [True, False, True, False, False, False]
result = from_shapely(g).is_ring
assert result.tolist() == expected
@pytest.mark.parametrize("attr", ["area", "length"])
def test_unary_float(attr):
na_value = np.nan
result = getattr(T, attr)
assert isinstance(result, np.ndarray)
assert result.dtype == np.dtype("float64")
expected = [getattr(t, attr) if t is not None else na_value for t in triangles]
np.testing.assert_allclose(result, expected)
def test_geom_types():
cat = T.geom_type
# empty polygon has GeometryCollection type
assert list(cat) == ["Polygon"] * (len(T) - 1) + [None]
def test_geom_types_null_mixed():
geoms = [
shapely.geometry.Polygon([(0, 0), (0, 1), (1, 1)]),
None,
shapely.geometry.Point(0, 1),
]
G = from_shapely(geoms)
cat = G.geom_type
assert list(cat) == ["Polygon", None, "Point"]
def test_binary_distance():
attr = "distance"
na_value = np.nan
# also use nan for empty
# vector - vector
result = P[: len(T)].distance(T[::-1])
expected = [
(
getattr(p, attr)(t)
if not ((t is None or t.is_empty) or (p is None or p.is_empty))
else na_value
)
for t, p in zip(triangles[::-1], points)
]
np.testing.assert_allclose(result, expected)
# vector - scalar
p = points[0]
result = T.distance(p)
expected = [
getattr(t, attr)(p) if not (t is None or t.is_empty) else na_value
for t in triangles
]
np.testing.assert_allclose(result, expected)
# other is empty
result = T.distance(shapely.geometry.Polygon())
expected = [na_value] * len(T)
np.testing.assert_allclose(result, expected)
# TODO other is None
def test_binary_relate():
attr = "relate"
na_value = None
# vector - vector
result = getattr(P[: len(T)], attr)(T[::-1])
expected = [
getattr(p, attr)(t) if t is not None and p is not None else na_value
for t, p in zip(triangles[::-1], points)
]
assert list(result) == expected
# vector - scalar
p = points[0]
result = getattr(T, attr)(p)
expected = [getattr(t, attr)(p) if t is not None else na_value for t in triangles]
assert list(result) == expected
@pytest.mark.parametrize("normalized", [True, False])
def test_binary_project(normalized):
na_value = np.nan
lines = (
[None]
+ [
shapely.geometry.LineString(
[(random.random(), random.random()) for _ in range(2)]
)
for _ in range(len(P) - 2)
]
+ [None]
)
L = from_shapely(lines)
result = L.project(P, normalized=normalized)
expected = [
(
line.project(p, normalized=normalized)
if line is not None and p is not None
else na_value
)
for p, line in zip(points, lines)
]
np.testing.assert_allclose(result, expected)
@pytest.mark.parametrize("cap_style", [CAP_STYLE.round, CAP_STYLE.square])
@pytest.mark.parametrize("join_style", [JOIN_STYLE.round, JOIN_STYLE.bevel])
@pytest.mark.parametrize("resolution", [16, 25])
def test_buffer(resolution, cap_style, join_style):
na_value = None
expected = [
(
p.buffer(
0.1, resolution=resolution, cap_style=cap_style, join_style=join_style
)
if p is not None
else na_value
)
for p in points
]
result = P.buffer(
0.1, resolution=resolution, cap_style=cap_style, join_style=join_style
)
assert equal_geometries(expected, result)
dist = np.array([0.1] * len(P))
result = P.buffer(
dist, resolution=resolution, cap_style=cap_style, join_style=join_style
)
assert equal_geometries(expected, result)
def test_simplify():
triangles = [
shapely.geometry.Polygon(
[(random.random(), random.random()) for i in range(3)]
).buffer(10)
for _ in range(10)
]
T = from_shapely(triangles)
result = T.simplify(1)
expected = [t.simplify(1) for t in triangles]
assert all(a.equals(b) for a, b in zip(expected, result))
def test_unary_union():
geoms = [
shapely.geometry.Polygon([(0, 0), (0, 1), (1, 1)]),
shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1)]),
]
G = from_shapely(geoms)
with pytest.warns(
DeprecationWarning, match="The 'unary_union' attribute is deprecated"
):
u = G.unary_union()
expected = shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
assert u.equals(expected)
assert u.equals(G.union_all())
def test_union_all():
geoms = [
shapely.geometry.Polygon([(0, 0), (0, 1), (1, 1)]),
shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1)]),
]
G = from_shapely(geoms)
u = G.union_all()
expected = shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
assert u.equals(expected)
u_cov = G.union_all(method="coverage")
assert u_cov.equals(expected)
if GEOS_GE_312 and SHAPELY_GE_21:
u_disjoint = G.union_all(method="disjoint_subset")
assert u_disjoint.equals(expected)
with pytest.raises(ValueError, match="Method 'invalid' not recognized."):
G.union_all(method="invalid")
@pytest.mark.parametrize(
"grid_size, expected",
[
(
None,
MultiPolygon(
[
Polygon([(0, 0), (10, 0), (10, 10)]),
Polygon([(0, 0.4), (4.6, 5), (0, 5)]),
]
),
),
(1, Polygon([(0, 5), (5, 5), (10, 10), (10, 0), (0, 0)])),
],
)
def test_union_all_grid_size(grid_size, expected):
geoms = [
Polygon([(0, 0), (10, 0), (10, 10)]),
Polygon([(0, 0.4), (4.6, 5), (0, 5)]),
]
G = from_shapely(geoms)
u = G.union_all(grid_size=grid_size)
assert u.equals(expected)
def test_union_all_grid_size_error():
geoms = [
shapely.geometry.Polygon([(0, 0), (0, 1), (1, 1)]),
shapely.geometry.Polygon([(0, 0), (1, 0), (1, 1)]),
]
G = from_shapely(geoms)
with pytest.raises(
ValueError, match="grid_size is not supported for method 'coverage'"
):
G.union_all(method="coverage", grid_size=1)
@pytest.mark.parametrize(
"attr, arg",
[
("affine_transform", ([0, 1, 1, 0, 0, 0],)),
("translate", ()),
("rotate", (10,)),
("scale", ()),
("skew", ()),
],
)
def test_affinity_methods(attr, arg):
result = getattr(T, attr)(*arg)
expected = [
getattr(shapely.affinity, attr)(t, *arg) if not (t is None or t.is_empty) else t
for t in triangles
]
assert equal_geometries(result, expected)
# def test_coords():
# L = T.exterior.coords
# assert L == [tuple(t.exterior.coords) for t in triangles]
def test_coords_x_y():
na_value = np.nan
result = P.x
expected = [p.x if p is not None else na_value for p in points]
np.testing.assert_allclose(result, expected)
result = P.y
expected = [p.y if p is not None else na_value for p in points]
np.testing.assert_allclose(result, expected)
def test_bounds():
result = T.bounds
expected = [
t.bounds if not (t is None or t.is_empty) else [np.nan] * 4 for t in triangles
]
np.testing.assert_allclose(result, expected)
# additional check for one empty / missing
for geom in [None, shapely.geometry.Polygon()]:
E = from_shapely([geom])
result = E.bounds
assert result.ndim == 2
assert result.dtype == "float64"
np.testing.assert_allclose(result, np.array([[np.nan] * 4]))
# empty array (https://github.com/geopandas/geopandas/issues/1195)
E = from_shapely([])
result = E.bounds
assert result.shape == (0, 4)
assert result.dtype == "float64"
def test_total_bounds():
result = T.total_bounds
bounds = np.array(
[t.bounds if not (t is None or t.is_empty) else [np.nan] * 4 for t in triangles]
)
expected = np.array(
[
np.nanmin(bounds[:, 0]), # minx
np.nanmin(bounds[:, 1]), # miny
np.nanmax(bounds[:, 2]), # maxx
np.nanmax(bounds[:, 3]), # maxy
]
)
np.testing.assert_allclose(result, expected)
# additional check for empty array or one empty / missing
for geoms in [[], [None], [shapely.geometry.Polygon()]]:
E = from_shapely(geoms)
result = E.total_bounds
assert result.ndim == 1
assert result.dtype == "float64"
np.testing.assert_allclose(result, np.array([np.nan] * 4))
def test_getitem():
points = [shapely.geometry.Point(i, i) for i in range(10)]
P = from_shapely(points)
P2 = P[P.area > 0.3]
assert isinstance(P2, GeometryArray)
P3 = P[[1, 3, 5]]
assert len(P3) == 3
assert isinstance(P3, GeometryArray)
assert [p.x for p in P3] == [1, 3, 5]
P4 = P[1::2]
assert len(P4) == 5
assert isinstance(P3, GeometryArray)
assert [p.x for p in P4] == [1, 3, 5, 7, 9]
P5 = P[1]
assert isinstance(P5, shapely.geometry.Point)
assert P5.equals(points[1])
@pytest.mark.parametrize(
"item",
[
geopandas.GeoDataFrame(
geometry=[shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])]
),
geopandas.GeoSeries(
[shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])]
),
np.array([shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])]),
[shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)])],
shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
],
)
def test_setitem(item):
points = [shapely.geometry.Point(i, i) for i in range(10)]
P = from_shapely(points)
P[[0]] = item
assert isinstance(P[0], shapely.geometry.Polygon)
def test_equality_ops():
with pytest.raises(ValueError):
_ = P[:5] == P[:7]
a1 = from_shapely([points[1], points[2], points[3]])
a2 = from_shapely([points[1], points[0], points[3]])
res = a1 == a2
assert res.tolist() == [True, False, True]
res = a1 != a2
assert res.tolist() == [False, True, False]
# check the correct expansion of list-like geometry
multi_poly = shapely.geometry.MultiPolygon(
[shapely.geometry.box(0, 0, 1, 1), shapely.geometry.box(3, 3, 4, 4)]
)
a3 = from_shapely([points[1], points[2], points[3], multi_poly])
res = a3 == multi_poly
assert res.tolist() == [False, False, False, True]
def test_dir():
assert "contains" in dir(P)
assert "to_numpy" in dir(P)
def test_chaining():
# contains will give False for empty / missing
T = from_shapely(triangle_no_missing)
assert T.contains(T.centroid).all()
def test_pickle():
import pickle
T2 = pickle.loads(pickle.dumps(T))
# assert (T.data != T2.data).all()
assert T2[-1] is None
assert T2[-2].is_empty
assert T[:-2].geom_equals(T2[:-2]).all()
def test_raise_on_bad_sizes():
with pytest.raises(ValueError) as info:
T.contains(P)
assert "lengths" in str(info.value).lower()
assert "12" in str(info.value)
assert "21" in str(info.value)
def test_buffer_single_multipolygon():
# https://github.com/geopandas/geopandas/issues/1130
multi_poly = shapely.geometry.MultiPolygon(
[shapely.geometry.box(0, 0, 1, 1), shapely.geometry.box(3, 3, 4, 4)]
)
arr = from_shapely([multi_poly])
result = arr.buffer(1)
expected = [multi_poly.buffer(1)]
equal_geometries(result, expected)
result = arr.buffer(np.array([1]))
equal_geometries(result, expected)
def test_astype_multipolygon():
# https://github.com/geopandas/geopandas/issues/1145
multi_poly = shapely.geometry.MultiPolygon(
[shapely.geometry.box(0, 0, 1, 1), shapely.geometry.box(3, 3, 4, 4)]
)
arr = from_shapely([multi_poly])
result = arr.astype(str)
assert isinstance(result[0], str)
assert result[0] == multi_poly.wkt
# astype(object) does not convert to string
result = arr.astype(object)
assert isinstance(result[0], shapely.geometry.base.BaseGeometry)
# astype(np_dtype) honors the dtype
result = arr.astype(np.dtype("U10"))
assert result.dtype == np.dtype("U10")
assert result[0] == multi_poly.wkt[:10]
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not installed")
def test_check_crs():
t1 = T.copy()
t1.crs = 4326
assert _check_crs(t1, T) is False
assert _check_crs(t1, t1) is True
assert _check_crs(t1, T, allow_none=True) is True
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not installed")
def test_crs_mismatch_warn():
t1 = T.copy()
t2 = T.copy()
t1.crs = 4326
t2.crs = 3857
# two different CRS
with pytest.warns(UserWarning, match="CRS mismatch between the CRS"):
_crs_mismatch_warn(t1, t2)
# left None
with pytest.warns(UserWarning, match="CRS mismatch between the CRS"):
_crs_mismatch_warn(T, t2)
# right None
with pytest.warns(UserWarning, match="CRS mismatch between the CRS"):
_crs_mismatch_warn(t1, T)
@pytest.mark.skipif(HAS_PYPROJ, reason="pyproj installed")
def test_missing_pyproj():
with pytest.warns(UserWarning, match="Cannot set the CRS, falling back to None"):
t = T.copy()
t.crs = 4326
assert t.crs is None
@pytest.mark.parametrize("NA", [None, np.nan])
def test_isna(NA):
t1 = T.copy()
t1[0] = NA
assert t1[0] is None
def test_isna_pdNA():
t1 = T.copy()
t1[0] = pd.NA
assert t1[0] is None
def test_shift_has_crs():
t = T.copy()
t.crs = 4326
assert t.shift(1).crs == t.crs
assert t.shift(0).crs == t.crs
assert t.shift(-1).crs == t.crs
def test_unique_has_crs():
t = T.copy()
t.crs = 4326
assert t.unique().crs == t.crs
@pytest.mark.skipif(HAS_PYPROJ, reason="pyproj installed")
def test_to_crs_pyproj_error():
t = T.copy()
t.crs = 4326
with pytest.raises(
ImportError, match="The 'pyproj' package is required for to_crs"
):
t.to_crs(3857)
@pytest.mark.skipif(HAS_PYPROJ, reason="pyproj installed")
def test_estimate_utm_crs_pyproj_error():
with pytest.raises(
ImportError, match="The 'pyproj' package is required for estimate_utm_crs"
):
T.estimate_utm_crs()
class TestEstimateUtmCrs:
def setup_method(self):
self.esb = shapely.geometry.Point(-73.9847, 40.7484)
self.sol = shapely.geometry.Point(-74.0446, 40.6893)
self.landmarks = from_shapely([self.esb, self.sol], crs="epsg:4326")
def test_estimate_utm_crs__geographic(self):
pyproj = pytest.importorskip("pyproj")
assert self.landmarks.estimate_utm_crs() == pyproj.CRS("EPSG:32618")
assert self.landmarks.estimate_utm_crs("NAD83") == pyproj.CRS("EPSG:26918")
def test_estimate_utm_crs__projected(self):
pyproj = pytest.importorskip("pyproj")
assert self.landmarks.to_crs("EPSG:3857").estimate_utm_crs() == pyproj.CRS(
"EPSG:32618"
)
def test_estimate_utm_crs__antimeridian(self):
pyproj = pytest.importorskip("pyproj")
antimeridian = from_shapely(
[
shapely.geometry.Point(1722483.900174921, 5228058.6143420935),
shapely.geometry.Point(4624385.494808555, 8692574.544944234),
],
crs="EPSG:3851",
)
assert antimeridian.estimate_utm_crs() == pyproj.CRS("EPSG:32760")
def test_estimate_utm_crs__out_of_bounds(self):
pytest.importorskip("pyproj")
with pytest.raises(RuntimeError, match="Unable to determine UTM CRS"):
from_shapely(
[shapely.geometry.Polygon([(0, 90), (1, 90), (2, 90)])], crs="EPSG:4326"
).estimate_utm_crs()
def test_estimate_utm_crs__missing_crs(self):
pytest.importorskip("pyproj")
with pytest.raises(RuntimeError, match="crs must be set"):
from_shapely(
[shapely.geometry.Polygon([(0, 90), (1, 90), (2, 90)])]
).estimate_utm_crs()
def test_reduce_keepdims():
arr = geopandas.GeoDataFrame(geometry=[]).geometry.array
assert arr._reduce("all", keepdims=True) == np.array([True], dtype=object)
assert arr._reduce("all", keepdims=False)
|