8

I want to find intersections in the same layer like picture below. All lines are separate features in the same layer.

How can I find these intersections in QGIS?

enter image description here

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
jin
  • 169
  • 6
  • Excuse me, but what do you mean by "to find", selecting or extracting? And what do you want "to find", e.g. points where intersections exist? What have you tried by so far? Are you aware of the 'Geometry Checker Plugin' with Self intersections? – Taras Nov 25 '20 at 06:10
  • 1
    The use of "self-intersection" in the title is misleading. Self-intersection means that a feature intersects itself but you seem to be interested in intersections between different features. – user30184 Nov 25 '20 at 07:19
  • I doubt if the title is misleading, because in the question conent the OP mentioned: "in same shapefile (same layer)." So, for me it sounds like "self-intersection", is not it? @user30184, please correct if I am wrong. – Taras Nov 25 '20 at 07:31
  • 1
    The points in the image seem to be at the intersections of different lines. They are on the same layer but they are not self-intersections like in this question https://gis.stackexchange.com/questions/158392/how-to-edit-self-intersecting-polyline-in-arcgis-for-desktop. OP may want to find also the self-intersections but obviously not only those. – user30184 Nov 25 '20 at 07:42
  • Thank you for clarity @user30184, now I agree with you. – Taras Nov 25 '20 at 08:02
  • I meant that I want to extract intersecting points. And the lines are different objects even though they're in the same layer.. because when I opened the layer's attributes table and chose the object, there were many individual lines like the green, blue, orange line in the picture above. – jin Nov 25 '20 at 08:19
  • @user30184, just a small idea. Can this situation be called as a "self-touch"? – Taras Nov 27 '20 at 09:04

3 Answers3

11

Try using "Line intersections" geoalgorithm that creates points where lines intersect.

intersections

Note that for 'Input layer' and 'Intersect layer' the same line layer was chosen.

If lines are disconnected (see image below), some additional geometry adjustment is required, i.e. snapping.

issue

For snapping geometries in a layer to the geometries within the same layer apply "Snap geometries to layer".

snapping

And after apply the "Line intersections" geoalgorithm.

Taras
  • 32,823
  • 4
  • 66
  • 137
11

Using the following script you can get the result you expect. But firstly, use "Vector geometry > Multipart to singleparts" tool if the line pairs of the same color are the same features. Because the script uses the nearest point between geometries.

Set layer name. You also need to set epsilon (eps) value to assume two lines intersect or touch. Because small changes in decimal in coordinates effect the result. Therefore, some tools and functions related a geometry contain a threshold.

I assume that data is in projected coordinate system. If it is geographic, decrease eps.

# specify minimum distance to assume that lines intersect/touch
eps = 0.01 # 0.01 meter = 1 cm

line_lyr = QgsProject.instance().mapLayersByName('LINE_LAYER_NAME')[0] crs = line_lyr.crs().authid()

make new point layer

point_lyr = QgsVectorLayer("Point?crs=" + crs, "points", "memory") point_lyr.startEditing()

for line1 in line_lyr.getFeatures(): geom1 = line1.geometry()

for line2 in line_lyr.getFeatures():
    geom2 = line2.geometry()

    if (geom1.distance(geom2) < eps 
        and line1.id() < line2.id()): # to avoid duplicate points 

        point = geom1.nearestPoint(geom2)
        feature = QgsFeature()
        feature.setGeometry(point)
        point_lyr.addFeature(feature)    

point_lyr.commitChanges() QgsProject.instance().addMapLayer(point_lyr)

enter image description here

Note: The script also gives the intersecting point highlighted in the image.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • Thank you for your detailed answer. I tried as you wrote but an error occurred like this. Could you tell me why this error occurred?

    File "", line 15 point_lyr.commitChanges() ^ SyntaxError: invalid syntax

    – jin Nov 25 '20 at 15:23
  • This means that you used invalid syntax. You probably missed : or ) etc in previous lines. Check especially line 14. – Kadir Şahbaz Nov 25 '20 at 18:11
6

I'm assuming that each line is one feature rather than each segment being one feature. You could then try overlaying buffered start and end nodes with the line layer.

  1. Run Extract Specific Vertices with 0 and -1 (to get points for all start and end nodes)
  2. Use Buffer on the result with say 0.2 meters or something, maybe less depending on your data.
  3. Run Join Attributes By Location (Summary) with the buffered start and end nodes as Input and the line layer as Join layer. Make sure to check the box Count in Summaries to calculate. This will give you a count on how many lines this buffered node overlaps.
  4. In the output layer from the Join, filter out all features that have a count of less than 2. The remaining ones should be your intersections.
  5. Either run Centroids on the result to get the nodes back as points, or select the original extracted nodes that overlap the results via Select by location.

The idea is that all buffered nodes should have 1 overlap from its own original line, unless they intersect with another line, then the count will be 2 (or more for multiline intersections.)

hexamon
  • 2,646
  • 1
  • 16
  • 26