Iterate and add rasters from multiple folders

2560
8
Jump to solution
01-18-2013 06:37 PM
GeorgeSutherland
New Contributor
Hello,

I am trying to add raster files from four folders together (i.e. file1 from folder1 + file1 from folder2 +file1 from folder3 + file1 from folder4). This would be easy enough to do with the Raster Calculator tool, however there are 90 files in each folder, each needing to be combined with the corresponding files in separate folders.

Using Model Builder I have create a model that allows me to select four input files to be added together, and assigns the output raster to a new location. I can batch process this, but when I press "fill" to fill in the other 90 files for each of the four folders, the original four files just get filled in the whole way, resulting in me having to manually change each of the names.

Just wondering if there is any way to build a model or write a script that will iterate through all four folders, pull out the same file in each folder, and add them together, for example:
file1 from folder1 + file1 from folder2 +file1 from folder3 + file1 from folder4 = output1,
file2 from folder1 + file2 from folder2 +file2 from folder3 + file2 from folder4 = output 2
.....
file'n' from folder1 + file'n' from folder2 +file'n' from folder3 + file'n' from folder4 = output'n'

Thanks in advance for any help!
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi George,

You could accomplish this in a python script.  Ex:

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)


What the code is doing is iterating through folder1 and appending all rasters to a list.  Then the code iterates through the list and finds each raster in folder2, folder3, and folder4.  With a python script you cannot use the Raster Calculator, so you will need to use the Plus tool for spatial analyst.  Therefore, you will need to sum the rasters from folder1 and folder2, then sum the rasters folder3 and folder4, and then finally sum the outputs from these two.

View solution in original post

0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor
Hi George,

You could accomplish this in a python script.  Ex:

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)


What the code is doing is iterating through folder1 and appending all rasters to a list.  Then the code iterates through the list and finds each raster in folder2, folder3, and folder4.  With a python script you cannot use the Raster Calculator, so you will need to use the Plus tool for spatial analyst.  Therefore, you will need to sum the rasters from folder1 and folder2, then sum the rasters folder3 and folder4, and then finally sum the outputs from these two.
0 Kudos
AngiraBaruah
New Contributor

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)

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Angira,

What is the error you are receiving?

0 Kudos
AngiraBaruah
New Contributor

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

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Replace:

import arcpy.sa

with:

from arcpy.sa import *

0 Kudos
AngiraBaruah
New Contributor

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

0 Kudos
JakeSkinner
Esri Esteemed Contributor

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)

0 Kudos
AngiraBaruah
New Contributor

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

0 Kudos