1

I have a request to carry out a task which resembles Creating alternatingly colored line from points layer in QGIS.

Is it possible to achieve an alternation of color between sections (several lines) and not only between each line?

In order to differentiate each of the sections (a section corresponds to an entity in my data layer). I am attaching an image to be clearer.

enter image description here

As you can see, I have several sections which each correspond to an entity (separated by a red line). After using @JGH's method from the earlier Q&A, here is the result :

enter image description here

As you can see, the alternation of color took place between each line and not between each section. I tried to modify the expression segments_to_lines($geometry) to solve the problem but it didn't work, do you have a solution ?

3
  • 3
    Please do not delete and repost Questions. In addition to filling the database with duplicates, you can incur the wrath of the automated anti-spam defenses, which can lock out your login.
    – Vince
    Nov 15, 2023 at 16:04
  • 3
    What defines the red cross section lines? Are these different records in the data table, are these different spatial data symbolized in a cross pattern, or did you just make them up?
    – GBG
    Nov 15, 2023 at 17:28
  • The red lines are hatches that intersect each of the features in my data layer. I created it from the "hatched line" type of symbology, then I checked the option "on the first vertex" (I'm French, sorry if the translation is not exact).
    – KQTN
    Nov 16, 2023 at 16:04

1 Answer 1

2

Solution: the idea and requirenments

You can use geometry generator for this. This solution requires that you have a point layer named points with points on (or near) the line where you want a color section to change and that the points are created in the correct order beginning in the line direction from the line's start- to it's end-point (if they are in the inverse order, change 1 to 0 in line 9). Like this, you can create substrings of the initial line between the points: create two geometry generator styles, one for red segments (uneven segments: 1st, 3rd, 5th etc.), the other for blue segments (2nd, 4th etc.).

Line layer containing a single line feature that is split up at the points (even if they are not exactly on the line, see no. 3 and 4); points start at the bottom and follow the line direction (from start towards end point/here: bottom to top); points are labeled with their $id value: make sure the points are in the correct order for the solution to work properly enter image description here

Implementation

On the line layer, create two symbol layers of type Geoemtry generator and Geometry type LineString. Then add this expression for the uneven parts. For the even parts, the expression is the same, you only have to change 0 to 1 in line 25. Style the two symbol layer types in different colors and you're done:

collect_geometries(
with_variable(
    'sect',
    array_sort(
        overlay_nearest(
            'points',
            $id,
            limit:=-1
        ),1 -- change to 0 in case points are in reverse order (end- to start-point of the line)
    ),
with_variable(
    'allp',
    array_insert( 
        array_append( 
            array_foreach (
                @sect,
                geometry (get_feature_by_id ('points', @element))
            ),
            end_point($geometry)
        ),
        0,
        start_point($geometry)
    ),
    array_foreach (
        generate_series (0, array_length(@allp)-2,2), -- change 0 to 1 here for even parts
        line_substring (
            $geometry, 
            line_locate_point(
                $geometry,
                @allp[@element]
            ),
            line_locate_point(
                $geometry,
                @allp[@element+1]
            )
        )
    )
)))

Solution works even with several lines, as long as the line direction follows the direction of the point's $id; if not (see leftmost line: starts from top to bottom), you get a different result:

enter image description here

2
  • I'll try this tomorrow and give you an update. Thank you
    – KQTN
    Nov 16, 2023 at 16:13
  • I tested it and it worked ! the only problem is that in some situations I need 3 colors (intersection of 3 lines) so it doesn't work, I think I will do it manually.
    – KQTN
    Nov 20, 2023 at 11:22

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.