Solved! Go to Solution.
import arcpy from arcpy import env from arcpy.sa import * env.workspace = r"C:\folder1" list = [] for raster in arcpy.ListRasters("*"): list.append(raster) for n in list: env.workspace = r"C:\folder2" if n in arcpy.ListRasters("*"): ras2 = n outPlus1 = Plus(n, ras2) env.workspace = r"C:\folder3" if n in arcpy.ListRasters("*"): ras3 = n env.workspace = r"C:\folder4" if n in arcpy.ListRasters("*"): ras4 = n outPlus2 = Plus(ras3, ras4) outPlus3 = Plus(outPlus1, outPlus2) outPlus3.save(r"C:\output" + "\\" + n)
import arcpy from arcpy import env from arcpy.sa import * env.workspace = r"C:\folder1" list = [] for raster in arcpy.ListRasters("*"): list.append(raster) for n in list: env.workspace = r"C:\folder2" if n in arcpy.ListRasters("*"): ras2 = n outPlus1 = Plus(n, ras2) env.workspace = r"C:\folder3" if n in arcpy.ListRasters("*"): ras3 = n env.workspace = r"C:\folder4" if n in arcpy.ListRasters("*"): ras4 = n outPlus2 = Plus(ras3, ras4) outPlus3 = Plus(outPlus1, outPlus2) outPlus3.save(r"C:\output" + "\\" + n)
I tried incorporating this script to add rasters but I keep getting errors. Here's the code I used:
import arcpy, os
from arcpy import env
import arcpy.sa
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/DN'
env.overwriteOutput = True
raster_list = []
for raster in arcpy.ListRasters("*"):
raster_list.append(raster)
for r in raster_list:
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/Cloud'
if r in arcpy.ListRasters("*"):
cloud = r
outPlus1 = Plus(r, cloud)
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/Shadow'
if r in arcpy.ListRasters("*"):
shadow = r
outPlus2 = Plus(outPlus1,shadow)
outPlus2.save(r"D:\Batch_Research_Test\Add_Rasters\Output" + "\\" + r)
Hi Angira,
What is the error you are receiving?
Hi Jake,
Thanks for your email. Here's what I get when I run the code:
==================================================================
"Traceback (most recent call last):
File
"C:\Python27\ArcGIS10.1\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 326, in RunScript
exec codeObject in __main__.__dict__
File "D:\Batch_Research_Test\Scripts\Test\Add_Rasters2.py", line 17, in
outPlus1 = Plus(r, cloud)
NameError: name 'Plus' is not defined"
===================================================================
Thanks!
Angira
Replace:
import arcpy.sa
with:
from arcpy.sa import *
I made that change and here's what I got:
=============================================================
"Traceback (most recent call last):
File
"C:\Python27\ArcGIS10.1\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 326, in RunScript
exec codeObject in __main__.__dict__
File "D:\Batch_Research_Test\Scripts\Test\Add_Rasters2.py", line 17, in
outPlus1 = Plus(r, cloud)
NameError: name 'cloud' is not defined"
==============================================================
Thanks!
Angira
It doesn't look like any rasters are being found in your folder:
'D:/Batch_Research_Test/Add_Rasters/Cloud'
Try adding some print statements to further debug this code. Ex:
import arcpy, os
from arcpy import env
from arcpy.sa import *
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/DN'
env.overwriteOutput = True
raster_list = []
for raster in arcpy.ListRasters("*"):
raster_list.append(raster)
for r in raster_list:
print r
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/Cloud'
if r in arcpy.ListRasters("*"):
print r
cloud = r
else:
print 'raster not found'
outPlus1 = Plus(r, cloud)
env.workspace = 'D:/Batch_Research_Test/Add_Rasters/Shadow'
if r in arcpy.ListRasters("*"):
print r
shadow = r
else:
print 'raster not found'
outPlus2 = Plus(outPlus1,shadow)
outPlus2.save(r"D:\Batch_Research_Test\Add_Rasters\Output" + "\\" + r)
Yes you are right. However the rasters do exist in the "Cloud" folder. Each
folder has raster files with different names. Here's the list of folders
with the raster names:
1. DN: dn_isc_atm_comp_2000_02_01_tm7.img
2. Cloud: cloud_isc_atm_comp_2000_02_01_tm7.img
3. Shadow: shadow_isc_atm_comp_2000_02_01_tm7.img
I just ran your code and here's what I got:
============================================================================
>>> dn_isc_atm_comp_2000_02_01_tm7.img
raster not found
Traceback (most recent call last):
File
"C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 326, in RunScript
exec codeObject in __main__.__dict__
File "D:\Batch_Research_Test\Scripts\Test\Add_Rasters_JS.py", line 20, in
============================================================================
Thanks!
Angira