question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ops.split inconsistent behavior with LineStrings and MultiLineStrings

See original GitHub issue

Expected behavior and actual behavior & steps to reproduce the problem

Splitting a linestring A with an intersecting linestring B does not produce the expected output (returns A). Splitting A with a multilinestring containing B and C (C intersecting A as well) produces the desired output (3 segments). This seems inconsistent.

from shapely.ops import split
from shapely.geometry import LineString, MultiLineString

A = LineString([(0, 0), (10, 0)])
B = LineString([(5, 0), (5, 5)])
C = LineString([(1, -1), (1, 1)])
assert (split(A, B).wkt == "GEOMETRYCOLLECTION (LINESTRING (0 0, 10 0))")
# Does not split
# Expected: "GEOMETRYCOLLECTION (LINESTRING (0 0, 5 0), LINESTRING (5 0, 10 0))"

assert (A.intersection(B).wkt == "POINT (5 0)")
# Although A and B do intersect

assert (split(A, MultiLineString([B, C])).wkt ==
        "GEOMETRYCOLLECTION (LINESTRING (0 0, 1 0), LINESTRING (1 0, 5 0), LINESTRING (5 0, 10 0))")
# OK - A is split by both B and C - but A was not split by B in the previous example.

Operating system

Ubuntu 18.04.2 LTS

Shapely version and provenance

Python 3.6.7 - Shapely 1.6.4.post2 (installed from PyPI using pip)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Jeremiah-Englandcommented, Nov 9, 2020

I think that would work fine, yes. And the behavior makes sense to me.

For a bit, I thought we could filter the goems in a MultiLineString splitter for those crossing line and take the difference of only those. But that would not solve the case where a single LineString splitter hooks back to touch the line it crosses at another point. I think the only way to get consistent behavior here while using difference for the heavy lifting is to split touching linear geometries.

I suspect that maybe crosses was used so the function returned early when only the boundary of line touches the splitter. In fact this case is tested for. But our current case, where the boundary of splitter touches line, is not in the tests, and may have been a blind spot. However, I’m a Shapely native without much experience with other simple feature libraries. And I don’t know whether only splitting when crossing is standard behavior for linear geometries.

It was 5 years ago, but if @georgeouzou remembers maybe he could confirm or explain why crosses was used?

We can have our cake and eat it too if we want to return early in the case tested for, but carry on with splitting in this issue’s case. That would require using relate directly. But I doubt that there would be a large performance difference either way.

1reaction
sgilliescommented, Mar 3, 2021

@georgeouzou @Jeremiah-England thanks for communicating about the intent of the splitter!

Splitting on touch seems consistent with https://postgis.net/docs/ST_Split.html, which allows a point to split a line. I’m going to merge #1034 now and it will be in 1.8a1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shapely strange splits when splitting LineString and Polygon
The problem is that shapely's split function splits complex (i.e. self-intersecting) linestrings at their self-intersection points in addition ...
Read more >
Shapely Documentation - Read the Docs
The split() function in shapely.ops splits a geometry by another geometry. ... Shapely 1.8 does not yet change this inconsistent behaviour, ...
Read more >
split() throws a "TypeError: object of type 'LineString' has no ...
Expected behavior and actual behavior. This is supposed to conduct a split of a LineString from a MultiLineString. If the latter is exported ......
Read more >
Ruby Style Guide
However, know when to be inconsistent — sometimes style guide ... Use empty lines between method definitions and also to break up methods...
Read more >
API Changelog | Google Earth Engine
BACKWARD INCOMPATIBLE Changed Export logic in the Code Editor to no longer use the map bounds as a default region for the output....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found