0

I am trying to calculate a sum for a set of raster but before I need to disregard No Data values (that are equal to -9999 for my rasters) by using SetNull function. It gives an error.

What could be the problem?

Here I'm attaching the function.

def __calc_seasonal_cumulative_raster(self, data_lst):
    print "\nCalculating seasonal cumulative raster."
    with open(self.log_file, 'a+') as f:
        f.write("\n\nCalculating seasonal cumulative raster.")
    outSetNull = sa.SetNull(data_lst, data_lst, "Value = -9999")
    cumu_ras = sa.CellStatistics(outSetNull, 'SUM', 'DATA')
    print "Calculated seasonal cumulative raster."
    with open(self.log_file, 'a+') as f:
        f.write("\nCalculated seasonal cumulative raster.")
    return cumu_ras

I am using arcpy. The error message is also attached.

Calculating seasonal cumulative raster.
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "H:/FEWS/KnowledgeBase/bin/global/yearly/chirts/global_yearly_chirts_products.py", line 228, in <module>
    main()
  File "H:/FEWS/KnowledgeBase/bin/global/yearly/chirts/global_yearly_chirts_products.py", line 201, in main
    calc_cumu(years, seasons, temp_dir)
  File "H:/FEWS/KnowledgeBase/bin/global/yearly/chirts/global_yearly_chirts_products.py", line 51, in calc_cumu
    my_global_season.create_seasonal_cumulative_raster()
  File "H:\fews\knowledgebase\lib\yearly_chirts_global_processes.py", line 199, in create_seasonal_cumulative_raster
    cumu_ras = self.__calc_seasonal_cumulative_raster(seasonal_rasters)
  File "H:\fews\knowledgebase\lib\yearly_chirts_global_processes.py", line 795, in __calc_seasonal_cumulative_raster
    outSetNull = sa.SetNull(data_lst, data_lst, "Value = -9999")
  File "C:\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 340, in SetNull
    where_clause)
  File "C:\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\ArcGIS\Desktop10.6\ArcPy\arcpy\sa\Functions.py", line 335, in Wrapper
    where_clause)
  File "C:\ArcGIS\Desktop10.6\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool
  • 1
    Are you using arcpy? Which is the exact error you are receiving? Please, include these info in your question by clicking the Edit button. – umbe1987 Sep 5 at 12:50
  • 1
    Also, you should avoid using image to show your code. Rather, you should write the code directly in the question itself and formatting it as code. – umbe1987 Sep 5 at 12:53
  • Try changing your where clause to UPPERCASE ("VALUE = -9999" instead of "Value = -9999") – umbe1987 Sep 5 at 12:59
  • I've tried that before. Same thing – user148822 Sep 5 at 13:02
  • Can you run the SetNull geoprocessing tool from within ArcMap by specifiying the same query and see if it works? Maybe, backup your data before so you can replicate it from the script. If it succeeed, you can then open the Results window from Geoprocessing menu -> Results, right click on the SetNull tool and "copy as Python snippet" and see how the code should look like. – umbe1987 Sep 5 at 13:11
0

I don't know why using arcpy.sa module does not work, but you can try calling the SetNull GP tool directly with arcpy.gp.SetNull_sa(data_lst, data_lst, "Value = -9999") and see if it works.

The difference is that arcpy.sa is a module that lets you access a lot of different stuff

including tools, operators, functions, and classes

, whereas arcpy.gp gievs you direct access to the GP tools of the Spatial Analyst Toolbox.

UPDATE

As both methods fail, might be worth changing how you build your query.

Following Specifying a query in Python:

In Python, building expressions surrounded with triple quotes is both easier to read and to understand.

Also, it's better to use the AddFieldDelimiters function because the field delimiters used in an SQL expression differ depending on the format of the queried data.

Try this:

where_clause = """{} = -9999""".format(arcpy.AddFieldDelimiters(data_lst, "Value"))
outSetNull = sa.SetNull(data_lst, data_lst, where_clause)
  • I've tried that as well. And it didn't work – user148822 Sep 5 at 13:36
  • Did the tool work from ArcMap? Is the error the same? – umbe1987 Sep 5 at 13:38
  • It worked when i did it manually on one of the rasters through ArcMap. – user148822 Sep 5 at 13:40
  • Hopefully last question: is the raster that makes your script produce the error the same you used in ArcMap or another one? If it's another, I would try the SetNull tool manually on that in ArcMap and see if it fails. If it fails, it might be a problem of input raster. – umbe1987 Sep 5 at 13:41
  • It is the same raster. The only difference is that I'm trying to calculate it for 12 rasters through Python but manually I only tried with one raster at a time. – user148822 Sep 5 at 13:49

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.