shapelib.tools.lines.grouping package#

Submodules#

shapelib.tools.lines.grouping.core module#

Cluster nearby, roughly parallel lines into groups.

This is a port of optimal-interlines’ LineGroupSeparator. The clustering engine (_LineGrouper) is shared with shapelib.tools.lines.interlines; the public entry point is group_lines().

shapelib.tools.lines.grouping.core.group_lines(lines, distance_threshold=2.5)[source]#

Cluster nearby, roughly parallel lines into groups.

Lines are clustered by proximity: stray “matacão” lines (close to many others at once) are excluded from a cluster, and clusters whose lines are mutually close are merged. Exact-duplicate geometries are dropped before clustering. Filtering lines by talhão is the caller’s responsibility — pass only the lines belonging to a single talhão to group them in isolation.

Parameters:
  • lines (GeoDataFrame) – Line geometries to group. Must be in a projected CRS (metres), since distance_threshold is expressed in CRS units. MultiLineStrings are flattened to single LineStrings in the output.

  • distance_threshold (float) – Maximum spacing (in CRS units) for two lines to be treated as neighbors.

Returns:

One row per de-duplicated input line, carrying the original attribute columns plus:

  • group_id : integer cluster id (the seed line’s id within the call).

  • line_id : per-call unique integer id of the line.

Return type:

GeoDataFrame

Examples

>>> import shapelib
>>> lines = shapelib.read_file("lines.shp")
>>> lines.to_utm()
>>> grouped = group_lines(lines, distance_threshold=3.5)
>>> grouped["group_id"].nunique()
2

shapelib.tools.lines.grouping.models module#

Internal value object used by the line grouping algorithm.

_GroupedLine is a private implementation detail of shapelib.tools.lines.grouping (and is reused by the interline models). It mirrors the original optimal-interlines GroupedLine wrapper, but the global auto-incrementing id counter has been replaced by an explicit line_id supplied by the caller, so the object carries no shared mutable state.