1

Using the QGIS Advanced Python Field Calculator, I am able to assign the new field a value from a feature an existing field using value = <Attribute_1>. I run into a problem when I am using more complex code like the following used to calculate the absolute differences in azimuths:

diff = <Az_1> - <Az_2>
absDiff = abs(diff)

if absDiff == 180:
    value = absDiff
elif absDiff < 180:
    value = diff
elif <Az_1> > <Az_2>:
    value = absDiff - 360
else:
    value = 360 - absDiff

When I run the code through a compile and substitute the Az_1 and Az_2 variables with integers, the code works, yet I get the following error when executing the model the code is a part of in QGIS:

TypeError: unsupported operand type(s) for -: 'NoneType' and 'float' 
5
  • 2
    At least one of <Az_1> values must be NULL. Check <Az_1> values in Attribute Table. – Kadir Şahbaz Nov 23 '20 at 19:09
  • What do you mean by "running the code through a compile"? – Kadir Şahbaz Nov 23 '20 at 19:12
  • Compiler. Thank you for the help, I will check for null values. – tacarn Nov 24 '20 at 13:27
  • I removed Null values by using the Extract by Expression algorithm to extract features where "Az_1 IS NOT NULL AND Az_2 IS NOT NULL" into a new temporary layer, but I am still getting the same error. – tacarn Nov 24 '20 at 14:09
  • 1
    Use "Az_1 IS NOT NULL OR Az_2 IS NOT NULL" to extracte features into a temporary layer, then try again. This may work. – Kadir Şahbaz Nov 24 '20 at 14:29
0

I came up with the following statement which checks for a 'none' value, and if detected, reassigns it a value of 181. Since 181 is not a valid difference between two azimuths, it can then be deleted using the Extract by Value algorithm. If the value of Az_1 and Az_2 are both numbers, the program calls a function which will run the azimuth calculation code described in the original post.

if Az_1 is None:
    value = 181
elif AZ_2 is None:
    value = 181
else:
    value = abs(AzimuthDifference())

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.