2

In QGIS I have the following script that evaluates the results in each feature then gives it a value. Some of the values are NULL, which is fine, but how do I get the Advanced Python field calculator to deal with it?

I get the following error that I think is due to my NULL syntax:

FieldPyculator code execute error. Global code block can't be executed! SyntaxError invalid syntax (, line 4) Execution failed after 0.02 seconds

Attribute Table

def getValue(distance, SPECIES_NM, F_SEN_ZN_1):
    if distance == None:
        value = 0.50
    elif SPECIES_NM is not None AND F_SEN_ZN_1 == 'No' AND distance <= 100: 
        value = 0.75 
    elif SPECIES_NM is not None AND F_SEN_ZN_1 is None AND distance <= 100:
        value = 0.50
    elif SPECIES_NM is not None AND F_SEN_ZN_1 = 'No' AND distance <= 100:
        value = 0.50
    elif SPECIES_NM is None AND F_SEN_ZN_1 = 'Yes' AND distance <= 100:
        value = 1.0
    elif SPECIES_NM is not None AND F_SEN_ZN_1 = 'Yes' AND distance <= 100:
        value = 1.0
    elif distance is None and distance > 100:
        value = 0.50
    elif SPECIES_NM is not None AND F_SEN_ZN_1 = 'No' AND distance > 100:
        value = 0.50
    elif SPECIES_NM is not None AND F_SEN_ZN_1 is None AND distance > 100:
        value = 0.50
    elif SPECIES_NM is not None AND F_SEN_ZN_1 = 'No' AND distance > 100:
        value = 0.50
    elif SPECIES_NM is None AND F_SEN_ZN_1 = 'Yes' AND distance > 100:
        value = 0.75
    elif SPECIES_NM is not None AND F_SEN_ZN_1 = 'Yes' AND distance > 100:
        value = 0.75
    else:
        value = -0.999999999
    return value

and then calling the function

value = getValue(<distance, SPECIES_NM, F_SEN_ZN_1>)
4
  • 1
    You have 2 options here: 1) Write your code using QGIS expressions (e.g. using CASE), or 2) Convert your function to an expression function using the Function Editor (see how here). Jun 21, 2021 at 18:21
  • Thank you @German Carrillo. I forgot about using cases for this one. I will post answer below.
    – MJM
    Jun 22, 2021 at 0:31
  • 1
    Good, make sure you accept your own answer (I think you need to wait some hours for that) to properly close the question. Jun 22, 2021 at 0:42
  • Try replacing getValue(<distance, SPECIES_NM, F_SEN_ZN_1>) with getValue(<distance>, <SPECIES_NM>,<F_SEN_ZN_1>)
    – BERA
    Nov 17, 2021 at 9:26

1 Answer 1

2

As mentioned by @GermánCarrillo, the use use of CASEs was utilized for my exercise:

-- Cases of fish vulnerability 

-- 1. no data for location
CASE 
    WHEN "distance" IS NULL THEN 0.5
    -- 2. less 100m, fish present, sensitive habitat
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" = 'Yes') THEN 1.0
    -- 3. less 100m, fish present, not sensitive habitat
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" = 'No') THEN 1.0
    -- 4. less 100m, fish present, no habitat data
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.75
    -- 5. less 100m, no fish, sensitive habitat
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'Yes') THEN 0.75
    -- 6.less 100m, no fish, not sensitive
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'No') THEN 0.5
    -- 7. less 100m, no fish, no habitat data
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.5
    -- 8. less 100m, no fish data, sensitive habitat
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'Yes') THEN 1.0
    -- 9. less 100m, no fish data, not sensitive
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'No') THEN 0.5
    -- 10. less 100m, no fish data, no habitat data
    WHEN ("distance" <= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.5
    -- 11. greater than 100m, fish present, sensitive habitat
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" = 'Yes') THEN 0.75
    -- 12. greater than 100m, fish present, not sensitive
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" = 'No') THEN 0.75
    --13. greater than 100m, fish present, no habitat data
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NOT NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.75
    --14. greater than 100m, no fish, sensitive habitat
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'Yes') THEN 0.75
    --15. greater than 100m, no fish, not sensitive
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'No') THEN 0.75
    -- 16. greater than 100m, no fish, no habitat data
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.5
    -- 17. greater than 100m, no fish data, sensitive habitat
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'Yes') THEN 0.75
    -- 18. greater than 100m, no fish data, not sensitive
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" = 'No') THEN 0.5
    -- 19. greater than 100m, no fish data, no habitat data
    WHEN ("distance" >= 100 AND "SPECIES_NM" is NULL AND "F_SEN_ZN_I" IS NULL) THEN 0.5 
END

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.