polysplit Module

This module contains the function polysplit, which splits a plygon into regions using k-medoids clustering and a distance metric based on the shortest distance between two points within the polygon.

class polysplit.VisibilityDistanceCache(polygon)

A cache for the shortest path distance between two points within a polygon. Speeds up computation by at least 50% compared to computing the full distance matrix at the beginning of the algorithm.

polysplit.euclidean_distance(point1: tuple, point2: tuple) float

Calculate the shortest path distance between two points.

Parameters:
  • point1 (shapely.geometry.Point) – The first point.

  • point2 (shapely.geometry.Point) – The second point.

Returns:

The shortest path distance between the points.

Return type:

float

polysplit.generate_points_within_polygon(polygon: Polygon, num_points: int) ndarray

Generate a set of points within a polygon.

Parameters:
  • polygon (shapely.geometry.Polygon) – The polygon to generate points within.

  • num_points (int) – The number of points to generate.

Returns:

A 2D array of shape (num_points, 2) containing the points.

Return type:

np.ndarray

polysplit.get_regions(polygon: Polygon, points: ndarray, labels: ndarray, k: int) list

Get the regions from the points and labels using Voronoi diagram on centroids.

Parameters:
  • points (np.ndarray) – The points.

  • labels (np.ndarray) – The labels.

  • k (int) – The number of regions.

Returns:

A list of shapely.geometry.Polygon objects.

Return type:

list

polysplit.main()

Main method for testing.

polysplit.plot_polygon_and_regions(polygon: Polygon, regions: list)

Plot the input polygon and the resulting regions.

Parameters:
  • polygon (shapely.geometry.Polygon) – The input polygon.

  • regions (list) – A list of shapely.geometry.Polygon objects.

polysplit.polysplit_main(polygon: Polygon, k: int = 2, num_points: int = 1000, plot=False) list

Split a polygon into k regions using k-medoids clustering and a distance metric based on the shortest distance between two points within the polygon.

Parameters:
  • polygon (shapely.geometry.Polygon) – The polygon to split.

  • k (int, optional) – The number of regions to split the polygon into, by default 2

  • num_points (int, optional) – The number of points to generate within the polygon, by default 1000

  • plot (bool, optional) – Whether to plot the results, by default False

Returns:

A list of shapely polygons, each of which is a region within the input polygon.

Return type:

list

polysplit.shapely_point_to_tuple(point: Point) tuple

Convert a shapely point to a tuple.

Parameters:

point (shapely.geometry.Point) – The point to convert.

Returns:

The tuple representation of the point.

Return type:

tuple

polysplit.visibility(polygon: Polygon, p1: tuple, p2: tuple)

Calculate the shortest path distance between two points within a polygon.

Parameters:
  • polygon (shapely.geometry.Polygon) – The polygon.

  • p1 (tuple) – The first point.

  • p2 (tuple) – The second point.

Returns:

  • tuple – The shortest path between the points.

  • float – The shortest path distance between the points.