0

I have two layers where different columns exist. I want to copy a layer column into antoher layer column programatically. I have done the below code so far:

layerNameValueSet = "PLATFORM"
#columnNameSet where to value add
columnNameSet = "ZONEID"

#LayerName Where to copy column values
layerNameValueGet= "LOT" 
#Above layer's[layerNameValueGet] cloumn name from value will be get for copy in layerNameValueSet
columnNameGet = "ZONEID"

#Getting layer of specifc name
layerNameValueSetObj = QgsMapLayerRegistry.instance().mapLayersByName(layerNameValueSet)[0];
layerNameValueGetObj = QgsMapLayerRegistry.instance().mapLayersByName(layerNameValueGet)[0];

fieldIndexToSet = layerNameValueSetObj.fieldNameIndex(columnNameSet)
fieldIndexToGet = layerNameValueGetObj.fieldNameIndex(columnNameGet)

for getFeature in layerNameValueGetObj.getFeatures():

    #this logic is not correct /taking too much time hanging

for setFeature in layerNameValueSetObj.getFeatures():
    if(getFeature.id() == setFeature.id()):
        layerNameValueSetObj.startEditing()
        updateValue = getFeature[columnNameGet]
        layerNameValueSetObj.changeAttributeValue(setFeature.id(), fieldIndexToSet, updateValue)
        layerNameValueSetObj.commitChanges()
0

You could :
- instead of editing / commiting in a loop. Build a list or even a dic. And so you would just need to commit once.
- Why not use the join by attribut tool instead of reinventing the wheel: see helpfull script

this could also help : link1, link2

1
  • actually my code is not working correctly i simply want to copy it from one column into antoher all data line by line. – Muhammad Faizan Khan Sep 28 '16 at 14:38
0

Finally zip help me to do this Job. I run both loop simultaneously in one loop and here is the answer:

import itertools
#layerNameValueSet Where to copy column values
layerNameValueSet = "PLATFORM"
#columnNameSet where to value add
columnNameSet = "ZONEID"

#LayerName Where to copy column values
layerNameValueGet= "LOT" 
#Above layer's[layerNameValueGet] cloumn name from value will be get for copy in layerNameValueSet
columnNameGet = "ZONEID"

#Getting layer of specifc name
layerNameValueSetObj = QgsMapLayerRegistry.instance().mapLayersByName(layerNameValueSet)[0];
layerNameValueGetObj = QgsMapLayerRegistry.instance().mapLayersByName(layerNameValueGet)[0];

fieldIndexToSet = layerNameValueSetObj.fieldNameIndex(columnNameSet)
fieldIndexToGet = layerNameValueGetObj.fieldNameIndex(columnNameGet)

#running two loops simeltenously so that copy one value into antoher
for getFeature, setFeature in zip(layerNameValueGetObj.getFeatures(), layerNameValueSetObj.getFeatures()):
    layerNameValueSetObj.startEditing()
    updateValue = getFeature[columnNameGet]
    layerNameValueSetObj.changeAttributeValue(setFeature.id(), fieldIndexToSet, updateValue)
    layerNameValueSetObj.commitChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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