geos icon indicating copy to clipboard operation
geos copied to clipboard

Union doesn't merge linestrings...

Open theroggy opened this issue 1 year ago • 3 comments

Is there a particular reason that the union operation doesn't merge multiple linestrings that are touching to a single linestring, but that you need to use linemerge for that?

Sample:

from shapely import LineString, MultiLineString, line_merge, union_all

geoms = [
	LineString([ (355041.15, 6688781.25, 0), (355040.9629213488, 6688781.437078651, 9.7) ]),
	LineString([ (355041.15, 6688781.25, 0), (354841.1500000001, 6688781.25, 0) ])
]

line_merge_result = line_merge(MultiLineString(geoms))
print(line_merge_result)
# LINESTRING Z (354841.1500000001 6688781.25 0, 355041.15 6688781.25 0, 355040.9629213488 6688781.437078651 9.7)

union_all_result = union_all(geoms)
print(union_all_result)
# MULTILINESTRING Z ((355041.15 6688781.25 0, 355040.9629213488 6688781.437078651 9.7), (355041.15 6688781.25 0, 354841.1500000001 6688781.25 0))

theroggy avatar Mar 26 '24 01:03 theroggy

Yes. It's because merging loses information, and takes slightly more time. So the design decision was to leave the results unmerged, and use a separate call to merge them if required.

dr-jts avatar Apr 03 '24 12:04 dr-jts

Yes. It's because merging loses information, and takes slightly more time. So the design decision was to leave the results unmerged, and use a separate call to merge them if required.

It is a bit counter-intuitive because for polygons union is the function to remove "redundant" lines between polygons... which also loses information... versus for lines union just collects all input to a MultiLineString...?

theroggy avatar Apr 03 '24 16:04 theroggy

union on LineStrings does more than just collect them. It nodes lines that cross, and eliminates duplicate sections of linework.

dr-jts avatar Apr 03 '24 23:04 dr-jts