Skip to content

Commit

Permalink
update geometry, make precission configurable, fixes #217
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Feb 6, 2024
1 parent 55bf2c5 commit cd46292
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
11 changes: 6 additions & 5 deletions fastkml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ def to_string(
verbosity: Verbosity = Verbosity.normal,
) -> str:
"""Return the KML Object as serialized xml."""
element = self.etree_element(
precision=precision,
verbosity=verbosity,
)
try:
return cast(
str,
config.etree.tostring( # type: ignore[attr-defined]
self.etree_element(
precision=precision,
verbosity=verbosity,
),
element,
encoding="UTF-8",
pretty_print=prettyprint,
).decode(
Expand All @@ -110,7 +111,7 @@ def to_string(
return cast(
str,
config.etree.tostring( # type: ignore[attr-defined]
self.etree_element(),
element,
encoding="UTF-8",
).decode(
"UTF-8",
Expand Down
6 changes: 4 additions & 2 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ def _etree_coordinates(
Element,
config.etree.Element(f"{self.ns}coordinates"), # type: ignore[attr-defined]
)
p = precision if precision is not None else 6
if len(coordinates[0]) == 2:
tuples = (f"{c[0]:f},{c[1]:f}" for c in coordinates)
tuples = (f"{c[0]:.{p}f},{c[1]:.{p}f}" for c in coordinates)
elif len(coordinates[0]) == 3:
tuples = (
f"{c[0]:f},{c[1]:f},{c[2]:f}" for c in coordinates # type: ignore[misc]
f"{c[0]:.{p}f},{c[1]:.{p}f},{c[2]:.{p}f}" # type: ignore[misc]
for c in coordinates
)
else:
msg = ( # type: ignore[unreachable]
Expand Down
48 changes: 47 additions & 1 deletion tests/geometries/point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_init(self) -> None:
assert point.altitude_mode is None
assert point.extrude is None

def test_to_string(self) -> None:
def test_to_string_2d(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2)

Expand All @@ -49,6 +49,52 @@ def test_to_string(self) -> None:
assert "Point" in point.to_string()
assert "coordinates>1.000000,2.000000</" in point.to_string()

def test_to_string_3d(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2, 3)

point = Point(geometry=p)

assert "Point" in point.to_string()
assert "coordinates>1.000000,2.000000,3.000000</" in point.to_string()

def test_to_string_2d_precision_0(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2)

point = Point(geometry=p)

assert "coordinates>1,2</" in point.to_string(precision=0)

def test_to_string_3d_precision_0(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2, 3)

point = Point(geometry=p)

assert "coordinates>1,2,3</" in point.to_string(precision=0)

def test_to_string_2d_precision_10(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2)

point = Point(geometry=p)

assert "coordinates>1.0000000000,2.0000000000</" in point.to_string(
precision=10,
)

def test_to_string_3d_precision_10(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2, 3)

point = Point(geometry=p)

assert (
"coordinates>1.0000000000,2.0000000000,3.0000000000</"
in point.to_string(precision=10)
)

def test_to_string_empty_geometry(self) -> None:
"""Test the to_string method."""
point = Point(geometry=geo.Point(None, None)) # type: ignore[arg-type]
Expand Down

0 comments on commit cd46292

Please sign in to comment.