Skip to content

geometry

calculate_distance(x1, y1, x2, y2)

Calculate the distance between two points.

Parameters:

Name Type Description Default
x1 float

The x-coordinate of the first point.

required
y1 float

The y-coordinate of the first point.

required
x2 float

The x-coordinate of the second point.

required
y2 float

The y-coordinate of the second point.

required

Returns:

Type Description
float

The distance between the two points.

Source code in src/layerforge/utils/geometry.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def calculate_distance(x1: float, y1: float, x2: float, y2: float) -> float:
    """Calculate the distance between two points.

    Parameters
    ----------
    x1 : float
        The x-coordinate of the first point.
    y1 : float
        The y-coordinate of the first point.
    x2 : float
        The x-coordinate of the second point.
    y2 : float
        The y-coordinate of the second point.

    Returns
    -------
    float
        The distance between the two points.
    """
    return float(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5)

polygon_parts(geometry)

Return every polygon with positive area inside geometry, however deeply nested.

shapely.get_parts descends only one level, so a GeometryCollection holding a MultiPolygon -- a real output of shapely.make_valid on some self-intersecting polygons, alongside the leftover line of the self-intersection -- would otherwise hide that MultiPolygon's own pieces behind a type that fails an isinstance(part, Polygon) check. This walks every level and keeps only what has area.

Source code in src/layerforge/utils/geometry.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def polygon_parts(geometry: BaseGeometry) -> list[Polygon]:
    """Return every polygon with positive area inside ``geometry``, however deeply nested.

    ``shapely.get_parts`` descends only one level, so a ``GeometryCollection`` holding a
    ``MultiPolygon`` -- a real output of ``shapely.make_valid`` on some self-intersecting
    polygons, alongside the leftover line of the self-intersection -- would otherwise hide
    that ``MultiPolygon``'s own pieces behind a type that fails an ``isinstance(part, Polygon)``
    check. This walks every level and keeps only what has area.
    """
    parts: list[Polygon] = []
    for part in shapely.get_parts(geometry):
        if isinstance(part, Polygon):
            if part.area > 0:
                parts.append(part)
        elif hasattr(part, "geoms"):
            parts.extend(polygon_parts(part))
    return parts