1

I'd like to create a new field considering duplicate and numeric values.

For example, I have an attribute table with duplicate values in the "field1" and numeric values in the "field2". I'd like to create a "field3" in which there are the suffix _1, _2...on the "field1" attributes, when they are duplicates and when "field2" > 0.

In case "field2" = 0 or not duplicated value in the "field1" -> "field3"="field1".

The suffix should not be set randomly, but following the spatial direction (or left or right); and for each group of duplicated values.

Before:

Before1

Before2

After:

After

Any suggestion to use a string in the field calculator to do this in QGIS?

2
  • 1
    For the directional ordering, see gis.stackexchange.com/questions/344161/…
    – JGH
    Commented Jun 7, 2020 at 13:11
  • I've seen this answer, but i don't understand how can i adapt it to my case study. Thanks!
    – Mark
    Commented Jun 7, 2020 at 19:40

1 Answer 1

5

You can try the following expression:

with_variable(
    'my_array',
    array_agg(
        $geometry,
        group_by:="field1",
        filter:="field2">0,
        order_by:=x($geometry)
        ),
    CASE
        WHEN array_find(@my_array, $geometry)=-1
        THEN to_string("field1")
        ELSE to_string("field1") || to_string(array_find(@my_array, $geometry) + 1)
    END
    )

A short explanation:

array_agg takes all geometries into an array with the same value in "field1" (the GROUP_BY), without the ones having a 0 in "field2" and order it by the easting value(x). In the CASE WHEN expression it will proof, if the current value (geometry) is in the array, if yes, it will be added by a position value from the array, if not, it will be nothing added. You may have to play around with the conditions a bit to get the result you want to achieve.

5
  • Thank you so much!! It works! In this code, i don't understand where is the condition where "not duplicated value in the "field1" has been set for not to take the suffix ("field1"='c' example). Also, i don't understand where you set "_" before the suffix number 1,2,3. Thanks.
    – Mark
    Commented Jun 9, 2020 at 21:33
  • to set the suffix you should change: ELSE to_string("field1") || '_' || to_string(array_find(@my_array,$geometry)+1) the 'c' is in fact a problem...
    – eurojam
    Commented Jun 10, 2020 at 5:18
  • about the second string....maybe, we can adding a condition not duplicated value in the "field1" in the row -> filter:="field2">0 ? I'm thinking -> filter:="field2">0 and count_distinct("id","field1")>1,....it should work
    – Mark
    Commented Jun 10, 2020 at 18:15
  • In this case, we are considering the 'x' spatial direction [order_by:=x($geometry))]. If there isn't a prevalent direction (x or y), and the points are positioned along a not regular route (presence of curves and intersections); is there a solution to order these points considering the street direction? Thanks a lot!
    – Mark
    Commented Jun 19, 2020 at 8:15
  • yes I think that is possible if you have the direction under your control, see gis.stackexchange.com/questions/25337/…
    – eurojam
    Commented Jun 20, 2020 at 5:35

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.