3

New to python, and been looking at this for a while, trying to write a scrip to automate a process we need to carry out in QGIS a few thousand times. As part of this I need to create a feature into an existing Postgres table without geometry. The existing table has approximately 40 different columsn, however I only need to populate the 6 as shown in the code.

I have the below code which I believe is correct, however the add features command doesnt seem to work, it returns False and doesnt write anything to the datbase.

def createNewIntermentRightRecord(layer,cemetery,yearsGranted,surname,firstName,otherName,liscadNo):
    if internRightsCaps & QgsVectorDataProvider.AddFeatures:
        feat = QgsFeature(layer.pendingFields())
        feat.initAttributes(6)
        feat.setAttributes(['Cemetery',cemetery])
        feat.setAttributes(['Years Granted',yearsGranted])
        feat.setAttributes(['IR Holder - LAST NAME',surname])
        feat.setAttributes(['IR Holder - First Name',firstName])
        feat.setAttributes(['IR Holder - Other Name/s'])
        feat.setAttributes(['Liscad Polygon No',liscadNo])
        (res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )
        print res
        print outFeats
PolyGeo
  • 65,136
  • 29
  • 109
  • 338

1 Answers1

1

Around the method:

(res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )

I think you miss a start edit and commit statement

def createNewIntermentRightRecord(layer,cemetery,yearsGranted,surname,firstName,otherName,liscadNo):
    if internRightsCaps & QgsVectorDataProvider.AddFeatures:
        feat = QgsFeature(layer.pendingFields())
        feat.initAttributes(6)
        feat.setAttributes(['Cemetery',cemetery])
        feat.setAttributes(['Years Granted',yearsGranted])
        feat.setAttributes(['IR Holder - LAST NAME',surname])
        feat.setAttributes(['IR Holder - First Name',firstName])
        feat.setAttributes(['IR Holder - Other Name/s'])
        feat.setAttributes(['Liscad Polygon No',liscadNo])
        layer.startEditing()
        (res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )
        layer.commitChanges()
        print res
        print outFeats
Jakob
  • 7,471
  • 1
  • 23
  • 41
  • Appreciate your help, definitely getting somewhere now, however still having issues. If I make the layer a shapefile, no problems writing to it.. However, if the layer in question is a postgres table which is in the layers panel it doesnt write to it, but doesnt throw any errors either. THe rest of the script reads from it fine though. – Brenton Light Mar 09 '16 at 03:26
  • Do you have a primary key on the table in PostGIS? Check this for adding one: http://gis.stackexchange.com/questions/157541/postgresql-trouble-editing-points-lines-polygons-in-qgis – Jakob Mar 09 '16 at 07:00
  • Interesting you should suggest that I had a problem with that on a different table earlier this week. But yes I do have an primary key on that particular table. THis has led me to some more searching, I seem to ahve no problems adding to a new table in my postgres database. Have got a work around for now, but something to look into in the future. Your assistance is greatly appreciated! – Brenton Light Mar 10 '16 at 03:26