I'm having difficulty in calculating the values for features in an empty attribute column by using existing values from other attribute columns in a joined table. For example, the feature 1 in attribute column 1 + feature 1 in attribute column 2 = new feature 1 value in attribute column 3. Specifically, that the column names being used are hard coded and that is definitely not ideal (see code below).

expression = QgsExpression("to_int(\"Primary\" * \"POP06\")")

for feature in huff_model.getFeatures():
    value = expression.evaluate(feature)
    feature[idx_pri_pop] = value  #idx_pri_pop is the index of the column where the new values go

"Primary" and "POP06" fields are both hard coded which will cause the script to fail if the user changes the field names or uses different data. I want the code to accept a field name no matter what it is, so it's flexible. However, it appears that QgsExpression does not accept variables, even if they hold strings (or it can and I can't seem to make it work). As such, I've also tried using the changeAttributeValue method, however that does not work at all, as the new field only has null values.

  • I have looked at other answer with joins, however I could not find one that explicitly covers how to calculate a value for a feature using features in other fields as variables for the calculation.
  • The attribute table that I am using is from the target layer after a join. I'm not sure if that might cause any issues or not.
  • It might also be the case that the rest of my code (see below) may not be in the right order or some other parts of it might be incorrect, despite running with no errors. That could be why the changeAttributeValue method might not be working. I'm not entirely sure.


Processing Script Parameters and Imports

##Mall=field Huff_Model_Layer
##Population_Data=field Census_Layer
##Mall_Layer_Name= string test
##PDF_file=output file pdf

from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os

Creating the layer

# Load the huff model layer
ori_huff_model = processing.getObject(Huff_Model_Layer)
# Load the census layer
census_layer = processing.getObject(Census_Layer)

# If script was run previous, remove the previously created layer to keep 
# layer panel with only one newly created layer at a time
    last_layer = QgsMapLayerRegistry.instance().mapLayersByName(Mall_Layer_Name)[0]
    del last_layer
except IndexError:
    child1 = root.children()[1]
    if child1.name() == census_layer.name():
        ids = root.findLayerIds()
        last_layer = root.findLayer(ids[0])
        print last_layer.name()

# Make a copy of the huff model layer in memory to work with so that all 
# changes made to copy only keeping original data intact
huff_model = QgsVectorLayer("Polygon?crs=epsg:4326", Mall_Layer_Name, "memory")
huff_model_data = huff_model.dataProvider()


attr = ori_huff_model.dataProvider().fields().toList()

feat = QgsFeature()
for elem in ori_huff_model.getFeatures():

# Choose the mall (field in table) to find the primary and secondary market 
# areas 
mall = ori_huff_model.fieldNameIndex(Mall)
for field in huff_model.fields():
    field_id = huff_model.fieldNameIndex(field.name())
    if mall == field.name():
        mall = huff_model.fieldNameIndex(field.name())

col_num = int(mall)

# Create two new fields that will only show the primary and secondary market 
# areas based on what is chosen as primary and secondary market 
# probabilities
primary = QgsField('Primary', QVariant.Double)
index_pri = huff_model.fieldNameIndex('Primary')

secondary = QgsField('Secondary', QVariant.Double)
index_sec = huff_model.fieldNameIndex('Secondary')

# Will hold population data based on primary and secondary markets 
# multiplied by the population for each census tract.
pri_pop = QgsField('PRI_POP', QVariant.Int)
idx_pri_pop = huff_model.fieldNameIndex('PRI_POP')

sec_pop = QgsField('SEC_POP', QVariant.Int)
idx_sec_pop = huff_model.fieldNameIndex('SEC_POP')

# Find the relevant features for the chosen mall
for feature in huff_model.getFeatures():
    probability = feature.attributes()
    ctuid = feature["CTUID"]
    if not probability[col_num]:
        print ("This mall does not exist")
        # Primary market values will go into the "Primary" field
        if probability[col_num] >= .6:
            huff_model.changeAttributeValue(feature.id(), index_pri, probability[col_num])
        # Secondary market values will go into the "Secondary" field
        if .4 <= probability[col_num] < .6:
            huff_model.changeAttributeValue(feature.id(), index_sec, probability[col_num])

# Delete all other features (rows) that do not have a probability in the  
# primary and the secondary fields
expr = QgsExpression("\"Primary\" is NULL and \"Secondary\" is NULL")
for f in huff_model.getFeatures(QgsFeatureRequest(expr)):


Setting up the Join

