11

Is there a way to create an auto increment primary key column when creating a new Shapefile in QGIS?

4 Answers 4

7

As far as I know, Shapefiles have no concept of primary key or auto increment data types.

What you can do is use Field Calculator to populate a column with row number.

1
  • The only issue is that they seem to change when I edit the data and recalculate; I think the best option is to switch to Spatialite.
    – mattwigway
    Oct 16, 2011 at 23:17
3

I get this question a lot on the pyshp mailing list and underdark is correct. The shapefile format provides no unique identifiers for geometry or attribute records.

The object id, feature id, or other GUID is a trapping of the software you use and is just a row number assigned as the records are read in. The only options are to assign a new attribute using scripting or spreadsheet software, or load the data into a spatial database.

1

A Shapefile has a OBJECTID field; which serves as its key and primary index which should suit your purposes.

2
  • I believe this is the same as the row number, and is what ArcGIS uses, but I'm not sure.
    – mattwigway
    Oct 16, 2011 at 23:16
  • 2
    But the OBJECTID can change as you delete rows; but that is the primary index.
    – D.E.Wright
    Oct 17, 2011 at 15:12
1

I used maximum(id)+1 in the default value field and checked 'Apply default value on update'. It worked fine with me.

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.