Skip to content

cast

cast_multilinestring(geom)

Cast a geometry to a MultiLineString.

If the geometry is a LineString, it will be converted to a single-part MultiLineString. MultiLineStrings are returned unchanged.

Parameters:

Name Type Description Default
geom BaseGeometry

The geometry to cast.

required
Source code in src/rastr/gis/cast.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def cast_multilinestring(geom: BaseGeometry) -> MultiLineString:
    """Cast a geometry to a MultiLineString.

    If the geometry is a LineString, it will be converted to a single-part
    MultiLineString. MultiLineStrings are returned unchanged.

    Args:
        geom: The geometry to cast.
    """
    if geom.is_empty:
        return MultiLineString()

    if geom.geom_type == "MultiLineString":
        if not isinstance(geom, MultiLineString):
            raise AssertionError
        return geom
    elif geom.geom_type == "LineString":
        if not isinstance(geom, LineString):
            raise AssertionError
        return MultiLineString([geom])
    else:
        msg = f"Cannot cast geometry of type {geom.geom_type} to MultiLineString."
        raise TypeError(msg)