Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MangaBoba committed Sep 7, 2023
1 parent 4f9c34c commit 7c90cfb
Show file tree
Hide file tree
Showing 55 changed files with 2,007 additions and 1,721 deletions.
1 change: 1 addition & 0 deletions gefest/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

125 changes: 89 additions & 36 deletions gefest/core/algs/geom/validation.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
"""
Here are defined general constraints on polygons by validation rules.
Validation is a checking on valid and invalid objects for further processing.
"""

from shapely.geometry import Point as GeomPoint, Polygon as GeomPolygon
from shapely.validation import explain_validity
from itertools import permutations

from gefest.core.structure.domain import Domain
from gefest.core.structure.polygon import Polygon
import numpy as np
from numpy.linalg import norm
from shapely.geometry import Point as GeomPoint
from shapely.geometry import Polygon as GeomPolygon
from shapely.validation import explain_validity

MIN_DIST = 15
from gefest.core.geometry import Polygon, Structure
from gefest.core.geometry.domain import Domain

min_dist_from_boundary = 1


def intersection(structure: 'Structure', domain: 'Domain') -> bool:
def intersection(structure: Structure, domain: Domain) -> bool:
"""The method for checking intersection between Polygons in the Structure
Args:
structure: the :obj:`Structure` that explore
Expand All @@ -36,31 +33,35 @@ def intersection(structure: 'Structure', domain: 'Domain') -> bool:
return False


def out_of_bound(structure: 'Structure', domain=None) -> bool:
def out_of_bound(structure: Structure, domain=None) -> bool:
"""The method for checking every polygon in the given :obj:`Structure`
on crossing borders of :obj:`Domain`
Args:
structure: the :obj:`Structure` that explore
domain: the :obj:`Domain` that determinates the main
parameters, this method requires ``allowed_area`` from :obj:`Domain`
Returns:
``True`` if at least one of the polygons in given :obj:`structure` crossing borders
of allowed area, otherwise - ``False``
on crossing borders of :obj:`Domain`
Args:
structure: the :obj:`Structure` that explore
domain: the :obj:`Domain` that determinates the main
parameters, this method requires ``allowed_area`` from :obj:`Domain`
Returns:
``True`` if at least one of the polygons in given :obj:`structure` crossing borders
of allowed area, otherwise - ``False``
"""
geom_poly_allowed = GeomPolygon([GeomPoint(pt[0], pt[1]) for pt in domain.allowed_area])
geom_poly_allowed = GeomPolygon(
[GeomPoint(pt.x, pt.y) for pt in domain.allowed_area],
)

for poly in structure.polygons:
for pt in poly.points:
geom_pt = GeomPoint(pt.x, pt.y)
if not geom_poly_allowed.contains(geom_pt) and not \
geom_poly_allowed.distance(geom_pt) < min_dist_from_boundary:
if (
not geom_poly_allowed.contains(geom_pt)
and not geom_poly_allowed.distance(geom_pt) < min_dist_from_boundary
):
return True

return False


def too_close(structure: 'Structure', domain: Domain) -> bool:
def too_close(structure: Structure, domain: Domain) -> bool:
"""Checking for too close location between every :obj:`Polygon` in the
given :obj:`Structure`
Args:
Expand All @@ -72,9 +73,16 @@ def too_close(structure: 'Structure', domain: Domain) -> bool:
distance set by :obj:`Domain`, otherwise - ``False``
"""
is_too_close = any(
[any([_pairwise_dist(poly_1, poly_2, domain) < domain.min_dist for
poly_2 in structure.polygons]) for poly_1
in structure.polygons])
[
any(
[
_pairwise_dist(poly_1, poly_2, domain) < domain.min_dist
for poly_2 in structure.polygons
],
)
for poly_1 in structure.polygons
],
)
return is_too_close


Expand All @@ -89,7 +97,7 @@ def _pairwise_dist(poly_1: Polygon, poly_2: Polygon, domain: Domain):
return domain.geometry.min_distance(poly_1, poly_2)


def self_intersection(structure: 'Structure') -> bool:
def self_intersection(structure: Structure, *args) -> bool:
"""The method indicates that any :obj:`Polygon` in the :obj:`Structure`
is self-intersected
Args:
Expand All @@ -98,12 +106,58 @@ def self_intersection(structure: 'Structure') -> bool:
``True`` if at least one of the polygons in the :obj:`Structure` is
self-intersected, otherwise - ``False``
"""
return any([len(poly.points) > 2 and
_forbidden_validity(explain_validity(GeomPolygon([GeomPoint(pt.x, pt.y) for pt in poly.points])))
for poly in structure.polygons])

return any(
[
len(poly.points) > 2
and _forbidden_validity(
explain_validity(
GeomPolygon([GeomPoint(pt.x, pt.y) for pt in poly.points]),
),
)
for poly in structure.polygons
],
)


def distance_between_points(structure: Structure, domain: Domain) -> bool:
"""The method indicates that any :obj:`Point` in each :obj:`Polygon` of :obj:`Structure`
is placed in correct distance by previous point
Args:
structure: the :obj:`Structure` that explore
Returns:
``True`` if any side of poly have incorrect lenght, otherwise - ``False``
"""
lenght = domain.dist_between_points
p1 = structure
check = []
for i in [[p.coords()[:2] for p in poly.points] for poly in p1.polygons]:
for ind, pnt in enumerate(i[1:]):
check.append(norm(np.array(pnt) - np.array(i[ind]), ord=1) < lenght)
if any(check):
print('Намутировал плохой полигон!, distance_between_points')
return any(check)


def distance_between_points_in_poly(poly: Polygon, domain: Domain) -> bool:
"""The method indicates that any :obj:`Point` in the :obj:`Polygon`
is placed in norm distance
Args:
poly: the :obj:`Polygon` that explore
Returns:
``True`` if side of poly have incorrect lenght, otherwise - ``False``
"""
lenght = domain.dist_between_points
check = []
for i in [p.coords()[:2] for p in poly.points]:
for ind, pnt in enumerate(i[1:]):
check.append(norm(np.array(pnt) - np.array(i[ind]), ord=1) < lenght)
if any(check):
print('Намутировал плохой полигон!, distance_between_points_in_poly')
return any(check)


def unclosed_poly(structure: 'Structure', domain: 'Domain') -> bool:
def unclosed_poly(structure: Structure, domain: Domain) -> bool:
"""Checking for equality of the first and the last points
Args:
structure: the :obj:`Structure` that explore
Expand All @@ -115,14 +169,13 @@ def unclosed_poly(structure: 'Structure', domain: 'Domain') -> bool:
otherwise - ``False``
"""

if domain.is_closed:
if domain.geometry.is_closed:
return any([poly.points[0] != poly.points[-1] for poly in structure.polygons])
else:
return False


def is_contain(structure: 'Structure',
domain: 'Domain') -> bool:
def is_contain(structure: Structure, domain: Domain) -> bool:
is_contains = []

try:
Expand Down
Loading

0 comments on commit 7c90cfb

Please sign in to comment.