Skip to content

Commit

Permalink
Test relaxed geometry parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Jan 29, 2024
1 parent 44c8811 commit 2c0644f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,5 +706,6 @@ def create_kml_geometry(
geometry=geom,
),
)
# this should be unreachable, but mypy doesn't know that
msg = f"Unsupported geometry type {type(geometry)}" # pragma: no cover
raise KMLWriteError(msg) # pragma: no cover
15 changes: 15 additions & 0 deletions tests/geometries/linearring_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ def test_from_string_invalid_coordinates_non_numerical(self) -> None:
"</kml:LinearRing>",
)

def test_mixed_2d_3d_coordinates_from_string_relaxed(self) -> None:
"""Test the from_string method with mixed 2D and 3D coordinates."""
linear_ring = cast(
LinearRing,
LinearRing.class_from_string(
'<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.0,1.0 "
"0.000000,0.000000 1.000000,2.000000,3.000000</kml:coordinates>"
"</kml:LinearRing>",
strict=False,
),
)

assert not linear_ring


class TestLinearRingLxml(Lxml, TestLinearRing):
"""Test with lxml."""
14 changes: 14 additions & 0 deletions tests/geometries/linestring_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ def test_mixed_2d_3d_coordinates_from_string(self) -> None:
"</LineString>",
)

def test_mixed_2d_3d_coordinates_from_string_relaxed(self) -> None:
line_string = LineString.class_from_string(
'<LineString xmlns="http://www.opengis.net/kml/2.2">'
"<extrude>1</extrude>"
"<tessellate>1</tessellate>"
"<coordinates>"
"-122.364383,37.824664 -122.364152,37.824322,0"
"</coordinates>"
"</LineString>",
strict=False,
)

assert not line_string

def test_empty_from_string(self) -> None:
"""Test the from_string method with an empty LineString."""
linestring = cast(
Expand Down
13 changes: 13 additions & 0 deletions tests/geometries/point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ def test_empty_from_string(self) -> None:
ns="",
)

def test_empty_from_string_relaxed(self) -> None:
"""Test that no error is raised when the geometry is empty and not strict."""
point = cast(
Point,
Point.class_from_string(
"<Point/>",
ns="",
strict=False,
),
)

assert point.geometry is None

def test_from_string_empty_coordinates(self) -> None:
with pytest.raises(
KMLParseError,
Expand Down
27 changes: 25 additions & 2 deletions tests/geometries/polygon_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,36 @@ def test_from_string_exterior_interior(self) -> None:
</kml:Polygon>
"""

polygon2 = cast(Polygon, Polygon.class_from_string(doc))
polygon = cast(Polygon, Polygon.class_from_string(doc))

assert polygon2.geometry == geo.Polygon(
assert polygon.geometry == geo.Polygon(
[(-1, -1), (2, -1), (2, 2), (-1, -1)],
[[(0, 0), (1, 0), (1, 1), (0, 0)]],
)

def test_from_string_exterior_mixed_interior_relaxed(self) -> None:
doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
<kml:outerBoundaryIs>
<kml:LinearRing>
<kml:coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000
-1.000000,-1.000000</kml:coordinates>
</kml:LinearRing>
</kml:outerBoundaryIs>
<kml:innerBoundaryIs>
<kml:LinearRing>
<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
0.000000,0.000000,1.00000</kml:coordinates>
</kml:LinearRing>
</kml:innerBoundaryIs>
</kml:Polygon>
"""

polygon = cast(Polygon, Polygon.class_from_string(doc, strict=False))

assert polygon.geometry == geo.Polygon(
((-1.0, -1.0), (2.0, -1.0), (2.0, 2.0), (-1.0, -1.0)),
)

def test_empty_polygon(self) -> None:
"""Test empty polygon."""
doc = (
Expand Down

0 comments on commit 2c0644f

Please sign in to comment.