Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions geo_extensions/transformations/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
def simplify_polygon(tolerance: float, preserve_topology: bool = True) -> Transformation:
"""CARTESIAN: Create a transformation that calls polygon.simplify.

:param tolerance: coordinates of the simplified geometry will be no more
than the tolerance distance from the original
:param preserve_topology: unless the topology preserving option is used, the
algorithm may produce self-intersecting or otherwise invalid geometries
:returns: a callable transformation using the passed parameters
"""

def simplify(polygon: Polygon) -> TransformationResult:
"""Perform a shapely simplify operation on the polygon."""
def simplify_polygon_transform(polygon: Polygon) -> TransformationResult:
# NOTE(reweeden): I have been unable to produce a situation where a
# polygon is simplified to a geometry other than Polygon.
yield cast(
Expand All @@ -44,7 +47,7 @@ def simplify(polygon: Polygon) -> TransformationResult:
),
)

return simplify
return simplify_polygon_transform


def split_polygon_on_antimeridian_ccw(polygon: Polygon) -> TransformationResult:
Expand Down Expand Up @@ -87,7 +90,7 @@ def split_polygon_on_antimeridian_fixed_size(
:returns: a callable transformation using the passed parameters
"""

def split(polygon: Polygon) -> TransformationResult:
def split_polygon_transform(polygon: Polygon) -> TransformationResult:
if not polygon_crosses_antimeridian_fixed_size(polygon, min_lon_extent):
yield polygon
return
Expand All @@ -98,7 +101,7 @@ def split(polygon: Polygon) -> TransformationResult:
for polygon in new_polygons:
yield _shift_polygon_back(polygon)

return split
return split_polygon_transform


def _shift_polygon(polygon: Polygon) -> Polygon:
Expand Down Expand Up @@ -152,7 +155,7 @@ def _split_polygon(
def _ignore_polygon(polygon: Polygon) -> bool:
min_lon, _, max_lon, _ = polygon.bounds
# We want to ignore any tiny slivers of polygons that might barely cross
# the antimeridian. For CMR, the polygons don't need to be that precice
# the antimeridian. For CMR, the polygons don't need to be that precise
# and we're rounding to 179.999 anyway. So realistically we don't want any
# polygons that are contained within the +/-0.001 degrees around the
# antimeridian. Due to possible floating point errors in the distance
Expand Down
17 changes: 13 additions & 4 deletions geo_extensions/transformations/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@


def reverse_polygon(polygon: Polygon) -> TransformationResult:
"""Perform a shapely reverse operation on the polygon."""
"""Perform a shapely reverse operation on the polygon.

:param polygon: the input polygon
:return: a generator yielding the transformed polygon
"""
yield polygon.reverse()


def drop_z_coordinate(polygon: Polygon) -> TransformationResult:
"""Drop the third element from each coordinate in the polygon."""
"""Drop the third element from each coordinate in the polygon.

:param polygon: the input polygon
:return: a generator yielding the transformed polygon
"""
yield Polygon(
shell=((x, y) for x, y, *_ in polygon.exterior.coords),
holes=[
Expand All @@ -30,10 +38,11 @@ def round_points(ndigits: SupportsIndex) -> Transformation:
"""Create a transformation that rounds polygon points to a given number of
digits.

:param ndigits: number of digits to round to
:returns: a callable transformation using the passed parameters
"""

def round_points_(polygon: Polygon) -> TransformationResult:
def round_points_transform(polygon: Polygon) -> TransformationResult:
"""Round the polygon's points."""
yield Polygon(
shell=(_round_coord(coord, ndigits) for coord in polygon.exterior.coords),
Expand All @@ -44,7 +53,7 @@ def round_points_(polygon: Polygon) -> TransformationResult:
],
)

return round_points_
return round_points_transform


def _round_coord(
Expand Down
7 changes: 2 additions & 5 deletions geo_extensions/transformations/geodetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def densify_polygon(tolerance_meters: float) -> Transformation:
if tolerance_meters <= 0:
raise ValueError("'tolerance_meters' must be greater than 0")

def densify(polygon: Polygon) -> TransformationResult:
"""Densify the polygon by adding additional points along the great
circle arcs between the existing points.
"""
def densify_polygon_transform(polygon: Polygon) -> TransformationResult:
yield Polygon(
shell=_densify_ring(polygon.exterior.coords, tolerance_meters),
holes=[
Expand All @@ -55,7 +52,7 @@ def densify(polygon: Polygon) -> TransformationResult:
],
)

return densify
return densify_polygon_transform


def _densify_ring(
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def data_path():

@pytest.fixture
def rectangle():
"""A rectanglular polygon"""
"""A rectangular polygon"""
polygon = Polygon(
[
(160.0, 60.0),
Expand All @@ -29,7 +29,7 @@ def rectangle():

@pytest.fixture
def centered_rectangle():
"""A rectanglular polygon centered at 0, 0"""
"""A rectangular polygon centered at 0, 0"""
polygon = Polygon(
[
(-30.0, 10.0),
Expand All @@ -47,7 +47,7 @@ def centered_rectangle():

@pytest.fixture
def antimeridian_centered_rectangle():
"""A rectanglular polygon centered over the antimeridian"""
"""A rectangular polygon centered over the antimeridian"""
polygon = Polygon(
[
(150.0, 10.0),
Expand All @@ -66,7 +66,7 @@ def antimeridian_centered_rectangle():
@pytest.fixture
def multi_crossing_polygon():
r"""A polygon that looks something like this, crossing back and forth
accross the IDL multiple times:
across the IDL multiple times:
--------
| /
| \
Expand Down
1 change: 0 additions & 1 deletion tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


def assume_valid_polygon(polygon):
(minx, _, maxx, _) = polygon.bounds
assume(polygon.area > 0.01)
simplified = shapely.remove_repeated_points(polygon.simplify(0))
assume(len(simplified.exterior.coords) > 4)
Expand Down