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
|
"""Polygons and Linear Rings"""
import numpy as np
import pytest
from shapely import LinearRing, LineString, Point, Polygon
from shapely.coords import CoordinateSequence
from shapely.errors import TopologicalError
from shapely.wkb import loads as load_wkb
def test_empty_linearring_coords():
assert LinearRing().coords[:] == []
def test_linearring_from_coordinate_sequence():
expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
assert ring.coords[:] == expected_coords
ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
assert ring.coords[:] == expected_coords
def test_linearring_from_points():
# From Points
expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing([Point(0.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0)])
assert ring.coords[:] == expected_coords
def test_linearring_from_closed_linestring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
line = LineString(coords)
ring = LinearRing(line)
assert len(ring.coords) == 4
assert ring.coords[:] == coords
assert ring.geom_type == "LinearRing"
def test_linearring_from_unclosed_linestring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
line = LineString(coords[:-1]) # Pass in unclosed line
ring = LinearRing(line)
assert len(ring.coords) == 4
assert ring.coords[:] == coords
assert ring.geom_type == "LinearRing"
def test_linearring_from_invalid():
coords = [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]
line = LineString(coords)
assert not line.is_valid
with pytest.raises(TopologicalError):
LinearRing(line)
def test_linearring_from_too_short_linestring():
# Creation of LinearRing request at least 3 coordinates (unclosed) or
# 4 coordinates (closed)
coords = [(0.0, 0.0), (1.0, 1.0)]
line = LineString(coords)
with pytest.raises(ValueError, match="requires at least 4 coordinates"):
LinearRing(line)
def test_linearring_from_linearring():
coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing(coords)
assert ring.coords[:] == coords
def test_linearring_from_generator():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
gen = (coord for coord in coords)
ring = LinearRing(gen)
assert ring.coords[:] == coords
def test_linearring_from_empty():
ring = LinearRing()
assert ring.is_empty
assert isinstance(ring.coords, CoordinateSequence)
assert ring.coords[:] == []
ring = LinearRing([])
assert ring.is_empty
assert isinstance(ring.coords, CoordinateSequence)
assert ring.coords[:] == []
def test_linearring_from_numpy():
# Construct from a numpy array
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing(np.array(coords))
assert ring.coords[:] == [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
def test_numpy_linearring_coords():
from numpy.testing import assert_array_equal
ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
ra = np.asarray(ring.coords)
expected = np.asarray([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)])
assert_array_equal(ra, expected)
def test_numpy_empty_linearring_coords():
ring = LinearRing()
assert np.asarray(ring.coords).shape == (0, 2)
def test_numpy_object_array():
geom = Polygon([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
ar = np.empty(1, object)
ar[:] = [geom]
assert ar[0] == geom
def test_polygon_from_coordinate_sequence():
coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
# Construct a polygon, exterior ring only
polygon = Polygon([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
assert polygon.exterior.coords[:] == coords
assert len(polygon.interiors) == 0
polygon = Polygon([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
assert polygon.exterior.coords[:] == coords
assert len(polygon.interiors) == 0
def test_polygon_from_coordinate_sequence_with_holes():
coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
# Interior rings (holes)
polygon = Polygon(coords, [[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25)]])
assert polygon.exterior.coords[:] == coords
assert len(polygon.interiors) == 1
assert len(polygon.interiors[0].coords) == 5
# Multiple interior rings with different length
coords = [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]
holes = [
[(1, 1), (2, 1), (2, 2), (1, 2), (1, 1)],
[(3, 3), (3, 4), (4, 5), (5, 4), (5, 3), (3, 3)],
]
polygon = Polygon(coords, holes)
assert polygon.exterior.coords[:] == coords
assert len(polygon.interiors) == 2
assert len(polygon.interiors[0].coords) == 5
assert len(polygon.interiors[1].coords) == 6
def test_polygon_from_linearring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
ring = LinearRing(coords)
polygon = Polygon(ring)
assert polygon.exterior.coords[:] == coords
assert len(polygon.interiors) == 0
# from shell and holes linearrings
shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0), (0.0, 0.0)])
holes = [
LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0), (60.0, 80.0)]),
LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0), (30.0, 10.0)]),
LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0), (90.0, 10.0)]),
]
polygon = Polygon(shell, holes)
assert polygon.exterior.coords[:] == shell.coords[:]
assert len(polygon.interiors) == 3
for i in range(3):
assert polygon.interiors[i].coords[:] == holes[i].coords[:]
def test_polygon_from_linestring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
line = LineString(coords)
polygon = Polygon(line)
assert polygon.exterior.coords[:] == coords
# from unclosed linestring
line = LineString(coords[:-1])
polygon = Polygon(line)
assert polygon.exterior.coords[:] == coords
def test_polygon_from_points():
polygon = Polygon([Point(0.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0)])
expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
assert polygon.exterior.coords[:] == expected_coords
def test_polygon_from_polygon():
coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
polygon = Polygon(coords, [[(0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25)]])
# Test from another Polygon
copy = Polygon(polygon)
assert len(copy.exterior.coords) == 5
assert len(copy.interiors) == 1
assert len(copy.interiors[0].coords) == 5
def test_polygon_from_invalid():
# Error handling
with pytest.raises(ValueError):
# A LinearRing must have at least 3 coordinate tuples
Polygon([[1, 2], [2, 3]])
def test_polygon_from_empty():
polygon = Polygon()
assert polygon.is_empty
assert polygon.exterior.coords[:] == []
polygon = Polygon([])
assert polygon.is_empty
assert polygon.exterior.coords[:] == []
def test_polygon_from_numpy():
a = np.array(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)))
polygon = Polygon(a)
assert len(polygon.exterior.coords) == 5
assert polygon.exterior.coords[:] == [
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
]
assert len(polygon.interiors) == 0
def test_polygon_from_generator():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
gen = (coord for coord in coords)
polygon = Polygon(gen)
assert polygon.exterior.coords[:] == coords
class TestPolygon:
def test_linearring(self):
# Initialization
# Linear rings won't usually be created by users, but by polygons
coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
ring = LinearRing(coords)
assert len(ring.coords) == 5
assert ring.coords[0] == ring.coords[4]
assert ring.coords[0] == ring.coords[-1]
assert ring.is_ring is True
def test_polygon(self):
coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
# Construct a polygon, exterior ring only
polygon = Polygon(coords)
assert len(polygon.exterior.coords) == 5
# Ring Access
assert isinstance(polygon.exterior, LinearRing)
ring = polygon.exterior
assert len(ring.coords) == 5
assert ring.coords[0] == ring.coords[4]
assert ring.coords[0] == (0.0, 0.0)
assert ring.is_ring is True
assert len(polygon.interiors) == 0
# Create a new polygon from WKB
data = polygon.wkb
polygon = None
ring = None
polygon = load_wkb(data)
ring = polygon.exterior
assert len(ring.coords) == 5
assert ring.coords[0] == ring.coords[4]
assert ring.coords[0] == (0.0, 0.0)
assert ring.is_ring is True
polygon = None
# Interior rings (holes)
polygon = Polygon(
coords, [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]
)
assert len(polygon.exterior.coords) == 5
assert len(polygon.interiors[0].coords) == 5
with pytest.raises(IndexError): # index out of range
polygon.interiors[1]
# Coordinate getter raises exceptions
with pytest.raises(NotImplementedError):
polygon.coords
# Geo interface
assert polygon.__geo_interface__ == {
"type": "Polygon",
"coordinates": (
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)),
),
}
def test_linearring_empty(self):
# Test Non-operability of Null rings
r_null = LinearRing()
assert r_null.wkt == "LINEARRING EMPTY"
assert r_null.length == 0.0
def test_dimensions(self):
# Background: see http://trac.gispython.org/lab/ticket/168
# http://lists.gispython.org/pipermail/community/2008-August/001859.html
coords = ((0.0, 0.0, 0.0), (0.0, 1.0, 0.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0))
polygon = Polygon(coords)
assert polygon._ndim == 3
gi = polygon.__geo_interface__
assert gi["coordinates"] == (
(
(0.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(1.0, 1.0, 0.0),
(1.0, 0.0, 0.0),
(0.0, 0.0, 0.0),
),
)
e = polygon.exterior
assert e._ndim == 3
gi = e.__geo_interface__
assert gi["coordinates"] == (
(0.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(1.0, 1.0, 0.0),
(1.0, 0.0, 0.0),
(0.0, 0.0, 0.0),
)
def test_attribute_chains(self):
# Attribute Chaining
# See also ticket #151.
p = Polygon([(0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)])
assert list(p.boundary.coords) == [
(0.0, 0.0),
(0.0, 1.0),
(-1.0, 1.0),
(-1.0, 0.0),
(0.0, 0.0),
]
ec = list(Point(0.0, 0.0).buffer(1.0, quad_segs=1).exterior.coords)
assert isinstance(ec, list) # TODO: this is a poor test
# Test chained access to interiors
p = Polygon(
[(0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)],
[[(-0.25, 0.25), (-0.25, 0.75), (-0.75, 0.75), (-0.75, 0.25)]],
)
assert p.area == 0.75
"""Not so much testing the exact values here, which are the
responsibility of the geometry engine (GEOS), but that we can get
chain functions and properties using anonymous references.
"""
assert list(p.interiors[0].coords) == [
(-0.25, 0.25),
(-0.25, 0.75),
(-0.75, 0.75),
(-0.75, 0.25),
(-0.25, 0.25),
]
xy = next(iter(p.interiors[0].buffer(1).exterior.coords))
assert len(xy) == 2
# Test multiple operators, boundary of a buffer
ec = list(p.buffer(1).boundary.coords)
assert isinstance(ec, list) # TODO: this is a poor test
def test_empty_equality(self):
# Test equals operator, including empty geometries
# see issue #338
point1 = Point(0, 0)
polygon1 = Polygon([(0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)])
polygon2 = Polygon([(0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)])
polygon_empty1 = Polygon()
polygon_empty2 = Polygon()
assert point1 != polygon1
assert polygon_empty1 == polygon_empty2
assert polygon1 != polygon_empty1
assert polygon1 == polygon2
assert polygon_empty1 is not None
def test_from_bounds(self):
xmin, ymin, xmax, ymax = -180, -90, 180, 90
coords = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]
assert Polygon(coords) == Polygon.from_bounds(xmin, ymin, xmax, ymax)
def test_empty_polygon_exterior(self):
p = Polygon()
assert p.exterior == LinearRing()
def test_linearring_immutable():
ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)])
with pytest.raises(AttributeError):
ring.coords = [(1.0, 1.0), (2.0, 2.0), (1.0, 2.0)]
with pytest.raises(TypeError):
ring.coords[0] = (1.0, 1.0)
class TestLinearRingGetItem:
def test_index_linearring(self):
shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0), (0.0, 0.0)])
holes = [
LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0), (60.0, 80.0)]),
LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0), (30.0, 10.0)]),
LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0), (90.0, 10.0)]),
]
g = Polygon(shell, holes)
for i in range(-3, 3):
assert g.interiors[i].equals(holes[i])
with pytest.raises(IndexError):
g.interiors[3]
with pytest.raises(IndexError):
g.interiors[-4]
def test_index_linearring_misc(self):
g = Polygon() # empty
with pytest.raises(IndexError):
g.interiors[0]
with pytest.raises(TypeError):
g.interiors[0.0]
def test_slice_linearring(self):
shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0), (0.0, 0.0)])
holes = [
LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0), (60.0, 80.0)]),
LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0), (30.0, 10.0)]),
LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0), (90.0, 10.0)]),
]
g = Polygon(shell, holes)
t = [a.equals(b) for (a, b) in zip(g.interiors[1:], holes[1:])]
assert all(t)
t = [a.equals(b) for (a, b) in zip(g.interiors[:-1], holes[:-1])]
assert all(t)
t = [a.equals(b) for (a, b) in zip(g.interiors[::-1], holes[::-1])]
assert all(t)
t = [a.equals(b) for (a, b) in zip(g.interiors[::2], holes[::2])]
assert all(t)
t = [a.equals(b) for (a, b) in zip(g.interiors[:3], holes[:3])]
assert all(t)
assert g.interiors[3:] == holes[3:] == []
|