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?

Taras
  • 32,823
  • 4
  • 66
  • 137
Mark
  • 965
  • 6
  • 12
  • 1
    For the directional ordering, see https://gis.stackexchange.com/questions/344161/how-to-create-a-grid-with-customized-labels-in-qgis/344212#344212 – JGH Jun 07 '20 at 13:11
  • I've seen this answer, but i don't understand how can i adapt it to my case study. Thanks! – Mark Jun 07 '20 at 19:40

1 Answers1

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.

Taras
  • 32,823
  • 4
  • 66
  • 137
eurojam
  • 10,762
  • 1
  • 13
  • 27
  • 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 Jun 09 '20 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 Jun 10 '20 at 05: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 Jun 10 '20 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 Jun 19 '20 at 08:15
  • yes I think that is possible if you have the direction under your control, see https://gis.stackexchange.com/questions/25337/qgis-calculate-distance-of-point-along-a-line – eurojam Jun 20 '20 at 05:35