1

I am currently learning arcgis and python.

The problem includes looping through 2 folders (with rasters of same filename) and performing raster calculation on each raster pair. Below is my code:

import arcpy, os, glob  
from arcpy.sa import *  
arcpy.CheckOutExtension("Spatial")  

ws1 = "E:\clip_qx"  
ws2 = "E:\clip_qy"  

outws = "E:\result"

rasterlist = arcpy.ListRasters()  
for r in ws1:  
    r1 = arcpy.sa.Raster(r)  
    r2 = arcpy.sa.Raster(os.path.join(ws2, basename))  
    result = sqrt ( r1^2 + r2^2 )  
    outname = basename    
    result.save(os.path.join(outws, outname))  

However, I am getting an error. It seems that the rasters were not read.

RuntimeError: ERROR 000732: Input Raster: Dataset E does not exist or is not supported.
2
  • 1
    Change your directory strings to raw format w1 = r"E:\clip_qx". Python interprets the backslash in a string as an indicator of a special character.
    – Bjorn
    Aug 2, 2017 at 16:48
  • 1
    also basename isn't defined. You probably mean r (the file name). Aug 2, 2017 at 17:46

2 Answers 2

1

You're iterating through the string ws1, instead of your list of rasters. Hence 'E' as not a supported input (the first character in string ws1). You need to create a list of rasters by setting the workspace and then using arcpy.ListRasters (). Try:

import arcpy, os, math
arcpy.CheckOutExtension("Spatial")  

ws1 = r"E:\clip_qx"  
ws2 = r"E:\clip_qy"  

outws = r"E:\result"

#set workspace
arcpy.env.workspace = ws1

rasterlist = arcpy.ListRasters()

for r in rasterlist:  
    r1 = arcpy.sa.Raster (r)  
    r2 = arcpy.sa.Raster(os.path.join(ws2, r))  
    result = math.sqrt ( r1^2 + r2^2 )      
    result.save(os.path.join(outws, r))  

However, this line will fail because you're trying to use python math operations on raster data:

result = math.sqrt ( r1^2 + r2^2 )

Look into raster math to learn how to execute this.

2
  • There seems to be wrong with the combination of rasters that were calculated. Kindly check this out
    – F Viray
    Aug 3, 2017 at 11:19
  • It's working now. Mistake on my end. Thanks again @Emil
    – F Viray
    Aug 4, 2017 at 1:51
0

are you doing some kind of ocean or wind modeling? i was lucky to find your question because i am dealing with the same issue.

i got the math function to work by:

import arcpy, os, math
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

and

result =  SquareRoot(r1 * r1 + r2 * r2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.