Skip to content

Commit

Permalink
desliver generalised
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Nov 8, 2024
1 parent 1b5f39b commit a225ba8
Show file tree
Hide file tree
Showing 14 changed files with 534 additions and 67 deletions.
50 changes: 25 additions & 25 deletions jord/geometric_analysis/center_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
from typing import Union, Tuple, List, Iterable
from typing import Iterable, List, Tuple, Union

import numpy
from numpy import array
Expand Down Expand Up @@ -40,28 +40,28 @@ def find_centerline(
int(min(ext_xy[1])),
)

vertices, ridges = _get_voronoi_vertices_and_ridges(
vertices, ridges = get_voronoi_vertices_and_ridges(
input_geometry, step_size, minx=_min_x, miny=_min_y
)

lines = []
for ridge in ridges:
if _ridge_is_finite(ridge):
starting_point = _create_point_with_restored_coordinates(
if ridge_is_finite(ridge):
starting_point = create_point_with_restored_coordinates(
x=vertices[ridge[0]][0],
y=vertices[ridge[0]][1],
_min_x=_min_x,
_min_y=_min_y,
)
ending_point = _create_point_with_restored_coordinates(
ending_point = create_point_with_restored_coordinates(
x=vertices[ridge[1]][0],
y=vertices[ridge[1]][1],
_min_x=_min_x,
_min_y=_min_y,
)
linestring = LineString((starting_point, ending_point))

if _linestring_is_within_input_geometry(linestring, input_geometry):
if linestring_is_within_input_geometry(linestring, input_geometry):
lines.append(linestring)

if len(lines) < 2:
Expand All @@ -70,94 +70,94 @@ def find_centerline(
return MultiLineString(lines=unary_union(lines))


def _get_voronoi_vertices_and_ridges(
def get_voronoi_vertices_and_ridges(
_input_geometry: BaseGeometry,
_step_size: float,
minx: float,
miny: float,
) -> Tuple[numpy.ndarray, List[List[int]]]:
borders = _get_densified_borders(_input_geometry, _step_size, minx, miny)
borders = densify_border(_input_geometry, _step_size, minx, miny)

voronoi_diagram = Voronoi(borders)
return voronoi_diagram.vertices, voronoi_diagram.ridge_vertices


def _ridge_is_finite(ridge: Iterable) -> bool:
def ridge_is_finite(ridge: Iterable) -> bool:
return -1 not in ridge


def _create_point_with_restored_coordinates(
def create_point_with_restored_coordinates(
x: float, y: float, _min_x: float, _min_y: float
) -> Tuple[float, float]:
return (x + _min_x, y + _min_y)


def _linestring_is_within_input_geometry(
def linestring_is_within_input_geometry(
linestring: LineString, input_geometry: BaseGeometry
) -> bool:
return linestring.within(input_geometry) and len(linestring.coords[0]) > 1


def _get_densified_borders(
def densify_border(
_input_geometry: BaseGeometry, _step_size, minx: float, miny: float
) -> numpy.ndarray:
polygons = iter_polygons(_input_geometry)
points = []
for polygon in polygons:
points += _get_interpolated_boundary(polygon.exterior, _step_size, minx, miny)
points += interpolated_boundary(polygon.exterior, _step_size, minx, miny)
if polygon_has_interior_rings(polygon):
for interior in polygon.interiors:
points += _get_interpolated_boundary(
points += interpolated_boundary(
interior, _step_size, minx=minx, miny=miny
)

return array(points)


def _get_interpolated_boundary(
def interpolated_boundary(
boundary: BaseGeometry, _step_size: float, minx: float, miny: float
) -> List[Tuple[float, float]]:
line = LineString(boundary)

return (
[_get_coordinates_of_first_point(line, minx, miny)]
+ _get_coordinates_of_interpolated_points(
[get_coordinates_of_first_point(line, minx, miny)]
+ get_coordinates_of_interpolated_points(
line, _step_size, min_x=minx, min_y=miny
)
+ [_get_coordinates_of_last_point(line, minx=minx, miny=miny)]
+ [get_coordinates_of_last_point(line, minx=minx, miny=miny)]
)


def _create_point_with_reduced_coordinates(
def create_point_with_reduced_coordinates(
x: float, y: float, _min_x: float, _min_y: float
) -> Tuple[float, float]:
return (x - _min_x, y - _min_y)


def _get_coordinates_of_first_point(
def get_coordinates_of_first_point(
linestring: LineString, minx: float, miny: float
) -> Tuple[float, float]:
return _create_point_with_reduced_coordinates(
return create_point_with_reduced_coordinates(
x=linestring.xy[0][0], y=linestring.xy[1][0], _min_x=minx, _min_y=miny
)


def _get_coordinates_of_last_point(
def get_coordinates_of_last_point(
linestring: LineString, minx: float, miny: float
) -> Tuple[float, float]:
return _create_point_with_reduced_coordinates(
return create_point_with_reduced_coordinates(
x=linestring.xy[0][-1], y=linestring.xy[1][-1], _min_x=minx, _min_y=miny
)


def _get_coordinates_of_interpolated_points(
def get_coordinates_of_interpolated_points(
linestring: LineString, _step_size: Number, min_x: float, min_y: float
) -> List[Tuple[float, float]]:
interpolation_distance = _step_size
intermediate_points = []
while interpolation_distance < linestring.length:
point = linestring.interpolate(interpolation_distance)
reduced_point = _create_point_with_reduced_coordinates(
reduced_point = create_point_with_reduced_coordinates(
x=point.x, y=point.y, _min_x=min_x, _min_y=min_y
)
intermediate_points.append(reduced_point)
Expand Down
13 changes: 9 additions & 4 deletions jord/geopandas_utilities/serialisation/well_known_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

from pathlib import Path

import pandas
import shapely.geometry.base
from shapely import wkb

__all__ = ["load_wkbs_from_csv", "csv_wkt_generator"]


def load_wkbs_from_csv(csv_file_path: Path, geometry_column: str = "Shape") -> wkb:
def load_wkbs_from_csv(
csv_file_path: Path, geometry_column: str = "Shape"
) -> pandas.DataFrame:
"""
Well-Known Text
"""
import pandas

return pandas.read_csv(str(csv_file_path))[geometry_column].apply(wkb.loads)


def csv_wkt_generator(csv_file_path: Path, geometry_column: str = "Shape") -> wkb:
def csv_wkt_generator(
csv_file_path: Path, geometry_column: str = "Shape"
) -> shapely.geometry.base.BaseGeometry:
"""
:param csv_file_path:
Expand All @@ -28,4 +33,4 @@ def csv_wkt_generator(csv_file_path: Path, geometry_column: str = "Shape") -> wk
for idx, g in pandas.read_csv(
str(csv_file_path), usecols=[geometry_column]
).iterrows():
yield wkb.loads(g)
yield wkb.loads(g) # g is pandas Series?
7 changes: 5 additions & 2 deletions jord/geopandas_utilities/serialisation/well_known_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Sequence

import pandas
import shapely.geometry.base
from pandas import DataFrame
from shapely import wkt

Expand Down Expand Up @@ -46,7 +47,9 @@ def load_wkts_from_csv(
return df


def csv_wkt_generator(csv_file_path: Path, geometry_column: str = "Shape") -> wkt:
def csv_wkt_generator(
csv_file_path: Path, geometry_column: str = "Shape"
) -> shapely.geometry.base.BaseGeometry:
"""
:param csv_file_path:
Expand All @@ -56,7 +59,7 @@ def csv_wkt_generator(csv_file_path: Path, geometry_column: str = "Shape") -> wk
for idx, g in pandas.read_csv(
str(csv_file_path), usecols=[geometry_column]
).iterrows():
yield wkt.loads(g)
yield wkt.loads(g) # g is a pandas Series?


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions jord/shapely_utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
from .mirroring import *
from .selection import *
from .uniformity import *
from .subdivision import *
Loading

0 comments on commit a225ba8

Please sign in to comment.