1

I am trying to convert a process usually used in ArcGIS Tools to arcpy in a Jupyter notebook. I have a very long Con statement that basically takes all the values of 10 rasters and adds them together. I have tested a small snippet of the code to make sure the syntax is correct, but I am running into errors saying 'TypeError: IsNull() takes 1 positional argument but 3 were given'

# Import system modules
import arcpy
import os

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
 
from arcpy.sa import *

# Set overwrite option
arcpy.env.overwriteOutput = True

# Set the workspace for ListFeatureClasses
arcpy.env.workspace = r"G:\Projects\bdv_2020Q4.gdb"
print(arcpy.env.workspace)

# Set the extent environment using the Extent class.
arcpy.env.extent = arcpy.Extent(-180.0, 90.0, 180.0, -90.0)

# Set the cell size environment using a number.
arcpy.env.cellSize = 0.0083333333

#Con statement
outputRaster = Con(IsNull(arcpy.Raster("IUCN_1a"),0,"IUCN_1a")) + Con(IsNull(arcpy.Raster("IUCN_not_reported"),0,"IUCN_not_reported"))
outputRaster.save("comb_test2")
1
  • If you newline and indent with each new paren, it's easier to find syntax errors. You can save space by making variables of all the Raster up front – Vince Aug 19 '20 at 12:57
2

Check your parentheses. Right now it looks like the second and third Con inputs are getting included in the IsNull statement. Move one of the closed parentheses at the end of each Con statement up to just after the Raster() call (before the first comma).

5
  • Thank you @ycartwhelen. I have amended the code and it runs, but the output isnt correct. Perhaps I need to add Raster() to the second part of each con statement? The current con statement is (apoligies, I dont know how to input actual code in the comments): #Con statement outputRaster = Con(IsNull(arcpy.Raster("IUCN_1a")),0,"IUCN_1a") + Con(IsNull(arcpy.Raster("IUCN_not_reported")),0,"IUCN_not_reported") – ChristineF Aug 19 '20 at 13:12
  • Looking at your code it looks like you have two things going - one is using Con statements to change the Null value to zeros, the other is summing the ten rasters. You might want to break this into two separate pieces. First, loop through all the rasters and use Con to deal with the Nulls, and then second after the loop finishes sum the rasters. This might help you isolate where the problems are coming from, and would reduce the chance of errors with Con parentheses. Editing your original post to add more details on how your output is different from what was expected would also be helpful. – ycartwhelen Aug 19 '20 at 13:25
  • Also check if you're summing the rasters correctly, or if this would be better done with a Map Algebra or Raster Calculator command. – ycartwhelen Aug 19 '20 at 13:26
  • I would recommend accepting the answer if it cleared your error and then opening a new question to address the syntax for your con statement. – Aaron Aug 19 '20 at 13:47
  • And there are a ton of existing GIS SE questions related to Con statement syntax, so take a look at those before posting a new question on the subject to make sure you aren't posting a duplicate of an existing question. – ycartwhelen Aug 19 '20 at 14:21

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.