Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I had a working plugin in QGIS 1.8 which read data from an MSAccess database and added it to a series of memory layers. There is some processing involved in the interim so I don't think simply using QGIS to read directly from the database is an option.

I would like to move from QGIS 1.8 to 1.9 (primarily due to the improved quality of the print composer). The plugin doesn't work with the new API.

I've tried a variety of methods that came up in google searches. One, modifying the below code - from http://www.qgis.org/pyqgis-cookbook/vector.html#memory-provider, i.e. adding geometry and attributes to the dataprovider and then updating the layer - to suit the new API worked a little but the attributes were not visible until I manully entered editing mode (similar to http://hub.qgis.org/issues/3713). An alternative approach, detailed in reply #1 of the above link, added the layer and attributes correctly but I was unable to add features to the layer.

Given that it should be a pretty simple task I'm hopeful that someone on here can offer a working example of how this should be done. (P.S. I am not a professional programmer and most of my coding is fairly crude - I welcome any guidance but ask that you excuse some ignorance on my part)

# Receivers = a list of lists returned from a database query

# create layer
vl = QgsVectorLayer("Point", item, "memory")
pr = vl.dataProvider()

# add fields
pr.addAttributes( [ QgsField("Rec_No", QVariant.Int), QgsField("Include",  QVariant.String), QgsField("Label",  QVariant.String), QgsField("X", QVariant.Double),
                    QgsField("Y", QVariant.Double), QgsField("Z", QVariant.Double), QgsField("Height", QVariant.Double),
                    QgsField("Project_Re", QVariant.String), QgsField("NCA", QVariant.String),
                    QgsField("DayCrit", QVariant.Int), QgsField("EveCrit", QVariant.Int), QgsField("NightCrit", QVariant.Int) ] )

for i in range(len(Receivers)):          
  # add a feature
  fet = QgsFeature()
  X = Receivers[i][3]
  Y = Receivers[i][4]
  fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(X,Y)) )

  # Details = a list of results returned from a database query specific to each result in 'Receivers'

  if Receivers[i][3] != 0:
    Include = 'Yes'
    Include = 'No'

  fet.setAttributeMap( { 0 : QVariant(Receivers[i][0]), 1 : QVariant(Include), 2 : QVariant(Receivers[i][2]),
                         3 : QVariant(Receivers[i][3]), 4 : QVariant(Receivers[i][4]), 5 : QVariant(Receivers[i][5]), 6 : QVariant(Receivers[i][6]),
                         7 : QVariant(Details[0]), 8 : QVariant(Details[1]), 9 : QVariant(Details[2]), 10 : QVariant(Details[3]), 11 : QVariant(Details[4]) } )
  pr.addFeatures( [ fet ] )

# add a style

# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer

share|improve this question
Take a look at the PinPoint plugin. It adds features with attributes to a memory layer and works with the 2.0 API. –  gsherman May 12 '13 at 14:48

1 Answer 1

Thanks to gsherman above the PinPoint plugin example is perfect.

As I understand it the process is:

  1. Create the layer with the attributes in the construction string
  2. Add said layer to the map registry
  3. Start editing on that layer
  4. Add features and commit the changes

Here is an extract of my code which now works.

layer =  QgsVectorLayer(

# Receivers = as in the above example 'Receivers' is a list of results
for i in range(len(Receivers)):

  # add a feature
  feature = QgsFeature()

  X = Receivers[i][3]
  Y = Receivers[i][4]
  feature.setGeometry( QgsGeometry.fromPoint(QgsPoint(X,Y)) )

  # Details = as in the above example 'Details' is a list of results

  if Receivers[i][1] != 0:
    Include = 'Yes'
    Include = 'No'

  values = [ QVariant(Receivers[i][0]), QVariant(Include), QVariant(Receivers[i][2]),
                         QVariant(Receivers[i][3]), QVariant(Receivers[i][4]), QVariant(Receivers[i][5]), QVariant(Receivers[i][6]),
                         QVariant(Details[0]), QVariant(Details[1]), QVariant(Details[2]), QVariant(Details[3]), QVariant(Details[4]) ]

  layer.addFeature(feature, True)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.