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?
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?
Try using "Line intersections" geoalgorithm that creates points where lines intersect.
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.
For snapping geometries in a layer to the geometries within the same layer apply "Snap geometries to layer".
And after apply the "Line intersections" geoalgorithm.
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)
Note: The script also gives the intersecting point highlighted in the image.
File "", line 15 point_lyr.commitChanges() ^ SyntaxError: invalid syntax
– jin Nov 25 '20 at 15:23: or ) etc in previous lines. Check especially line 14.
– Kadir Şahbaz
Nov 25 '20 at 18:11
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.
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.)