3

In QGIS 3.16 in Windows 10 I have:

  • a line layer 'centerline.shp'
  • a polygon layer 'o31001_AX_Gebaeude_f_12342.shp'

I am trying to classify my line layer based on the number of intersection points with the polygon layer.

I use the Field Calculator of the line layer and wrote this:

CASE
    WHEN aggregate(layer:='o31001_AX_Gebaeude_f_12342', aggregate:='count', expression:="ID", filter:=intersects($geometry, geometry(@parent))) = 0 THEN 5
    WHEN aggregate(layer:='o31001_AX_Gebaeude_f_12342', aggregate:='count', expression:="ID", filter:=intersects($geometry, geometry(@parent))) = 1 THEN 1
    WHEN aggregate(layer:='o31001_AX_Gebaeude_f_12342', aggregate:='count', expression:="ID", filter:=intersects($geometry, geometry(@parent))) > 1 THEN 2
END

I can not get proper results. I think it might be a problem with the:

filter:=intersects($geometry, geometry(@parent))

How can I find if a line has an intersection with the polygon in QGIS? If yes, then how many points do they share.

2

What you seem to mean is how often does each line cross the boundary (outline) of each polygon. When a line crosses a polygon, the number of intersection points is infinite as the intersection has the form of a line and this consists of an infinite number of points.

To count the number of crossing points with the boundary, first convert your polygons to lines using Menu vector / Geometry tools / Polygons to lines, than explode lines with Menu Processing / Toolbox / Explode lines to get a separate feature for each line segment.

Now on your centerline layer calculate the number of crossing points using field calculator and this expression (replace exploded with the name of the layer created before): array_length (overlay_intersects( 'exploded', $geometry))

As you already have QGIS 3.16, using overlay_intersects should not be a problem - this function is only available since QGIS 3.16.

Screenshot: the black lines are labeled with the expression from above. There are two special cases, however, where the number differs from the actual number: 1. case on the left, where the line touches exactly one point (vertex) of the polygon: as the line in fact touches two segments of the boundary (the two polygon-boundary segments starting at the vertex), it returns a value of 2; 2. Line on the bottom: in one case (crossing points 4 and 5 from the left), the line twice crosses the same segment, thus it is counted just once - and so, the resulting value is 9 instead of 10: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.