1

I am trying to make my field in a Routeline to automatically count all the ducts that passed throught it. I was able to come up with this code.

aggregate(layer:='Duct',
          aggregate:='count',
          expression:='*',
          filter:=intersects($geometry, geometry(@parent)))

But I am getting is not quite accurate because it somehow randomly counts ducts. See picture below. (accurate counts are in red) enter image description here

What I thought that can solve this issue is to count lines that are intersecting the midpoint of the Routeline. In which I used this code, but now it is just returning 0. Any help?

aggregate(layer:='Duct', 
          aggregate:='count', 
          expression:='*', 
          filter:=intersects($geometry, 
                             line_interpolate_point(@current_geometry, 
                                                     length(@current_geometry)/2)))
2
  • 1
    It is hard to tell from the diagram where the individual footways and ducts start and finish (footways in particular), and also whether footway is a line or a polygon. Maybe you can clarify? May 19, 2023 at 3:23
  • Not sure what exactly you intend to do. Maybe similar to this? gis.stackexchange.com/a/458593/88814
    – Babel
    May 19, 2023 at 8:06

1 Answer 1

1

On the assumption that both layers are lines, your problem is most likely to do with the fact that you are trying to intersect a point with a line, which will rarely work.

One workaround is to buffer the point by a small amount, which works correctly in my example screenshot below:

aggregate(layer:='Duct', 
          aggregate:='count', 
          expression:='*', 
          filter:=intersects($geometry, 
                             buffer(
                                 line_interpolate_point(
                                     geometry(@parent),
                                     length(geometry(@parent))/2),
                                 10)))

The image below shows the interpolated points, and the buffered region. The amount of buffering needs to be based on how co-incident the ducts and footways are.

Too large a buffer, and you will get overlap with other ducts. Too small and you will miss the intersections altogether.

[Also worth noting that the @current_geometry variable may not work - see https://github.com/qgis/QGIS/issues/44499]

enter image description here

1
  • This expression is working great. The buffer works great on half a meter in which sometimes i do have multiple parallel lines that I digitize. Thank you! May 21, 2023 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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