Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a new layer in QGIS based on an existing layer. The new features should have all the attributes of the corresponding features in the original layer. I've been trying to use outFeat.setAttributeMap(inFeat.attributeMap() ) similar to what Victor Olaya is doing at, but all the attributes come out as NULL.

Could you tell me where the error is in my code?

def test(outputFilename):
 layer = qgis.utils.iface.activeLayer()
 provider = layer.dataProvider()
 fields = provider.fields()
 writer = QgsVectorFileWriter(outputFilename, "CP1250", fields, provider.geometryType(),, "ESRI Shapefile")
 inFeat = QgsFeature()
 outFeat = QgsFeature()
 inGeom = QgsGeometry()
 while provider.nextFeature(inFeat):
  point = inFeat.geometry().asPoint()
  inGeom = inFeat.geometry()
  outFeat.setGeometry(inFeat.geometry() )
  outFeat.setAttributeMap(inFeat.attributeMap() )
  writer.addFeature( outFeat )
 del writer
 newlayer = QgsVectorLayer(outputFilename, "Output", "ogr")

share|improve this question
up vote 3 down vote accepted

You are missing a method call. )
while provider.nextFeature(inFeat):
share|improve this answer
Ah, thank you so much! That fixes it! (I had to add provider.attributeIndexes() ), though, which makes sense when looking at the docs) – Jake Jan 28 '13 at 23:33
All good. Just be aware that this has changed in the latest dev build so you will have to update it in 2.0 – Nathan W Jan 28 '13 at 23:49

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.