forked from cnheider/jord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ | |
from .grouping import * | ||
from .mirroring import * | ||
from .selection import * | ||
from .uniformity import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import logging | ||
from typing import Optional | ||
|
||
import shapely | ||
from shapely import Polygon | ||
from shapely.geometry.base import BaseGeometry | ||
from shapely.validation import make_valid | ||
|
||
from .morphology import dilate | ||
from .rings import ensure_ccw_ring, ensure_cw_ring | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
__all__ = ["clean_shape", "ensure_cw_poly", "ensure_ccw_poly", "zero_buffer"] | ||
|
||
|
||
def clean_shape( | ||
shape: shapely.geometry.base.BaseGeometry, grid_size: Optional[float] = None | ||
) -> shapely.geometry.base.BaseGeometry: | ||
""" | ||
removes self-intersections and duplicate points | ||
:param grid_size: | ||
:param shape: The shape to cleaned | ||
:return: the cleaned shape | ||
""" | ||
|
||
if isinstance(shape, shapely.Polygon): | ||
shape = ensure_cw_poly(shape) | ||
|
||
if grid_size is not None: | ||
if not shape.is_valid: | ||
try: | ||
shape = make_valid(shape) | ||
except shapely.errors.GEOSException as e: | ||
logger.error(e) | ||
|
||
shape = shapely.set_precision( | ||
shape, | ||
grid_size, | ||
mode="keep_collapsed", | ||
) | ||
|
||
shape = zero_buffer(shape).simplify(0) | ||
|
||
if not shape.is_valid: | ||
try: | ||
shape = make_valid(shape) | ||
except shapely.errors.GEOSException as e: | ||
logger.error(e) | ||
|
||
return shape | ||
|
||
|
||
def ensure_ccw_poly(polygon: Polygon) -> Polygon: | ||
""" | ||
This function checks if the polygon is counter-clockwise if not it is reversed | ||
:param polygon: The polygon to check | ||
:return: Returns the polygon turned clockwise | ||
""" | ||
|
||
return Polygon( | ||
shell=ensure_ccw_ring(polygon.exterior), | ||
holes=[ensure_ccw_ring(hole) for hole in polygon.interiors], | ||
) | ||
|
||
|
||
def ensure_cw_poly(polygon: Polygon) -> Polygon: | ||
""" | ||
This function checks if the polygon is clockwise if not it is reversed | ||
:param polygon: The polygon to check | ||
:return: Returns the polygon turned clockwise | ||
""" | ||
|
||
return Polygon( | ||
shell=ensure_cw_ring(polygon.exterior), | ||
holes=[ensure_cw_ring(hole) for hole in polygon.interiors], | ||
) | ||
|
||
|
||
def zero_buffer( | ||
geom: BaseGeometry, | ||
) -> BaseGeometry: | ||
return dilate(geom, distance=0) |
Oops, something went wrong.