Skip to content

Commit

Permalink
one WKT to rule them all
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 29, 2023
1 parent 77305cd commit 8b9592b
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/LICENSE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

.. _LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.0.en.html
.. _LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
19 changes: 5 additions & 14 deletions pygeoif/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ def _wkt_coords(self) -> str:
@property
def _wkt_inset(self) -> str:
"""Return Z for 3 dimensional geometry or an empty string for 2 dimensions."""
return ""
if self.is_empty:
return f"{self._wkt_type} EMPTY"
if self.has_z:
return " Z "
return " "

@property
def _wkt_type(self) -> str:
Expand Down Expand Up @@ -307,11 +311,6 @@ def has_z(self) -> bool:
def _wkt_coords(self) -> str:
return " ".join(str(coordinate) for coordinate in self._geoms)

@property
def _wkt_inset(self) -> str:
"""Return Z for 3 dimensional geometry or an empty string for 2 dimensions."""
return " Z " if len(self._geoms) == 3 else " " # noqa: PLR2004

@property
def __geo_interface__(self) -> GeoInterface:
"""Return the geo interface."""
Expand Down Expand Up @@ -407,10 +406,6 @@ def maybe_valid(self) -> bool:
"""
return len({p.coords[0] for p in self._geoms}) > 1

@property
def _wkt_inset(self) -> str:
return self.geoms[0]._wkt_inset # noqa: SLF001

@property
def _wkt_coords(self) -> str:
return ", ".join(point._wkt_coords for point in self.geoms) # noqa: SLF001
Expand Down Expand Up @@ -648,10 +643,6 @@ def _wkt_coords(self) -> str:
)
return f"({ec}){ic}"

@property
def _wkt_inset(self) -> str:
return self.exterior._wkt_inset # noqa: SLF001

@property
def __geo_interface__(self) -> GeoInterface:
"""Return the geo interface."""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_empty() -> None:
def test_wkt_inset() -> None:
base_geo = geometry._Geometry()

assert base_geo._wkt_inset == ""
with pytest.raises(NotImplementedError, match="^Must be implemented by subclass$"):
assert base_geo._wkt_inset == ""


def test_wkt_coordinates() -> None:
Expand Down
18 changes: 9 additions & 9 deletions tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class TestWKT:
(30 20, 20 25, 20 15, 30 20)))""",
"""MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)),
((15 5, 40 10, 10 20, 5 10, 15 5)))""",
"MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))",
"GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))",
"MULTIPOLYGON (((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))",
"GEOMETRYCOLLECTION (POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))",
]

# these are valid WKTs but not supported
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_multipoint(self) -> None:
assert isinstance(p, geometry.MultiPoint)
assert next(iter(p.geoms)).x == 3.5
assert list(p.geoms)[1].y == 10.5
assert p.wkt == "MULTIPOINT(3.5 5.6, 4.8 10.5)"
assert p.wkt == "MULTIPOINT (3.5 5.6, 4.8 10.5)"
p = factories.from_wkt("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))")
assert isinstance(p, geometry.MultiPoint)
assert next(iter(p.geoms)).x == 10.0
Expand All @@ -221,14 +221,14 @@ def test_multipoint(self) -> None:

def test_multilinestring(self) -> None:
p = factories.from_wkt(
"MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))",
"MULTILINESTRING ((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))",
)

assert isinstance(p, geometry.MultiLineString)
assert next(iter(p.geoms)).coords == (((3, 4), (10, 50), (20, 25)))
assert list(p.geoms)[1].coords == (((-5, -8), (-10, -8), (-15, -4)))
assert (
p.wkt == "MULTILINESTRING((3 4, 10 50, "
p.wkt == "MULTILINESTRING ((3 4, 10 50, "
"20 25),(-5 -8, "
"-10 -8, -15 -4))"
)
Expand All @@ -241,12 +241,12 @@ def test_multilinestring_1(self) -> None:

assert isinstance(p, geometry.MultiLineString)
assert p.wkt == (
"MULTILINESTRING((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))"
"MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))"
)

