Raster Calculator ArcMap Python

4758
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
DavidPike
MVP Frequent Contributor

I've updated it to arcpy.sa.Con...

I've tested it with dummy data on my system and it works. If this doesn't work i'd suggest it's a corruption of your raster(s) or a file naming or path issue.

import arcpy
import os


arcpy.CheckOutExtension("Spatial")


arcpy.env.workspace = r"D:\test"
out_workspace = r"D:\output"

#InMask = r"E:/JASTTpaperWork/Data/Shapefile/KRG.shp"

# Get a list of grids in the workspace.
rasters = arcpy.ListRasters("","TIF")

for raster in rasters:

    # Set the outputname for each output to be the same as the input.
    output_path = os.path.join(out_workspace,raster)

    #create raster object
    raster_path = os.path.join(arcpy.env.workspace,raster)
    raster_object = arcpy.Raster(raster_path)

    #return conditional raster
    con_raster = arcpy.sa.Con(raster_object <= -1, 0, raster_object)
    
    #save it
    con_raster.save(output_path)

arcpy.CheckInExtension("Spatial")

Yaseen_TahaMustafa
New Contributor III

Dear David, 

Thanks a lot.... it is working very well

Highly appreciated 

0 Kudos
DanPatterson_Retired
MVP Emeritus

rename the output so it doesn't begin with a number.

Is the destination location folder on a 'real' drive? or is it a virtual drive or thumb drive? 

Make a folder called C:\myoutput and save the results there.  I would also suggest that you copy your script and run it from there as well. 

Yaseen_TahaMustafa
New Contributor III

Thanks Dan 

Your suggestion is indeed helpful......

0 Kudos