2

I'm a rookie in python programming. I want to do map algebra for multiple rasters but these rasters are in different folders. Here's the code ive created so far:

#Importing modules
import arcpy
from arcpy import env
from arcpy.sa import *

#Files name
name1 = "BR_CRU_PRE"
name2 = "BR_CRU_ETP"

#Workspace
path = r"C:\Users\Ibytee\Documents\FUNCEME\ParaGiullian\Dados_ArcGIS\PRE_ETP"
env.workspace = path

#Checking for extension
arcpy.CheckOutExtension("Spatial")

#Creating raster lists
folders = ["PRE","ETP"]
env.workspace = path +"\\"+ folders[0]
a = arcpy.ListRasters(name1+"*", "TIF")
env.workspace = path +"\\"+ folders[1]
b = arcpy.ListRasters(name2+"*", "TIF")

#Executing the loop
for p, etp in zip(a, b):
     print p
     outRaster = Raster(p) - Raster(etp)
     outRaster.save(path+"\\PRE_ETP_SUB\\"+"sub_"+name1+"_%s" %etp)
     print "sub_"+name1+"_%s" %etp

This script seems to be accessing the right folders and creating the raster list, but when it comes the time to do the map algebra, looks like the script searches the rasters in the wrong folder. Here's the error I get:

C:\Python27\ArcGIS10.1\python.exe C:/Users/Ibytee/Documents/eclipse_workspace/Scripts/teste.py
BR_CRU_PRE_ANO1971.tif
Traceback (most recent call last):
  File "C:/Users/Ibytee/Documents/eclipse_workspace/Scripts/teste.py", line 24, in <module>
    outRaster = Raster(p) - Raster(etp)
RuntimeError: ERROR 000732: Input Raster: Dataset BR_CRU_PRE_ANO1971.tif does not exist or is not supported

Process finished with exit code 1
  • For future reference, learn to love os.path.join. For example os.path.join(path, 'PRE_ETP_SUB', 'sub_%s_%s' % (name1, etp)) – Mike T Aug 21 '14 at 22:59
1

For this to work, the variables p and etp must have the complete path, since the rasters they reference are located in separate directories.

So changing this:

outRaster = Raster(p) - Raster(etp)

To this:

outRaster = Raster(path +"\\"+ folders[0] + "\\" + p) - Raster(path +"\\"+ folders[1] + "\\" + petp)

...would seem to be a quick band aid. You might consider trying to tighten up the way you are handling these two directories though, as it currently is it seems a bit shaky to me.

0

This is a similar solution. We are subtracting rasters from two seperate folders (folder1 and folder 2) and saving them out in a new folder giving them the same names as the files in folder 1.

#Import arcpy packages into python
import arcpy,os,sys,string
from arcpy import env
from arcpy.sa import *

#Define folder locations    
folder1 = r'C:\SomeFolder1'
folder2 = r'C:\SomeFolder2'
out_folder = r'c:\SomeOutFolder'

#Call License
arcpy.CheckOutExtension("Spatial")

#Creating raster lists
folders = [folder1, folder2]
env.workspace = folder1
a = arcpy.ListRasters()
env.workspace = folder2
b = arcpy.ListRasters()

#Executing the loop
for file1, file2 in zip(a, b):
print file1 #This just gives you a list of file names for reference
outRaster = Raster(folder1 + "/" + file1) - Raster(folder2 + "/" + file)
outRaster.save(out_folder + os.sep + file1) #file1 was selected to duplication file1 names for output.

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.