Skip to main content

geometry

geometry

Functions to apply on a shapely.geometry

Functions

Name Description
basin_to_point Return a representative point for the basin; centroid if it is within (Multi)Polygon or polylabel if not.
connected_parts Split a (Multi)Polygon in the parts connected to the part that contains point, and the other parts.
drop_z Drop the z-coordinate of a geometry if it has.
is_sliver Whether polygons are on average at most tolerance wide, taken as their area divided by their perimeter.
line_between The part of a line between the points nearest to two points, connected to these points.
link Create a LineString geometry between two Point geometries, dropping z-coordinate if any
polygon_parts The polygons of a geometry that are on average wider than tolerance, so without slivers, see is_sliver.
snap_boundaries_to_other_line Snap the boundaries of line to the boundary or link/vertice of other_line if boundaries are within tolerance
sort_basins Sort basins in a MultiPolygon or list of Polygons on .area in ascending order (small to large).
split_basin Split a polygon with a line into two polygons.
split_line Split a line into two lines based on a point.
union_polygons The union of polygons, as shapely.union_all, but checked.
without_slivers The geometry made valid, as a MultiPolygon without the parts and holes that are slivers, see is_sliver.

basin_to_point

geometry.basin_to_point(basin_polygon, tolerance=None)

Return a representative point for the basin; centroid if it is within (Multi)Polygon or polylabel if not.

Parameters

basin_polygon : Polygon | MultiPolygon (Multi)Polygon to get representative point for tolerance: None | float Enforce a tolerance by which the point is to be within the polygon

Returns

Point Representative point for the basin

connected_parts

geometry.connected_parts(geometry, point, tolerance=0.001)

Split a (Multi)Polygon in the parts connected to the part that contains point, and the other parts.

Parts at most tolerance apart are connected, also through other parts. That joins parts that versions of GEOS leave touching or a sliver apart. The connected parts are returned as Polygon if there is one.

drop_z

geometry.drop_z(geometry)

Drop the z-coordinate of a geometry if it has.

Parameters

geometry : LineString | MultiPolygon | Point | Polygon Input geometry

Returns

LineString | Point | Polygon | MultiPolygon Output geometry

is_sliver

geometry.is_sliver(polygons, tolerance=SLIVER_WIDTH)

Whether polygons are on average at most tolerance wide, taken as their area divided by their perimeter.

line_between

geometry.line_between(line, start, end)

The part of a line between the points nearest to two points, connected to these points.

Parameters

Name Type Description Default
line LineString line, drawn from start to end required
start Point first point required
end Point last point required

Returns

Name Type Description
LineString line that starts at start and ends at end

polygon_parts

geometry.polygon_parts(geometry, tolerance=0.0)

The polygons of a geometry that are on average wider than tolerance, so without slivers, see is_sliver.

snap_boundaries_to_other_line

geometry.snap_boundaries_to_other_line(line, other_line, tolerance=0.1)

Snap the boundaries of line to the boundary or link/vertice of other_line if boundaries are within tolerance

sort_basins

geometry.sort_basins(basin_polygons)

Sort basins in a MultiPolygon or list of Polygons on .area in ascending order (small to large).

Parameters

basin_polygons : MultiPolygon | list[Polygon] MultiPolygon or list of polygons to be sorted

Returns

MultiPolygon | list[Polygon] MultiPolygon with sorted polygons

split_basin

geometry.split_basin(basin_polygon, line)

Split a polygon with a line into two polygons.

Credits to https://kuanbutts.com/2020/07/07/subdivide-polygon-with-linestring/

Parameters

basin_polygon : Polygon Polygon to cut into two line : LineString Cutline fro the Polygon

Returns

MultiPolygon Multipolygon with two polygons

split_line

geometry.split_line(line, point, *, tolerance=0.1)

Split a line into two lines based on a point.

As the built-in shapely.ops.split is not designed to do this splitting, its use should be avoided.

This line-splitting function uses the splitting point as part of the newly generated lines.

Args: line (LineString): line to be split point (Point): point to mark split-location tolerance (float, optional): tolerance of distance from point to line, defaults to 0.1.

Returns

tuple[LineString] | tuple[LineString, LineString]: a tuple of one or two LineString-objects

union_polygons

geometry.union_polygons(polygons, tolerance=0.001)

The union of polygons, as shapely.union_all, but checked.

GEOS can lose or add whole parts in a union with slivers. A union is wrong if it exceeds the area of the polygons together by more than tolerance times their perimeter, or is further than tolerance from a point inside a polygon that is not a sliver, on average wider than 100 times tolerance. Then the union is taken again with a fixed precision of tolerance, or if that fails, without slivers.

without_slivers

geometry.without_slivers(geometry, tolerance=SLIVER_WIDTH)

The geometry made valid, as a MultiPolygon without the parts and holes that are slivers, see is_sliver.

Removing a sliver hole fills it.