Raster Calculator ArcMap Python

4768
23
03-17-2020 04:13 AM
Yaseen_TahaMustafa
New Contributor III

Hi All,
I am trying to do some calculation on Raster using raster calculator in python. I have several restaer and I implemented the calculation using loop. I was working well when I was using ArcMap 9, and now I am using version 10.7. but it gives some errors. The code is below:

import sys, string, os, arcgisscripting, math, arcpy
from arcpy import env
from arcpy.sa import *
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
arcpy.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.7/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")
gp.workspace = "E:/JASTTpaperWork/Data/Processed/TIFF/"
out_workspace = "E:/JASTTpaperWork/Data/Processed/TIFF/ArcGIS02"
InMask = "E:/JASTTpaperWork/Data/Shapefile/KRG.shp"
# Get a list of grids in the workspace.
rasters = gp.ListRasters("","TIF")
raster = rasters.next()
while raster:
    print raster
    raster = rasters.next()
rasters.reset()
raster = rasters.next()
print gp.GetMessages()
while raster:
    # Set local variables
    InRaster =  raster
    # Set the outputname for each output to be the same as the input.
    OutRaster = out_workspace + "/" + raster
    InExpression ="Con("+ InRaster +" <=  - 1,0,"+ InRaster +")"
    arcpy.gp.RasterCalculator_sa(InExpression, OutRaster)
    gp.CheckOutExtension("Spatial")
    print raster
    #print InExpression
    raster = rasters.next()
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()

Any help would be highly appreciated. 

0 Kudos
23 Replies
DanPatterson_Retired
MVP Emeritus

The errors?

Yaseen_TahaMustafa
New Contributor III

Sorry the errors are:

Traceback (most recent call last):
File "E:/JASTTpaperWork/Data/testetstetstetstsCON.py", line 27, in <module>
arcpy.gp.RasterCalculator_sa(InExpression, OutRaster)
File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1)

0 Kudos
DanPatterson_Retired
MVP Emeritus

could be

Raster Calculator—Help | Documentation 

Note:

The Raster Calculator tool is not intended to be used in scripting environments and is not available in the standard Spatial Analyst ArcPy module. To learn more about scripting and Map Algebra, see the help topic What is Map Algebra? in the Spatial Analyst extension help.

You will probably have to implememt 'Con' directly

Con—Help | Documentation 

0 Kudos
by Anonymous User
Not applicable

Looks like its the '/'.  Place an 'r' in the beginning to declare it a raw string and change slashes to be backslashes throughout the code-

gp.workspace = r"E:\JASTTpaperWork\Data\Processed\TIFF"
out_workspace = r"E:\JASTTpaperWork\Data\Processed\TIFF\ArcGIS02"‍‍

It also looks like you are checking out 'spatial' twice.  A little bit of code to help with checking out licenses:

if arcpy.CheckExtension("Spatial) == "Available":
    arcpy.AddMessage("Checking out {} licence".format(extlicense))
    # Check out the extension if available
    arcpy.CheckOutExtension(extlicense)
else:
    # If extension is not available
    arcpy.AddMessage("!!!! Failed !: {}".format(arcpy.GetMessages(0)))
    sys.exit(0)


# And then return it when you're done:‍‍‍‍‍‍‍‍‍‍‍
arcpy.CheckInExtension("Spatial")‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Emeritus

Forward slashes are valid path separators.  Raw encoding, double back slashes or single forward slashes will be parsed properly.  Folders with spaces or characters other than letters and numbers and the underscore are iffy.

by Anonymous User
Not applicable

My bad.  I'm used to raw encoding and coding backslashes and forget sometimes.

Line 8 does have that iffyness ... 'C:\Program Files (x86)\ArcGIS\Desktop10.7\'...

Yaseen_TahaMustafa
New Contributor III

Dear Jeffy and Dan, Thanks for the quick response. I have followed your response and double checked each line of the cod. however I got the same error.

Any suggestions please

0 Kudos
DanPatterson_Retired
MVP Emeritus

so you replaced the raster calculator and implemented Con directly from the spatial analyst tools (see the Note

0 Kudos
Yaseen_TahaMustafa
New Contributor III

Actully I did not get the point. 

I exported the python code from module builder and Iater expanded the code through reading and exporting the rasters. So I do not know what do you mean. 

0 Kudos