0

I have two datasets of data let say set_1 and set_2. set_1 is original dataset and set_2 is modified dataset (duplicates and wrong data has been removed from the data). Due to the modification some of the data has been removed from the set_2. Now i am adding few more new features to the data. so i want new ids for the new features. enter image description here

if i use $id the features id's i have already deleted in set_2 will again regenerated. if i vlook up data from the set_1 in case the data wont join correctly. thats why i dont want any of the id which are in the set_1 should not repeat. thats why i want new ids for my new features................hope you understand my question

0

2 Answers 2

2

I don't know how you created your data. However, the behaviour is not normal. Normally, you should not need to care about the id this way, as QGIS would handle this automatically the way you want. Saving your data as Geopackage, you automatically get a fid that exactly does what you describe.

So you probably miss a point here, but you don't provide enough information to see what goes wrong. Still, to answer your question in case you really want to continue to do this manually using the following expression (see below for explanation). Be aware: in my case, fid is the name of the id-value in Set_1; in Set_2 it's called id (change this accordingly):

if (
    "id" is NULL,
    aggregate ('Set_1', 'max', "fid") +
    @row_number -
    array_length ( 
        aggregate( 
            @layer,
            'array_agg',
            "id", 
            filter:= "id" is not NULL
        )
    ),
    "id"
)

enter image description here

  1. Find the largest existing id value in Set_1 with aggregate( 'Set_1','max',fid). In your screenshot, this would return 8.

  2. Find the number of remaining features (rows, lines) that already have an id value in Set_2 with array_length (array_agg ("id", filter:= "id" is not NULL)). Returns 6 in your example.

  3. Now for each of the additional rows (lines 7, 8,...), calculate a sequential series of how much you have to add: add 1 for line 7, 2 for line 8 etc. To calculate this, use @row_number - [result from step 2] - replace the part in angular braces [] with the expression from step 2.

  4. You want to start with id=9 (next larger value from step 1) in line 7 (next feature from step 2). Thus add value from step 1 with value from step 3.

  5. You want to update only id's that are empty, but keep pre existing values

2
  1. Select the new features. You can use Select by Expression with fid is NULL.

  2. Run Field calculator and check the box next to Only update [n] selected features.


You could even update $id for all fid fields, including those that already have a fid value. If this pre existing fid is based on $id, it will return the same value as before.

enter image description here

9
  • cant we continue the series from 996
    – Bruno B
    Commented Dec 22, 2022 at 9:17
  • numbers are repeated in this case makes feature duplicate. can't we continue the series from the last id @babel
    – Bruno B
    Commented Dec 22, 2022 at 10:42
  • Using $id you should get automatically an autoincrement number that starts from the last id you have, thus from 996 in your case. If for some reason that should not be the case, then the question is: how was this fid generated? What does it represent? In this case, you can add 996 to the $id and use this expression: $id + 996
    – Babel
    Commented Dec 22, 2022 at 10:45
  • when using $id it calculates the id's with the help of FID which is already existing. I dont want any of the Ids created with the help of any other columns @babel
    – Bruno B
    Commented Dec 22, 2022 at 11:23
  • Don't really understand what you mean... If you don't want to re-calculate the fid, then make it like I described it in the solution and only fid's that don't already exist will be created.
    – Babel
    Commented Dec 22, 2022 at 12:33

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.