Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added logic to deal with MultiPolygon geometries #94

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
16 changes: 8 additions & 8 deletions koswat/calculations/reinforcement_layers_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from typing import List, Protocol, runtime_checkable
from typing import Protocol, runtime_checkable

from shapely.geometry import LineString, Polygon
from shapely.geometry import LineString, Polygon, MultiPolygon

from koswat.core.geometries.calc_library import get_polygon_coordinates
from koswat.dike.layers import KoswatLayerProtocol
Expand All @@ -14,18 +14,18 @@

@runtime_checkable
class ReinforcementLayerProtocol(KoswatLayerProtocol, Protocol):
new_layer_geometry: Polygon
new_layer_geometry: Polygon | MultiPolygon
new_layer_surface: LineString
old_layer_geometry: Polygon


class ReinforcementCoatingLayer(KoswatLayerProtocol):
class ReinforcementCoatingLayer(ReinforcementLayerProtocol):
material_type: KoswatMaterialType
outer_geometry: Polygon
material_geometry: Polygon
upper_points: LineString
old_layer_geometry: Polygon
new_layer_geometry: Polygon
new_layer_geometry: Polygon | MultiPolygon
new_layer_surface: LineString
removal_layer_geometry: Polygon

Expand Down Expand Up @@ -84,7 +84,7 @@ class ReinforcementBaseLayer(ReinforcementLayerProtocol):
outer_geometry: Polygon
material_geometry: Polygon
upper_points: LineString
new_layer_geometry: Polygon
new_layer_geometry: Polygon | MultiPolygon
new_layer_surface: LineString
old_layer_geometry: Polygon

Expand Down Expand Up @@ -115,7 +115,7 @@ def from_koswat_base_layer(

class ReinforcementLayersWrapper(KoswatLayersWrapperProtocol):
base_layer: ReinforcementBaseLayer
coating_layers: List[ReinforcementCoatingLayer]
coating_layers: list[ReinforcementCoatingLayer]

def __init__(self) -> None:
self.base_layer = None
Expand Down Expand Up @@ -143,7 +143,7 @@ def get_layer(self, material_type: KoswatMaterialType) -> ReinforcementCoatingLa
return _found_layer

@property
def layers(self) -> List[ReinforcementLayerProtocol]:
def layers(self) -> list[ReinforcementLayerProtocol]:
"""
All the stored layers being the `KoswatBaseLayer` the latest one in the collection.
Expand Down
21 changes: 11 additions & 10 deletions koswat/core/geometries/calc_library.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from typing import List, Union

from shapely import affinity, geometry, ops

Expand Down Expand Up @@ -36,13 +35,13 @@ def order_geometry_points(dike_polygon: geometry.Polygon) -> geometry.Polygon:


def as_unified_geometry(
source_geom: Union[geometry.Polygon, geometry.MultiPolygon]
source_geom: geometry.Polygon | geometry.MultiPolygon,
) -> geometry.Polygon:
"""
Ensures the calculated geometry is returned as a single polygon.
Args:
source_geom (Union[geometry.Polygon, geometry.MultiPolygon]): Calculated source geometry.
source_geom (geometry.Polygon | geometry.MultiPolygon): Calculated source geometry.
Returns:
geometry.Polygon: Unified resulting geometry with its points ordered (first one is the most-left x coordinate).
Expand Down Expand Up @@ -84,13 +83,13 @@ def get_relative_core_layer(


def get_polygon_coordinates(
pol_geometry: Union[geometry.Polygon, geometry.MultiPolygon]
pol_geometry: geometry.Polygon | geometry.MultiPolygon,
) -> geometry.LineString:
"""
Given a single or multi geometry returns the coordinates composing its outer layout.
Args:
pol_geometry (Union[geometry.Polygon, geometry.MultiPolygon]): Source geometry.
pol_geometry (geometry.Polygon | geometry.MultiPolygon): Source geometry.
Raises:
NotImplementedError: When the provided geometry is not yet supported.
Expand All @@ -100,7 +99,7 @@ def get_polygon_coordinates(
"""
if isinstance(pol_geometry, geometry.Polygon):
return geometry.LineString(pol_geometry.exterior.coords)
raise NotImplementedError(f"Geometry type {geometry.geom_type} not supported.")
raise NotImplementedError(f"Geometry type {pol_geometry.geom_type} not supported.")


def get_groundlevel_surface(pol_geometry: geometry.Polygon) -> geometry.LineString:
Expand Down Expand Up @@ -144,9 +143,9 @@ def _last_point_intersects() -> bool:

def get_normalized_polygon_difference(
left_geom: geometry.Polygon, right_geom: geometry.Polygon
) -> geometry.Polygon:
) -> geometry.Polygon | geometry.MultiPolygon:
"""
Given two polygons calculates the difference between them and removes any residual polygon due to minor precission errors.
Given two polygons calculates the difference between them and removes any residual polygon due to minor precision errors.
Args:
left_geom (geometry.Polygon): Base polygon from where to substract.
Expand All @@ -156,6 +155,8 @@ def get_normalized_polygon_difference(
geometry.Polygon: Resulting normalized substraction polygon.
"""
_result_geom = order_geometry_points(left_geom.difference(right_geom))
if isinstance(_result_geom, geometry.MultiPolygon):
return geometry.MultiPolygon(map(_get_normalized_polygon, _result_geom.geoms))
return _get_normalized_polygon(_result_geom)


Expand All @@ -179,7 +180,7 @@ def _get_single_polygon_surface_points(


def get_polygon_surface_points(
base_geometry: Union[geometry.Polygon, geometry.MultiPolygon]
base_geometry: geometry.Polygon | geometry.MultiPolygon,
) -> geometry.LineString:
"""
Gets all the points composing the upper surface of a 'dike' geometry.
Expand All @@ -200,7 +201,7 @@ def get_polygon_surface_points(
return base_geometry


def profile_points_to_polygon(points_list: List[geometry.Point]) -> geometry.Polygon:
def profile_points_to_polygon(points_list: list[geometry.Point]) -> geometry.Polygon:
"""
Transforms a list of points into a valid 'dike' polygon. When there is a difference in height between left and right side then we correct it in the x = 0 coordinate.
Expand Down
38 changes: 37 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pytest-cov = "^3.0.0"
coverage = "^6.4.4"
teamcity-messages = "^1.32"
opencv-python = "^4.8.1.78"
pytest-xdist = "^3.3.1"

[tool.poetry.group.dev.dependencies]
black = "^22.8.0"
Expand Down