2

I am trying to perform a if/elif/else routine in the field calculator environment from PyQGIS (i.e. coding in Python).

My code is the following, however I keep getting indentation errors. The field calculator code is working fine for normal operations between fields, but I am not managing to get the conditional statement working.

Note that I get the error already when defining the function funct_calculator with the conditonal statements, so before actually calculating attributes.

myField = QgsField( 'wg_maize', QVariant.Double )
voronoi_clipped_reprojected.dataProvider().addAttributes([myField])
voronoi_clipped_reprojected.updateFields()

def funct_calculator():
    if feature['clzonemaj'] = 5401:
        feature['maize_area'] * wr_rainfed_maize_5401
    elif feature['clzonemaj'] = 6401:
        feature['maize_area'] * wr_rainfed_maize_6401
    elif feature['clzonemaj'] = 7101:
        feature['maize_area'] * wr_rainfed_maize_7101
    elif feature['clzonemaj'] = 7501:
        feature['maize_area'] * wr_rainfed_maize_7501
    elif feature['clzonemaj'] = 7701:
        feature['maize_area'] * wr_rainfed_maize_7701
    else:
        0

def calculate_attributes():
    with edit(voronoi_clipped_reprojected):
        for feature in voronoi_clipped_reprojected.getFeatures():
            feature.setAttribute(feature.fieldNameIndex('wg_maize'), funct_calculator)
            voronoi_clipped_reprojected.updateFeature(feature)
    print(f"Attribute calculated")

calculate_attributes()

In particular, I get the following error:

def funct_calculator():
    if feature['clzonemaj'] = 5401:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 2
    if feature['clzonemaj'] = 5401:
                            ^
SyntaxError: invalid syntax
        feature['maize_area'] * wr_rainfed_maize_5401
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    feature['maize_area'] * wr_rainfed_maize_5401
    ^
IndentationError: unexpected indent
    elif feature['clzonemaj'] = 6401:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    elif feature['clzonemaj'] = 6401:
    ^
IndentationError: unexpected indent
        feature['maize_area'] * wr_rainfed_maize_6401
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    feature['maize_area'] * wr_rainfed_maize_6401
    ^
IndentationError: unexpected indent
    elif feature['clzonemaj'] = 7101:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    elif feature['clzonemaj'] = 7101:
    ^
IndentationError: unexpected indent
        feature['maize_area'] * wr_rainfed_maize_7101
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    feature['maize_area'] * wr_rainfed_maize_7101
    ^
IndentationError: unexpected indent
    elif feature['clzonemaj'] = 7501:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    elif feature['clzonemaj'] = 7501:
    ^
IndentationError: unexpected indent
        feature['maize_area'] * wr_rainfed_maize_7501
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    feature['maize_area'] * wr_rainfed_maize_7501
    ^
IndentationError: unexpected indent
    elif feature['clzonemaj'] = 7701:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    elif feature['clzonemaj'] = 7701:
    ^
IndentationError: unexpected indent
        feature['maize_area'] * wr_rainfed_maize_7701
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    feature['maize_area'] * wr_rainfed_maize_7701
    ^
IndentationError: unexpected indent
    else:
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    else:
    ^
IndentationError: unexpected indent
        0
Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    0
    ^
IndentationError: unexpected indent
2
  • 1
    You should use == to make comparisons in the if clause. For example: if feature['clzonemaj'] == 5401: Jun 12, 2019 at 13:25
  • 1
    Solution was easy and silly. Thank you very much! If you reply properly I will mark it as solved.
    – Jackk
    Jun 12, 2019 at 13:43

1 Answer 1

3

Everything is fine, you just have to use the == to make comparisons in the if clause.

myField = QgsField( 'wg_maize', QVariant.Double )
voronoi_clipped_reprojected.dataProvider().addAttributes([myField])
voronoi_clipped_reprojected.updateFields()

def funct_calculator():
    if feature['clzonemaj'] == 5401:
        feature['maize_area'] * wr_rainfed_maize_5401
    elif feature['clzonemaj'] == 6401:
        feature['maize_area'] * wr_rainfed_maize_6401
    elif feature['clzonemaj'] == 7101:
        feature['maize_area'] * wr_rainfed_maize_7101
    elif feature['clzonemaj'] == 7501:
        feature['maize_area'] * wr_rainfed_maize_7501
    elif feature['clzonemaj'] == 7701:
        feature['maize_area'] * wr_rainfed_maize_7701
    else:
        0

def calculate_attributes():
    with edit(voronoi_clipped_reprojected):
        for feature in voronoi_clipped_reprojected.getFeatures():
            feature.setAttribute(feature.fieldNameIndex('wg_maize'), funct_calculator)
            voronoi_clipped_reprojected.updateFeature(feature)
    print(f"Attribute calculated")

calculate_attributes()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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