Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasteuwen committed Aug 16, 2024
1 parent eb04b05 commit 32f710a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
Binary file added dlup/_geometry.cpython-310-darwin.so
Binary file not shown.
25 changes: 16 additions & 9 deletions dlup/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import abc
import dlup._geometry as _dg
from dlup.utils.imports import SHAPELY_AVAILABLE
import warnings


if SHAPELY_AVAILABLE:
from shapely.geometry import Polygon as ShapelyPolygon
Expand All @@ -23,7 +25,7 @@ def from_shapely(cls, shapely_geometry: ShapelyPoint | ShapelyPolygon) -> "_Base

def set_field(self, name: str, value: Any) -> None:
raise NotImplementedError

def get_field(self, name: str) -> None:
raise NotImplementedError

Expand Down Expand Up @@ -64,14 +66,13 @@ def __eq__(self, other: "_BaseGeometry") -> bool:
fields = self.fields
other_fields = other.fields

if fields != other_fields:
if sorted(fields) != sorted(other_fields):
return False

for field in fields:
if self.get_field(field) != other.get_field(field):
return False

if not self.wkt == other.wkt:
if self.wkt != other.wkt:
return False

return True
Expand Down Expand Up @@ -105,7 +106,12 @@ def __init__(self, *args, **kwargs):
_BaseGeometry.__init__(self)
if SHAPELY_AVAILABLE:
if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], ShapelyPolygon):
return self.from_shapely(args[0])

warnings.warn("Creating a Polygon from a Shapely Polygon is deprecated and will be removed dlup v1.0.0. Please use the `from_shapely` method instead.", UserWarning)
shapely_polygon = args[0]
exterior = list(shapely_polygon.exterior.coords)
interiors = [list(interior.coords) for interior in shapely_polygon.interiors]
args = (exterior, interiors)

# Ensure no new Polygon is created; just wrap the existing one
if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], _dg.Polygon):
Expand Down Expand Up @@ -155,7 +161,6 @@ def __setstate__(self, state):
self.set_field(key, value)

def __copy__(self):
import warnings
warnings.warn("Copying a Polygon currently creates a complete new object, without reference to the previous one, and is essentially the same as a deepcopy.")
new_copy = DlupPolygon(self)
return new_copy
Expand Down Expand Up @@ -201,7 +206,9 @@ def __init__(self, *args, **kwargs):
_BaseGeometry.__init__(self)
if SHAPELY_AVAILABLE:
if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], ShapelyPoint):
return self.from_shapely(args[0])
warnings.warn("Creating a Polygon from a Shapely Point is deprecated and will be removed dlup v1.0.0. Please use the `from_shapely` method instead.", UserWarning)
shapely_point = args[0]
args = (shapely_point.x, shapely_point.y)

# Ensure no new Point is created; just wrap the existing one
if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], _dg.Point):
Expand Down Expand Up @@ -231,7 +238,7 @@ def from_shapely(cls, shapely_point: "ShapelyPoint"):
raise ValueError(f"Expected a shapely.geometry.Point, but got {type(shapely_point)}")

return cls(shapely_point.x, shapely_point.y)

def to_shapely(self):
if not SHAPELY_AVAILABLE:
raise ImportError(
Expand All @@ -243,7 +250,7 @@ def to_shapely(self):
@property
def x(self):
return self.get_coordinates()[0]

@property
def y(self):
return self.get_coordinates()[1]
Expand Down
28 changes: 17 additions & 11 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,17 @@ def test_pickle_objects(self, object_to_pickle):
def test_repr(self):
polygon = DlupPolygon([(1, 1), (2, 3), (3, 4), (0, 0)], label="label", index=1, color=(1, 1, 1))
polygon.set_field("random", True)
assert repr(polygon) == "<DlupPolygon(color=(1, 1, 1), index=1, random=True, label=label) WKT='POLYGON((1 1,2 3,3 4,0 0,1 1))'>"
assert (
repr(polygon)
== "<DlupPolygon(color=(1, 1, 1), index=1, random=True, label=label) WKT='POLYGON((1 1,2 3,3 4,0 0,1 1))'>"
)

point = DlupPoint(1, 1, label="label", index=1, color=(1, 1, 1))
assert repr(point) == "<DlupPoint(color=(1, 1, 1), index=1, label=label) WKT='POINT(1 1)'>"

polygon = DlupPolygon([(1, 1) for _ in range(100)])
assert repr(polygon) == "<DlupPolygon() WKT='POLYGON((1 1,1 1,1 1,1 1,1 1,1...'>"


@pytest.mark.parametrize("original_object", polygons + points)
def test_deep_copy(self, original_object):
copied_object = copy.deepcopy(original_object)
Expand All @@ -108,8 +110,6 @@ def test_copy_polygon(self):
assert polygon.get_interiors() == polygon_copy.get_interiors()
assert polygon.get_exterior() == polygon_copy.get_exterior()



def test_container_add_object(self):
container = GeometryCollection()
container.add_polygon(polygons[0])
Expand Down Expand Up @@ -225,6 +225,9 @@ def test_from_shapely_polygon(self):
polygon_converted = DlupPolygon.from_shapely(shapely_polygon)
polygon_direct = DlupPolygon(exterior, interiors)

polygon_shapely_2 = DlupPolygon(shapely_polygon)
assert polygon_shapely_2 == polygon_converted

assert (
polygon_converted.get_exterior() == list(shapely_polygon.exterior.coords) == polygon_direct.get_exterior()
)
Expand All @@ -238,6 +241,9 @@ def test_from_shapely_polygon(self):
def test_from_shapely_point(self):
dlup_point = DlupPoint(1, 1)
shapely_point = ShapelyPoint(1, 1)
dlup_point2 = DlupPoint(shapely_point)

assert dlup_point2 == dlup_point

assert dlup_point == DlupPoint.from_shapely(shapely_point)
assert dlup_point.to_shapely() == shapely_point
Expand Down Expand Up @@ -287,6 +293,7 @@ def mock_shapely_available():
if shapely_available:
shapely_polygon = ShapelyPolygon([(0, 0), (0, 3), (3, 3), (3, 0)])
DlupPolygon.from_shapely(shapely_polygon)
DlupPolygon(shapely_polygon)
else:
with pytest.raises(ImportError):
DlupPolygon.from_shapely(None)
Expand Down Expand Up @@ -324,20 +331,19 @@ def test_cannot_subtract_geometries(self):
polygons[0] -= polygons[0]

def test_inequality(self):
polygon0 = copy.deepcopy(polygons[0])
polygon1 = copy.deepcopy(polygons[1])
polygon0 = DlupPolygon([(0, 0), (0, 3), (3, 3), (3, 0)], [])
polygon1 = DlupPolygon([(0, 0), (1, 3), (3, 3), (3, 0)], [])

polygon0.label = "test"
polygon1.label = "test"

assert polygon0 != polygon1

point0 = copy.deepcopy(points[0])
point1 = copy.deepcopy(points[1])
point0 = DlupPoint(0, 1)
point1 = DlupPoint(1, 1)

point0.color = (1, 2, 3)
point1.color = (1, 2, 3)

assert polygon0 != point1
assert point0 != point1


0 comments on commit 32f710a

Please sign in to comment.