0

I'm trying use raster calculator that can export a raster to do different average values for for multiple layers. For example, I want to do averages for only the values and layers that are greater than 0. I'm trying to input Python code but it keeps failing. Here is a video where I try and explain my project if you need more context. https://www.youtube.com/watch?v=bt6ecPGjjDA&t=24s

I've also tried putting single quotes on the line but there is still a syntax error.

>>> import arcpy
... from arcpy.sa import *
... f1 = arcpy.Raster("S:/npenme2/Faunalyze\Feature_KB3_Right_Radius_14497/value")
... f2 = arcpy.Raster("S:/npenme2/Faunalyze\Feature_KB1_Right_Radius_14471_RC11/value")
... f3 = arcpy.Raster("S:/npenme2/Faunalyze\Feature_KB4_Right_Radius_14510/value")
... if value > 1:
...     outraster = (f1 + f2 + f3)/3
... else:
...     outraster = (f1 + f2 +f3)/3 - 1
...     f123 = f1 + f2 + f3
... outraster = Con(f123 > 0, f123 / 3, f123 / 3 - 1)
... outraster.save(S:/npenme2/Faunalyze/rasterCalc)
... 
Parsing error SyntaxError: invalid syntax (line 12)
>>> 

I've also tried SQL on the actual raster calculator tool with this query. But it too does not work:

Con("Feature_KB3_Right_Radius_14497" + "Feature_KB4_Right_Radius_14510" + "Feature_KB1_Right_Radius_14471_RC11">0,(("Feature_KB3_Right_Radius_14497" + "Feature_KB4_Right_Radius_14510" + "Feature_KB1_Right_Radius_14471_RC11")/3), ("Feature_KB3_Right_Radius_14497" + "Feature_KB4_Right_Radius_14510" + "Feature_KB1_Right_Radius_14471_RC11")/3-1)) 
3

The error tells you exactly what is wrong and where IndentationError: expected an indented block (line 7). You need to indent lines in your if clause:

if value > 1:
    outraster = (f1 + f2 + f3)/3
else:
    outraster = (f1 + f2 +f3)/3 - 1

However, this code won't do what you want it to. You need to use Con:

f123 = f1 + f2 + f3
outraster = Con(f123 > 0, f123 / 3, f123 / 3 - 1)
outraster.save('S:/npenme2/Faunalyze/rasterCalc')
  • Thank you and sorry I accidentally copied my previous edit. I fixed the indentation error. I added the con statement but I'm still getting an error. I updated my question. – Neharika Penmetcha Mar 23 at 15:24
  • The if value > 1 won't work, that is what the Con replaces, remove it. Also, you didn't quote the filepath, see edit. – user2856 Mar 23 at 20:11

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.