1

I need to change the id (feature.id() in the python API or $id in the field calculator) of for features in shapefile layers in qgis, and I need to do it in Python. Is there a way?

I tried something like what was suggested in the post How to change attributes with QGIS python? , but it did not work, because apparently id is not a field.

In any case, is there a way of changing it? If not, is there a way to create a geographic feature and specify its id?

1 Answer 1

6

If you try to change the ID of the feature in the shapefile it will break the shapefile. A Shapefile is not one file but a collection of files (shp, dbf, sbn, sbx) and the ID is the key to match the feature with all these files. If you were to change it in the dbf file (attributes) it would no longer be found in the others.

ID's in shapefiles are transient and not fixed like OIDs are in databases and change from time to time during edits, the software handles all of this for you. The software gives the next available ID to a feature when it's created and can change them at a whim.

If you need a constant field for a reference add a field which you can calculate or store your data in a database.

1
  • Thanks, Michael! I suspected that shapefiles were not very orthodox regarding its IDs, so I will create an ID field independent from the shapefile $id! Commented May 20, 2014 at 15:50

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.