def test_multipolygon(self) -> None:
p = factories.from_wkt(
"MULTIPOLYGON(((0 0,10 20,30 40,0 0),"
"MULTIPOLYGON (((0 0,10 20,30 40,0 0),"
"(1 1,2 2,3 3,1 1)),"
"((100 100,110 110,120 120,100 100)))",
)
Expand All @@ -273,7 +273,7 @@ def test_multipolygon(self) -> None:
(100.0, 100.0),
)
assert (
p.wkt == "MULTIPOLYGON(((0 0, 10 20, "
p.wkt == "MULTIPOLYGON (((0 0, 10 20, "
"30 40, 0 0),"
"(1 1, 2 2, 3 3, 1 1)),"
"((100 100, 110 110,"
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_geometrycollection(self) -> None:
assert len(list(gc.geoms)) == 2
assert isinstance(next(iter(gc.geoms)), geometry.Point)
assert isinstance(list(gc.geoms)[1], geometry.LineString)
assert gc.wkt == "GEOMETRYCOLLECTION(POINT (4 6), LINESTRING (4 6, 7 10))"
assert gc.wkt == "GEOMETRYCOLLECTION (POINT (4 6), LINESTRING (4 6, 7 10))"

def test_wkt_ok(self) -> None:
for wkt in self.wkt_ok:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_signed_area_crescent_ish() -> None:


def test_empty_hull() -> None:
assert not convex_hull([])
assert convex_hull([]) == []


def test_point() -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_geometrycollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_geo_wkt() -> None:
gc = geometry.GeometryCollection([poly1, poly2, p0, p1, ring, line])

assert gc.wkt == (
"GEOMETRYCOLLECTION"
"GEOMETRYCOLLECTION "
"(POLYGON ((0 0, 1 1, 1 0, 0 0)), "
"POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0),(1 0, 0.5 0.5, 1 1, 1.5 0.5, 1 0)), "
"POINT (0 0), POINT (-1 -1), "
Expand Down Expand Up @@ -271,7 +271,7 @@ def test_multipoint_wkt() -> None:
multipoint = geometry.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)])
gc = geometry.GeometryCollection([multipoint])

assert gc.wkt == "GEOMETRYCOLLECTION(MULTIPOINT(0 0, 1 1, 1 2, 2 2))"
assert gc.wkt == "GEOMETRYCOLLECTION (MULTIPOINT (0 0, 1 1, 1 2, 2 2))"


def test_multipoint_repr() -> None:
Expand Down Expand Up @@ -309,8 +309,8 @@ def test_nested_geometry_collection() -> None:
gc3 = geometry.GeometryCollection([gc2, poly1])

assert gc3.wkt == (
"GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(GEOMETRYCOLLECTION("
"POINT (0 0), MULTIPOINT(0 0, 1 1, 1 2, 2 2)), LINESTRING (0 0, 3 1)), "
"GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (GEOMETRYCOLLECTION ("
"POINT (0 0), MULTIPOINT (0 0, 1 1, 1 2, 2 2)), LINESTRING (0 0, 3 1)), "
"POLYGON ((0 0, 1 1, 1 0, 0 0)))"
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_multiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def test_wkt() -> None:
([(0, 0), (1, 1), (1, 2), (2, 2)], [[0.0, 0.0], [1.0, 2.0]]),
)

assert lines.wkt == "MULTILINESTRING((0 0, 1 1, 1 2, 2 2),(0.0 0.0, 1.0 2.0))"
assert lines.wkt == "MULTILINESTRING ((0 0, 1 1, 1 2, 2 2),(0.0 0.0, 1.0 2.0))"


def test_wkt_single_line() -> None:
lines = geometry.MultiLineString(([(0, 0), (1, 1), (1, 2), (2, 2)],))

assert lines.wkt == "MULTILINESTRING((0 0, 1 1, 1 2, 2 2))"
assert lines.wkt == "MULTILINESTRING ((0 0, 1 1, 1 2, 2 2))"


def test_repr() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multipoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_unique() -> None:
def test_wkt() -> None:
multipoint = geometry.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)])

assert multipoint.wkt == "MULTIPOINT(0 0, 1 1, 1 2, 2 2)"
assert multipoint.wkt == "MULTIPOINT (0 0, 1 1, 1 2, 2 2)"


def test_repr() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multipolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_wkt() -> None:
)

assert polys.wkt == (
"MULTIPOLYGON(((0.0 0.0, 0.0 1.0, 1.0 1.0, 1.0 0.0, 0.0 0.0),"
"MULTIPOLYGON (((0.0 0.0, 0.0 1.0, 1.0 1.0, 1.0 0.0, 0.0 0.0),"
"(0.1 0.1, 0.1 0.2, 0.2 0.2, 0.2 0.1, 0.1 0.1)),"
"((0.0 0.0, 0.0 1.0, 1.0 1.0, 1.0 0.0, 0.0 0.0)))"
)
Expand Down

0 comments on commit 8b9592b

Please sign in to comment.