Bug report #14494

geometry.intersection() does not work properly in this case

Added by Jesus Irigoyen over 8 years ago. Updated over 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Geometry
Affected QGIS version:2.8.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:end of life
Crashes QGIS or corrupts data:No Copied to github as #:22469

Description

When we try to intersect two linestring as shown in attached figure, we get a QGis.WKBUnknown geometry just because LString1 and LString2 do not intersect in a singular geometry but in a collection of geometries. As shown in the attached figure, the geometryCollection should be compound by two LineStrings and a Point.
But if LString2 starts inside the first segment of LString1 we get two Points and a LString as the result of the geometryCollection which is wrong.

bug.pdf (6.4 KB) Jesus Irigoyen, 2016-03-15 04:40 AM

bug.svg (22.1 KB) Jesus Irigoyen, 2016-03-15 04:48 AM

bug1-1.svg (7.78 KB) Jesus Irigoyen, 2016-03-15 08:15 AM

bug1-2.svg (8.29 KB) Jesus Irigoyen, 2016-03-15 08:15 AM

nointersect.jpg - example with sample code. (46.5 KB) Maximilian Krambach, 2016-03-18 04:24 AM

History

#1 Updated by Jesus Irigoyen over 8 years ago

When we try to intersect two linestring as shown in attached picture, we get a QGis.WKBUnknown geometry just because LString1 and LString2 do not intersect in a singular geometry but in a collection of geometries. As shown in the attached figure, the geometryCollection should be compound by two LineStrings and a Point.
But if LString2 starts inside the first segment of LString1 we get two Points and a LString as the result of the geometryCollection which is wrong.

#2 Updated by Maximilian Krambach over 8 years ago

AFAIK, the intersection works at vertices only
(a "line" intersection is created if at least two vertices of Line A and two vertices of line B intersect).

Thus, in your example, the left line intersects only once, resulting in a "point intersection". If the red line starts at a vertex of the blue line, the intersection would be a line.

#3 Updated by Jesus Irigoyen over 8 years ago

Ok, it makes sense. After your explanation I have tried with the bug1-1.svg attached image and the result is: a Point and a Line where there should be two Points and a Line. However with file bug1-2.svg the result is a Point and two lines.

#4 Updated by Maximilian Krambach over 8 years ago

Okay, I was partially wrong. The intersection also works on non-vertices, but only if line1 crosses line2. If they only touch, it seems to be the "vertex only" issue. I attached an example with vertices marked, which results in no geometry instead of a multiline.

#5 Updated by Giovanni Manghi over 8 years ago

  • Status changed from Open to Feedback
  • Target version deleted (Future Release - High Priority)

better post the code as text, not in as an image, thanks.

#6 Updated by Giovanni Manghi over 8 years ago

  • Status changed from Feedback to Open

#7 Updated by Jukka Rahkonen over 8 years ago

I would suggest to use WKT instead of images for easier reproducing. For example:
line 1:
LINESTRING ( -40 340, 60 340, 100 260, 160 340, 300 340, 340 260, 360 380, 500 380 )
line 2:
LINESTRING ( -40 420, 0 340, 40 340, 100 380, 160 340, 200 380, 320 380, 360 420, 400 380, 460 380, 500 440 )

Expected result:
GEOMETRYCOLLECTION ( POINT ( 160 340 ), LINESTRING ( 0 340, 40 340 ), LINESTRING ( 400 380, 460 380 ))

#8 Updated by Giovanni Manghi over 7 years ago

  • Regression? set to No
  • Easy fix? set to No

#9 Updated by Loïc BARTOLETTI almost 6 years ago

  • Description updated (diff)
  • Status changed from Open to Feedback

Do you still have this problem, I just tested with QGIS 3.4 and GEOS 3.6:

> g1 = QgsGeometry.fromWkt("LINESTRING ( -40 340, 60 340, 100 260, 160 340, 300 340, 340 260, 360 380, 500 380 )")
> g2 = QgsGeometry.fromWkt("LINESTRING ( -40 420, 0 340, 40 340, 100 380, 160 340, 200 380, 320 380, 360 420, 400 380, 460 380, 500 440 )")
> g1.intersection(g2)
<QgsGeometry: GeometryCollection (Point (160 340),LineString (0 340, 40 340),LineString (400 380, 460 380))>

#10 Updated by Giovanni Manghi over 5 years ago

  • Resolution set to end of life
  • Status changed from Feedback to Closed

Also available in: Atom PDF