# Add newly created layer to map to prepare for the join
reg = QgsMapLayerRegistry.instance()

# Set the appropriate layers for the join. Mall_Layer_Name is the name for 
# the huff_model layer 
targetLyr = QgsMapLayerRegistry.instance().mapLayersByName(Mall_Layer_Name)[0]
censusLyr = QgsMapLayerRegistry.instance().mapLayersByName(census_layer.name())[0]

# Moves the census layer to the second position in the layer panel as a 
# conditional to remove the newly created layer on a subsequent run of the 
# script.
mycensusLyr = root.findLayer(censusLyr.id())
censusClone = mycensusLyr.clone()
parent = mycensusLyr.parent()
parent.insertChildNode(1, censusClone)

# Set properties for the join
targetField = 'CTUID'
inField = 'CTUID'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = censusLyr.id()
joinObject.joinFieldName = inField
joinObject.targetFieldName = targetField
joinObject.memoryCache = True
joinObject.prefix = ''
print(targetLyr.addJoin(joinObject))  # You should get True as response.

# The population data used to calculate the the values for the new fields 
# and populate the features(see code above). However, it can't be used in  
# QgsExpression!
ct_pop = Population_Data
ct_pop = "{}\\".format(ct_pop)


# Expression used to populate the "PRI_POP" field (see above code). Both 
# fields are hard coded. Primary is fine, since it's created in the code but 
# "POP06" should not be hard coded. 
expression = QgsExpression("to_int(\"Primary\" * \"POP06\")")

for feature in huff_model.getFeatures():
    value = expression.evaluate(feature)
    feature[idx_pri_pop] = value

# Expression used to populate the "PRI_POP" field (see above code). Both 
# fields are hard coded. Primary is fine, since it's created in the code but 
# "POP06" should not be hard coded. 
expression2 = QgsExpression("to_int(\"Secondary\" * \"POP06\")")

for feature in huff_model.getFeatures():
    value = expression2.evaluate(feature)
    feature[idx_sec_pop] = value

# Delete all other columns except for the CTUID, Primary, Secondary columns. 
# For some reason, this has to be here AFTER the join, otherwise the newly  
# calculated features don't show up in "PRI_POP" and "SEC_POP.
# It also doesn't delete fields NOT included in the set in the newly joined 
# table. It does however delete the fields in target layer (huff_model)  
# before the join that are not in the set.
fields = []

fieldnames = {'CTUID', 'Primary', 'Secondary', 'PRI_POP', 'SEC_POP'}
for field in huff_model.fields():
    if field.name() not in fieldnames:
        # print(fields.append(huff_model.fieldNameIndex(field.name())))


Creating the PDF output

# Make sure that the newly created and joined layer is the active layer.
huff_model = iface.activeLayer()

# Create the attribute using the map composer in QGIS and show the output in 
# PDF format.
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x, y, w, h)

table = QgsComposerAttributeTable(c)

# Checks for the existence of a file with the same name and removes it.
if os.path.isfile(PDF_file):

printer = QPrinter()
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)

The image below shows which fields I require for the calculation of the features in the "PRI_POP" and "SEC_POP" fields for clarification purposes. Please let me know if I need to edit anything if I didn't explain a process well!

Joined Attribute Table After Script is Run

enter image description here

1 Answer 1


QgsExpression takes the column name without the double quotes, so your expression could be

expression = QgsExpression('to_int(Primary * POP06)')

To use a custom field name, you would do

myField = 'POP06'
expression = QgsExpression('to_int(Primary * ' + myField  +')')
  • Awesome thank you! Would you happen to know what the rounding is really off when using QgsExpression? Or would you suggest another, more accurate way to multiply a float with an int? The numbers I get aren't too far off, but I'd still like it to round to the nearest whole number, but I see some values off by like 30-40 Commented Jan 14, 2018 at 6:11
  • @OskiTuranoglu as your float just have 2 decimals the result should be +-1. Check your number though as the values in your screenshot are wrong (pop06 * primary)
    – JGH
    Commented Jan 14, 2018 at 12:12
  • The original values for primary and secondary have many more decimal places, I believe up to 23? I changed them to only two with primary = QgsField('Primary', QVariant.Double, double, 2, 2) and secondary = QgsField('Secondary', QVariant.Double, double, 2, 2). Could it be that the calculation is occurring with the values at the original values and what I see on the table is only for viewing purposes? Commented Jan 14, 2018 at 17:39
  • @OskiTuranoglu that would make sense
    – JGH
    Commented Jan 15, 2018 at 13:01

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.