| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the Outer and Inner Boundary classes.""" |
| 18 |
|
| 19 |
from typing import Union |
| 20 |
|
| 21 |
import pygeoif.geometry as geo |
| 22 |
import pytest |
| 23 |
|
| 24 |
import fastkml |
| 25 |
from fastkml.exceptions import GeometryError |
| 26 |
from fastkml.geometry import Coordinates |
| 27 |
from fastkml.geometry import InnerBoundaryIs |
| 28 |
from fastkml.geometry import LinearRing |
| 29 |
from fastkml.geometry import OuterBoundaryIs |
| 30 |
from tests.base import Lxml |
| 31 |
from tests.base import StdLibrary |
| 32 |
|
| 33 |
|
| 34 |
class TestBoundaries(StdLibrary): |
| 35 |
def test_outer_boundary(self) -> None: |
| 36 |
"""Test the init method.""" |
| 37 |
coords = ((1, 2), (2, 0), (0, 0), (1, 2)) |
| 38 |
outer_boundary = OuterBoundaryIs( |
| 39 |
kml_geometry=LinearRing(kml_coordinates=Coordinates(coords=coords)), |
| 40 |
) |
| 41 |
|
| 42 |
assert outer_boundary.geometry == geo.LinearRing(coords) |
| 43 |
assert outer_boundary.to_string(prettyprint=False, precision=6).strip() == ( |
| 44 |
'<kml:outerBoundaryIs xmlns:kml="http://www.opengis.net/kml/2.2">' |
| 45 |
"<kml:LinearRing><kml:coordinates>" |
| 46 |
"1.000000,2.000000 2.000000,0.000000 0.000000,0.000000 1.000000,2.000000" |
| 47 |
"</kml:coordinates></kml:LinearRing></kml:outerBoundaryIs>" |
| 48 |
) |
| 49 |
|
| 50 |
def test_read_outer_boundary(self) -> None: |
| 51 |
"""Test the from_string method.""" |
| 52 |
outer_boundary = OuterBoundaryIs.from_string( |
| 53 |
'<kml:outerBoundaryIs xmlns:kml="http://www.opengis.net/kml/2.2">' |
| 54 |
"<kml:LinearRing>" |
| 55 |
"<kml:coordinates>1.0,4.0 2.0,0.0 0.0,0.0 1.0,4.0</kml:coordinates>" |
| 56 |
"</kml:LinearRing>" |
| 57 |
"</kml:outerBoundaryIs>", |
| 58 |
) |
| 59 |
|
| 60 |
assert outer_boundary.geometry == geo.LinearRing( |
| 61 |
((1, 4), (2, 0), (0, 0), (1, 4)), |
| 62 |
) |
| 63 |
|
| 64 |
def test_inner_boundary(self) -> None: |
| 65 |
"""Test the init method.""" |
| 66 |
coords = ((1, 2), (2, 0), (0, 0), (1, 2)) |
| 67 |
|
| 68 |
inner_boundary = InnerBoundaryIs( |
| 69 |
kml_geometry=LinearRing(kml_coordinates=Coordinates(coords=coords)), |
| 70 |
) |
| 71 |
|
| 72 |
assert inner_boundary.geometry == geo.LinearRing(coords) |
| 73 |
assert bool(inner_boundary) |
| 74 |
assert inner_boundary.to_string(prettyprint=False, precision=6).strip() == ( |
| 75 |
'<kml:innerBoundaryIs xmlns:kml="http://www.opengis.net/kml/2.2">' |
| 76 |
"<kml:LinearRing><kml:coordinates>" |
| 77 |
"1.000000,2.000000 2.000000,0.000000 0.000000,0.000000 1.000000,2.000000" |
| 78 |
"</kml:coordinates></kml:LinearRing></kml:innerBoundaryIs>" |
| 79 |
) |
| 80 |
|
| 81 |
def _test_boundary_geometry_error( |
| 82 |
self, |
| 83 |
boundary_class: Union[type[InnerBoundaryIs], type[OuterBoundaryIs]], |
| 84 |
) -> None: |
| 85 |
p = geo.LinearRing(((1, 2), (2, 0))) |
| 86 |
coords = ((1, 2), (2, 0), (0, 0), (1, 2)) |
| 87 |
|
| 88 |
with pytest.raises(GeometryError): |
| 89 |
boundary_class( |
| 90 |
kml_geometry=LinearRing(kml_coordinates=Coordinates(coords=coords)), |
| 91 |
geometry=p, |
| 92 |
) |
| 93 |
|
| 94 |
def test_outer_boundary_geometry_error(self) -> None: |
| 95 |
"""Test that OuterBoundaryIs raises GeometryError with invalid geometry.""" |
| 96 |
self._test_boundary_geometry_error(OuterBoundaryIs) |
| 97 |
|
| 98 |
def test_inner_boundary_geometry_error(self) -> None: |
| 99 |
"""Test that InnerBoundaryIs raises GeometryError with invalid geometry.""" |
| 100 |
self._test_boundary_geometry_error(InnerBoundaryIs) |
| 101 |
|
| 102 |
def test_read_inner_boundary_multiple_linestrings(self) -> None: |
| 103 |
""" |
| 104 |
Test the from_string method. |
| 105 |
|
| 106 |
When there are multiple LinearRings in the innerBoundaryIs element |
| 107 |
only the first one is used. |
| 108 |
""" |
| 109 |
inner_boundary = InnerBoundaryIs.from_string( |
| 110 |
'<kml:innerBoundaryIs xmlns:kml="http://www.opengis.net/kml/2.2">' |
| 111 |
"<kml:LinearRing>" |
| 112 |
"<kml:coordinates>1.0,4.0 2.0,0.0 0.0,0.0 1.0,4.0</kml:coordinates>" |
| 113 |
"</kml:LinearRing>" |
| 114 |
"<kml:LinearRing><kml:coordinates>" |
| 115 |
"-122.366212,37.818977,30 -122.365424,37.819294,30 " |
| 116 |
"-122.365704,37.819731,30 -122.366488,37.819402,30 -122.366212,37.818977,30" |
| 117 |
"</kml:coordinates></kml:LinearRing>" |
| 118 |
"</kml:innerBoundaryIs>", |
| 119 |
) |
| 120 |
|
| 121 |
assert inner_boundary.geometry == geo.LinearRing( |
| 122 |
((1, 4), (2, 0), (0, 0), (1, 4)), |
| 123 |
) |
| 124 |
|
| 125 |
def test_inner_boundary_repr_roundtrip(self) -> None: |
| 126 |
"""Test that repr(obj) can be eval'd back to obj.""" |
| 127 |
coords = ((1, 2), (2, 0), (0, 0), (1, 2)) |
| 128 |
inner_boundary = InnerBoundaryIs( |
| 129 |
kml_geometry=LinearRing(kml_coordinates=Coordinates(coords=coords)), |
| 130 |
) |
| 131 |
|
| 132 |
assert inner_boundary == eval( |
| 133 |
repr(inner_boundary), |
| 134 |
{}, |
| 135 |
{ |
| 136 |
"fastkml": fastkml, |
| 137 |
"LinearRing": geo.LinearRing, |
| 138 |
}, |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
class TestBoundariesLxml(Lxml, TestBoundaries): |
| 143 |
pass |