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.
It means you can't use modelbuilder. The link that I provided is for the Con tool and you can use it directly instead of through a raster calculator expression. See the code example in the link
Thanks but I cannot see the link that you sent.... Could you please send the link.....
In the ArcMap help, search on Con... these are for ArcGIS Pro, but the help topic is the same
Thanks for the prompt response
import arcpy
import os
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r"E:/JASTTpaperWork/Data/Processed/TIFF/"
out_workspace = r"E:/JASTTpaperWork/Data/Processed/TIFF/ArcGIS02"
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 = Con(raster_object <= -1, 0, raster_object)
#save it
con_raster.save(output_path)
arcpy.CheckInExtension("Spatial")
def ty(name):
return print("Thanks" + name)
ty("Dave")
Dear David,
Thanks for the your genourus help. I tried to run the cond but I got an error as shown below:
Traceback (most recent call last):
File "E:\JASTTpaperWork\Data\ExtRasCal - Copy.py", line 23, in <module>
raster_object = raster(raster_path)
TypeError: 'unicode' object is not callable
I search online about this error but still I cannot figure out how to solve it.
Any suggestion.....!
I've changed this to arcpy.Raster
if that doesn't work try arcpy.Raster(raster)
Thank you for the reply. The previous error is gone after I tried your suggestion, and added some library in the top. the code is below:
importarcpy, sys, string, arcgisscripting, math, arcpy
importos
fromarcpy import env
fromarcpy.sa import *
gp = arcgisscripting.create()
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r"E:\\JASTTpaperWork\\Data\\Processed\\TIFF\\"
out_workspace = r"E:\\JASTTpaperWork\\Data\\Processed\\TIFF\\ArcGIS02\\"
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)
print output_path
#return conditional raster
con_raster = Con(raster_object <= -1, 0, raster_object)
#save it
con_raster.save(output_path)
arcpy.CheckInExtension("Spatial")
def ty(name):
return print("Thanks" + name)
ty("Dave")
However, this time another error appears which it seems to be a problem with the path of saving the data. The error is:
Traceback (most recent call last):
File "E:\JASTTpaperWork\Data\ExtRasCal - Copy.py", line 32, in <module>
con_raster.save(output_path)
RuntimeError: ERROR 000875: Output raster: E:\JASTTpaperWork\Data\Processed\TIFF\ArcGIS02\2000_EVI.tif's workspace is an invalid output workspace.
Any dvice....!
Try hardcoding a different path as the out workspace. look at your path formatting and check against examples in the esri help.
There's no need to keep adding those modules - sys, string, arcgisscripting, math. it just slows down your process.
Fine .... I rechecked the code and I change the path of the out workspace. The error is still there and I an really frustrating. The error message is:
Traceback (most recent call last):
File "E:\JASTTpaperWork\Data\ExtRasCal - Copy.py", line 35, in <module>
con_raster.save(output_path)
RuntimeError: ERROR 010240: Could not save raster dataset to 2000_EVI.tif with output format TIFF.
Assist please.....