4

I have a segmented line (road) dataset and I want to get the number of times each road is segmented.

In SQL it's just

SELECT RoadMntnc, count(RoadMntnc) AS count
FROM clip_DSC_Roads
GROUP BY RoadMntnc

How do I do the same in field calculator?

Sample data is in https://drive.google.com/drive/folders/1RL7VZL_giZuZ9xVfDFgk409_RIErIB0m?usp=sharing

I assume it would be something like aggregate(count("RoadMntnc"), group_by="RoadMntnc") but this doesn't work.

1 Answer 1

5

EDIT: or to your changed request it should be:

array_length( array_agg( "RoadMntnc", "RoadMntnc"))

You can use the following expression to count the segments of a line or polygon:

num_geometries(segments_to_lines($geometry))

example

3
  • Thanks - this works but I didn't explain what I meant by segments properly. I don't mean the number of vertices on the line (each time it bends). What I meant is that a single road is made up of multiple lines with the same road name. I want to get the number of lines that make up a road. The SQL in the question does this...I want to do the same in field calculator. It should be some form of the aggregate/group_by expression but I can't figure it out.
    – GeorgeC
    Sep 5, 2019 at 5:43
  • this should be: array_length( array_agg( "RoadMntnc", "RoadMntnc"))
    – eurojam
    Sep 5, 2019 at 6:09
  • -brilliant. That works well. Good value for money on this question where you have given 2 great answers that will both be helpful. :-)
    – GeorgeC
    Sep 5, 2019 at 6:13

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.