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
|
import numpy as np
import pytest
from numpy.testing import assert_array_equal
import shapely
from shapely import LinearRing, Polygon
from shapely.testing import assert_geometries_equal
from shapely.tests.common import empty_point, line_string, linear_ring, point, polygon
pnts = shapely.points
lstrs = shapely.linestrings
geom_coll = shapely.geometrycollections
@pytest.mark.parametrize(
"func", [shapely.points, shapely.linestrings, shapely.linearrings]
)
@pytest.mark.parametrize(
"coordinates",
[
np.empty((2,)), # not enough dimensions
np.empty((2, 4, 1)), # too many dimensions
np.empty((2, 4)), # wrong inner dimension size
None,
np.full((2, 2), "foo", dtype=object), # wrong type
],
)
def test_invalid_coordinates(func, coordinates):
with pytest.raises((TypeError, ValueError)):
func(coordinates, indices=[0, 1])
@pytest.mark.parametrize(
"func",
[
shapely.multipoints,
shapely.multilinestrings,
shapely.multipolygons,
shapely.geometrycollections,
],
)
@pytest.mark.parametrize(
"geometries", [np.array([1, 2], dtype=np.intp), None, np.array([[point]]), "hello"]
)
def test_invalid_geometries(func, geometries):
with pytest.raises((TypeError, ValueError)):
func(geometries, indices=[0, 1])
@pytest.mark.parametrize(
"func", [shapely.points, shapely.linestrings, shapely.linearrings]
)
@pytest.mark.parametrize("indices", [[point], " hello", [0, 1], [-1]])
def test_invalid_indices_simple(func, indices):
with pytest.raises((TypeError, ValueError)):
func([[0.2, 0.3]], indices=indices)
non_writeable = np.empty(3, dtype=object)
non_writeable.flags.writeable = False
@pytest.mark.parametrize(
"func", [shapely.points, shapely.linestrings, shapely.geometrycollections]
)
@pytest.mark.parametrize(
"out",
[
[None, None, None], # not an ndarray
np.empty(3), # wrong dtype
non_writeable, # not writeable
np.empty((3, 2), dtype=object), # too many dimensions
np.empty((), dtype=object), # too few dimensions
np.empty((2,), dtype=object), # too small
],
)
def test_invalid_out(func, out):
if func is shapely.points:
x = [[0.2, 0.3], [0.4, 0.5]]
indices = [0, 2]
elif func is shapely.linestrings:
x = [[1, 1], [2, 1], [2, 2], [3, 3], [3, 4], [4, 4]]
indices = [0, 0, 0, 2, 2, 2]
else:
x = [point, line_string]
indices = [0, 2]
with pytest.raises((TypeError, ValueError)):
func(x, indices=indices, out=out)
def test_points_invalid():
# attempt to construct a point with 2 coordinates
with pytest.raises(shapely.GEOSException):
shapely.points([[1, 1], [2, 2]], indices=[0, 0])
def test_points():
actual = shapely.points(
np.array([[2, 3], [2, 3]], dtype=float),
indices=np.array([0, 1], dtype=np.intp),
)
assert_geometries_equal(actual, [point, point])
def test_points_no_index_raises():
with pytest.raises(ValueError):
shapely.points(
np.array([[2, 3], [2, 3]], dtype=float),
indices=np.array([0, 2], dtype=np.intp),
)
@pytest.mark.parametrize(
"indices,expected",
[
([0, 1], [point, point, empty_point, None]),
([0, 3], [point, None, empty_point, point]),
([2, 3], [None, None, point, point]),
],
)
def test_points_out(indices, expected):
out = np.empty(4, dtype=object)
out[2] = empty_point
actual = shapely.points(
[[2, 3], [2, 3]],
indices=indices,
out=out,
)
assert_geometries_equal(out, expected)
assert actual is out
@pytest.mark.parametrize(
"coordinates,indices,expected",
[
([[1, 1], [2, 2]], [0, 0], [lstrs([[1, 1], [2, 2]])]),
([[1, 1, 1], [2, 2, 2]], [0, 0], [lstrs([[1, 1, 1], [2, 2, 2]])]),
(
[[1, 1], [2, 2], [2, 2], [3, 3]],
[0, 0, 1, 1],
[lstrs([[1, 1], [2, 2]]), lstrs([[2, 2], [3, 3]])],
),
],
)
def test_linestrings(coordinates, indices, expected):
actual = shapely.linestrings(
np.array(coordinates, dtype=float), indices=np.array(indices, dtype=np.intp)
)
assert_geometries_equal(actual, expected)
def test_linestrings_invalid():
# attempt to construct linestrings with 1 coordinate
with pytest.raises(shapely.GEOSException):
shapely.linestrings([[1, 1], [2, 2]], indices=[0, 1])
@pytest.mark.parametrize(
"indices,expected",
[
([0, 0, 0, 1, 1, 1], [line_string, line_string, empty_point, None]),
([0, 0, 0, 3, 3, 3], [line_string, None, empty_point, line_string]),
([2, 2, 2, 3, 3, 3], [None, None, line_string, line_string]),
],
)
def test_linestrings_out(indices, expected):
out = np.empty(4, dtype=object)
out[2] = empty_point
actual = shapely.linestrings(
[(0, 0), (1, 0), (1, 1), (0, 0), (1, 0), (1, 1)],
indices=indices,
out=out,
)
assert_geometries_equal(out, expected)
assert actual is out
@pytest.mark.parametrize(
"coordinates",
[
[[1, 1], [1, float("nan")], [2, 2]],
],
)
def test_linestrings_allow_nan(coordinates):
actual = shapely.linestrings(
np.array(coordinates, dtype=float),
indices=np.zeros(len(coordinates), dtype=np.intp),
)
assert_array_equal(shapely.get_coordinates(actual), coordinates)
@pytest.mark.parametrize(
"coordinates,indices,expected",
[
([[1, 1], [1, float("nan")], [2, 2]], [0, 0, 0], [lstrs([[1, 1], [2, 2]])]),
],
)
def test_linestrings_handle_nan_skip(coordinates, indices, expected):
actual = shapely.linestrings(
np.array(coordinates, dtype=float),
indices=np.array(indices, dtype=np.intp),
handle_nan="skip",
)
assert_geometries_equal(actual, expected)
def test_linestrings_handle_nan_skip_invalid():
# the NaN makes the linestring too short
with pytest.raises(shapely.GEOSException):
shapely.linestrings(
[[1, 1], [2, float("nan")]], indices=[0, 0], handle_nan="skip"
)
def test_linestrings_handle_nan_skip_only_nan():
actual = shapely.linestrings(
np.full((3, 2), fill_value=np.nan), indices=[0, 0, 0], handle_nan="skip"
)
assert actual[0].is_empty
def test_linestrings_handle_nan_error():
with pytest.raises(ValueError, match=".*NaN.*"):
shapely.linestrings(
[[0, 0], [float("nan"), 0], [1, 1]], indices=[0, 0, 0], handle_nan="error"
)
@pytest.mark.parametrize(
"coordinates", [([[1, 1], [2, 1], [2, 2], [1, 1]]), ([[1, 1], [2, 1], [2, 2]])]
)
def test_linearrings(coordinates):
actual = shapely.linearrings(
np.array(coordinates, dtype=np.float64),
indices=np.zeros(len(coordinates), dtype=np.intp),
)
assert_geometries_equal(actual, shapely.linearrings(coordinates))
@pytest.mark.parametrize(
"coordinates",
[
([[1, 1], [1, 1]]), # too short
([[1, np.nan], [2, 1], [2, 2], [1, 1]]), # starting with nan
],
)
def test_linearrings_invalid(coordinates):
with pytest.raises((shapely.GEOSException, ValueError)):
shapely.linearrings(coordinates, indices=np.zeros(len(coordinates)))
def test_linearrings_unclosed_all_coords_equal():
actual = shapely.linearrings([(0, 0), (0, 0), (0, 0)], indices=np.zeros(3))
assert_geometries_equal(actual, LinearRing([(0, 0), (0, 0), (0, 0), (0, 0)]))
@pytest.mark.parametrize(
"indices,expected",
[
([0, 0, 0, 0, 0], [linear_ring, None, None, empty_point]),
([1, 1, 1, 1, 1], [None, linear_ring, None, empty_point]),
([3, 3, 3, 3, 3], [None, None, None, linear_ring]),
],
)
def test_linearrings_out(indices, expected):
out = np.empty(4, dtype=object)
out[3] = empty_point
actual = shapely.linearrings(
[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)],
indices=indices,
out=out,
)
assert_geometries_equal(out, expected)
assert actual is out
@pytest.mark.parametrize("dim", [2, 3])
@pytest.mark.parametrize("order", ["C", "F"])
def test_linearrings_buffer(dim, order):
coords = np.random.randn(10, 4, dim)
coords1 = np.asarray(coords.reshape(10 * 4, dim), order=order)
indices1 = np.repeat(range(10), 4)
result1 = shapely.linearrings(coords1, indices=indices1)
# with manual closure -> can directly copy from buffer if C order
coords2 = np.hstack((coords, coords[:, [0], :]))
coords2 = np.asarray(coords2.reshape(10 * 5, dim), order=order)
indices2 = np.repeat(range(10), 5)
result2 = shapely.linearrings(coords2, indices=indices2)
assert_geometries_equal(result1, result2)
@pytest.mark.parametrize(
"coordinates",
[
[[1, 1], [2, 1], [2, float("nan")], [1, 1]],
],
)
def test_linearrings_allow_nan(coordinates):
actual = shapely.linearrings(
np.array(coordinates, dtype=float),
indices=np.zeros(len(coordinates), dtype=np.intp),
)
assert_array_equal(shapely.get_coordinates(actual), coordinates)
@pytest.mark.parametrize(
"coordinates",
[
[[1, 1], [2, 1], [2, 2], [2, float("nan")], [1, 1]],
[[1, 1], [2, 1], [2, float("nan")], [2, 2]],
[[1, 1], [2, 1], [2, 2], [1, 1], [2, float("nan")]],
[[2, float("nan")], [1, 1], [2, 1], [2, 2], [1, 1]],
[[1, 1], [2, 1], [2, 2], [2, float("nan")]],
[[2, float("nan")], [1, 1], [2, 1], [2, 2]],
],
)
def test_linearrings_handle_nan_skip(coordinates):
actual = shapely.linearrings(
np.array(coordinates, dtype=np.float64),
indices=np.zeros(len(coordinates), dtype=np.intp),
handle_nan="skip",
)
assert_geometries_equal(actual, shapely.linearrings(coordinates, handle_nan="skip"))
def test_linearrings_handle_nan_skip_invalid():
# the NaN makes the linearring too short
with pytest.raises(ValueError):
shapely.linearrings(
[[1, 1], [float("nan"), 1], [1, 1]], indices=[0, 0, 0], handle_nan="skip"
)
def test_linearrings_handle_nan_skip_only_nan():
actual = shapely.linearrings(
np.full((5, 2), fill_value=np.nan), indices=[0] * 5, handle_nan="skip"
)
assert actual[0].is_empty
def test_linearrings_handle_nan_error():
with pytest.raises(ValueError, match=".*NaN.*"):
shapely.linearrings(
[[1, 1], [2, 1], [2, 2], [2, float("nan")], [1, 1]],
indices=[0, 0, 0, 0, 0],
handle_nan="error",
)
hole_1 = shapely.linearrings([(0.2, 0.2), (0.2, 0.4), (0.4, 0.4)])
hole_2 = shapely.linearrings([(0.6, 0.6), (0.6, 0.8), (0.8, 0.8)])
poly = shapely.polygons(linear_ring)
poly_empty = Polygon()
poly_hole_1 = shapely.polygons(linear_ring, holes=[hole_1])
poly_hole_2 = shapely.polygons(linear_ring, holes=[hole_2])
poly_hole_1_2 = shapely.polygons(linear_ring, holes=[hole_1, hole_2])
@pytest.mark.parametrize(
"rings,indices,expected",
[
([linear_ring, linear_ring], [0, 1], [poly, poly]),
([None, linear_ring], [0, 1], [poly_empty, poly]),
([None, linear_ring, None, None], [0, 0, 1, 1], [poly, poly_empty]),
([linear_ring, hole_1, linear_ring], [0, 0, 1], [poly_hole_1, poly]),
([linear_ring, linear_ring, hole_1], [0, 1, 1], [poly, poly_hole_1]),
([None, linear_ring, linear_ring, hole_1], [0, 0, 1, 1], [poly, poly_hole_1]),
([linear_ring, None, linear_ring, hole_1], [0, 0, 1, 1], [poly, poly_hole_1]),
([linear_ring, None, linear_ring, hole_1], [0, 1, 1, 1], [poly, poly_hole_1]),
([linear_ring, linear_ring, None, hole_1], [0, 1, 1, 1], [poly, poly_hole_1]),
([linear_ring, linear_ring, hole_1, None], [0, 1, 1, 1], [poly, poly_hole_1]),
(
[linear_ring, hole_1, hole_2, linear_ring],
[0, 0, 0, 1],
[poly_hole_1_2, poly],
),
(
[linear_ring, hole_1, linear_ring, hole_2],
[0, 0, 1, 1],
[poly_hole_1, poly_hole_2],
),
(
[linear_ring, linear_ring, hole_1, hole_2],
[0, 1, 1, 1],
[poly, poly_hole_1_2],
),
(
[linear_ring, hole_1, None, hole_2, linear_ring],
[0, 0, 0, 0, 1],
[poly_hole_1_2, poly],
),
(
[linear_ring, hole_1, None, linear_ring, hole_2],
[0, 0, 0, 1, 1],
[poly_hole_1, poly_hole_2],
),
(
[linear_ring, hole_1, linear_ring, None, hole_2],
[0, 0, 1, 1, 1],
[poly_hole_1, poly_hole_2],
),
],
)
def test_polygons(rings, indices, expected):
actual = shapely.polygons(
np.array(rings, dtype=object), indices=np.array(indices, dtype=np.intp)
)
assert_geometries_equal(actual, expected)
@pytest.mark.parametrize(
"indices,expected",
[
([0, 1], [poly, poly, empty_point, None]),
([0, 3], [poly, None, empty_point, poly]),
([2, 3], [None, None, poly, poly]),
],
)
def test_polygons_out(indices, expected):
out = np.empty(4, dtype=object)
out[2] = empty_point
actual = shapely.polygons([linear_ring, linear_ring], indices=indices, out=out)
assert_geometries_equal(out, expected)
assert actual is out
@pytest.mark.parametrize(
"func",
[
shapely.polygons,
shapely.multipoints,
shapely.multilinestrings,
shapely.multipolygons,
shapely.geometrycollections,
],
)
@pytest.mark.parametrize("indices", [np.array([point]), " hello", [0, 1], [-1]])
def test_invalid_indices_collections(func, indices):
with pytest.raises((TypeError, ValueError)):
func([point], indices=indices)
@pytest.mark.parametrize(
"geometries,indices,expected",
[
([point, line_string], [0, 0], [geom_coll([point, line_string])]),
([point, line_string], [0, 1], [geom_coll([point]), geom_coll([line_string])]),
([point, None], [0, 0], [geom_coll([point])]),
([point, None], [0, 1], [geom_coll([point]), geom_coll([])]),
([None, point, None, None], [0, 0, 1, 1], [geom_coll([point]), geom_coll([])]),
([point, None, line_string], [0, 0, 0], [geom_coll([point, line_string])]),
],
)
def test_geometrycollections(geometries, indices, expected):
actual = shapely.geometrycollections(
np.array(geometries, dtype=object), indices=indices
)
assert_geometries_equal(actual, expected)
def test_geometrycollections_no_index_raises():
with pytest.raises(ValueError):
shapely.geometrycollections(
np.array([point, line_string], dtype=object), indices=[0, 2]
)
@pytest.mark.parametrize(
"indices,expected",
[
([0, 0], [geom_coll([point, line_string]), None, None, empty_point]),
([3, 3], [None, None, None, geom_coll([point, line_string])]),
],
)
def test_geometrycollections_out(indices, expected):
out = np.empty(4, dtype=object)
out[3] = empty_point
actual = shapely.geometrycollections([point, line_string], indices=indices, out=out)
assert_geometries_equal(out, expected)
assert actual is out
def test_multipoints():
actual = shapely.multipoints(
np.array([point], dtype=object), indices=np.zeros(1, dtype=np.intp)
)
assert_geometries_equal(actual, shapely.multipoints([point]))
def test_multilinestrings():
actual = shapely.multilinestrings(
np.array([line_string], dtype=object), indices=np.zeros(1, dtype=np.intp)
)
assert_geometries_equal(actual, shapely.multilinestrings([line_string]))
def test_multilinearrings():
actual = shapely.multilinestrings(
np.array([linear_ring], dtype=object), indices=np.zeros(1, dtype=np.intp)
)
assert_geometries_equal(actual, shapely.multilinestrings([linear_ring]))
def test_multipolygons():
actual = shapely.multipolygons(
np.array([polygon], dtype=object), indices=np.zeros(1, dtype=np.intp)
)
assert_geometries_equal(actual, shapely.multipolygons([polygon]))
@pytest.mark.parametrize(
"geometries,func",
[
([point], shapely.polygons),
([line_string], shapely.polygons),
([polygon], shapely.polygons),
([line_string], shapely.multipoints),
([polygon], shapely.multipoints),
([point], shapely.multilinestrings),
([polygon], shapely.multilinestrings),
([point], shapely.multipolygons),
([line_string], shapely.multipolygons),
],
)
def test_incompatible_types(geometries, func):
with pytest.raises(TypeError):
func(geometries, indices=[0])
def test_points_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `points` is deprecated"
):
shapely.points([[0, 1], [2, 3]], None, None, [0, 1])
def test_linestrings_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `linestrings` is deprecated"
):
shapely.linestrings([[0, 1], [2, 3], [4, 5], [6, 7]], None, None, [0, 0, 1, 1])
def test_linearrings_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `linearrings` is deprecated"
):
shapely.linearrings([[0, 1], [1, 1], [1, 0]], None, None, [0, 0, 0])
def test_polygons_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `polygons` is deprecated"
):
shapely.polygons([linear_ring, linear_ring], None, [0, 1])
def test_multipoints_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `multipoints` is deprecated"
):
shapely.multipoints([point, point], [0, 1])
def test_multilinestrings_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `multilinestrings` is deprecated"
):
shapely.multilinestrings([line_string, line_string], [0, 1])
def test_multipolygons_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `multipolygons` is deprecated"
):
shapely.multipolygons([polygon, polygon], [0, 1])
def test_geometrycollections_deprecate_positional():
with pytest.deprecated_call(
match="positional argument `indices` for `geometrycollections` is deprecated"
):
shapely.geometrycollections([point, polygon], [0, 1])
|