AoiSampler#
- class AoiSampler[source]#
Bases:
object
Allows efficiently sampling points inside an AOI uniformly at random.
To achieve this, each polygon in the AOI is first partitioned into triangles (triangulation). Then, to sample a single point, we first sample a triangle at random with probability proportional to its area and then sample a point within that triangle uniformly at random.
Methods
__init__
(polygons)polygon_to_graph
(polygon)Given a polygon, return its graph representation.
sample
([n])Sample a random point within the AOI, using the following algorithm:
triangle_area
(vertices, simplices)Calculate area of each triangle specified by the simplices array using Heron's formula.
triangle_origin_and_basis
(vertices, simplices)For each triangle ABC, return point A, vector AB, and vector AC.
triangle_side_lengths
(vertices, simplices)Calculate lengths of all 3 sides of each triangle specified by the simplices array.
triangulate
(polygons)triangulate_polygon
(polygon)Triangulate polygon.
- polygon_to_graph(polygon: shapely.geometry.polygon.Polygon | shapely.geometry.polygon.LinearRing) tuple[numpy.ndarray, numpy.ndarray] [source]#
Given a polygon, return its graph representation.
- Parameters:
polygon (shapely.geometry.polygon.Polygon | shapely.geometry.polygon.LinearRing) – A polygon or polygon-exterior.
- Returns:
An (N, 2) array of vertices and an (N, 2) array of indices to vertices representing edges.
- Return type:
- sample(n: int = 1) ndarray [source]#
- Sample a random point within the AOI, using the following algorithm:
Randomly sample one triangle (ABC) with probability proportional
to its area. - Starting at A, travel a random distance along vectors AB and AC. - Return the final position.
- Parameters:
n (int) – Number of points to sample. Defaults to 1.
- Returns:
(n, 2) 2D coordinates of the sampled points.
- Return type:
np.ndarray
- triangle_area(vertices: ndarray, simplices: ndarray) ndarray [source]#
Calculate area of each triangle specified by the simplices array using Heron’s formula.
- triangle_origin_and_basis(vertices: ndarray, simplices: ndarray) tuple[numpy.ndarray, tuple[numpy.ndarray, numpy.ndarray]] [source]#
For each triangle ABC, return point A, vector AB, and vector AC.
- triangle_side_lengths(vertices: ndarray, simplices: ndarray) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray] [source]#
Calculate lengths of all 3 sides of each triangle specified by the simplices array.