1

I have 5 folders, each with 80 .tif files. These 80 files represent one landsat image taken on a different date. Each folder represents a vegetative indice (NDVI, NDII etc.)

Each file in each folder is listed in the same order, based on date. So the first file in all five folders correspond to each other. I want to add all 5 .tif files together (for all 80 dates) using map algebra. The code I am trying so far is this:

import arcpy
from arcpy import *
arcpy.CheckOutExtension('Spatial')
arcpy.env.overwriteOutput = True

arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\NDVI'
NDVIrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\NDII'
NDIIrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\RGR'
RGRrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\SWIR32'
SWIR32rasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\SATVI'
SATVIrasters=arcpy.ListRasters('*.tif*')
outpath='F:\Sheyenne\Normalized_Indices\Composited\\'
for ndviraster in NDVIrasters:
    ras1 = Raster(ndviraster)
    for ndiiraster in NDIIrasters:
        ras2=Raster(ndiiraster)
        for rgrraster in RGRrasters:
            ras3=Raster(rgrraster)
            for swir32raster in SWIR32rasters:
                ras4=Raster(swir32raster)
                for satviraster in SATVIrasters:
                    ras5=Raster(satviraster)
                    outRaster = ras1 + ras2 +ras3 +ras4 + ras5
                    outpath=outpath + ras1
                    outRaster.save(outpath)

but I think there is something wrong with how I am doing the nested loop, first of all, when I simply print ras5 at the end, it lists more than the 80 files found in the pathway r'F:\Sheyenne\Normalized_Indices\SATVI'

The actual error returned is:

Traceback (most recent call last):

  File "<ipython-input-61-567d86ebd99a>", line 1, in <module>
    runfile('F:/python codes/normalize_indices.py', wdir='F:/python codes')

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "F:/python codes/normalize_indices.py", line 98, in <module>
    outpath=outpath + ras1

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 4143, in Plus
    in_raster_or_constant2)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 4140, in Wrapper
    ["Plus", in_raster_or_constant1, in_raster_or_constant2])

RuntimeError: ERROR 000732: Input Raster: Dataset F:\Sheyenne\Normalized_Indices\Composited\ does not exist or is not supported

but that pathway definitely does exists for the outpath. Possibly an indenting issue in the last three lines?

EDIT:

I figured out the problem with the loop based on below answers and comments.

I am now using this:

outpath='F:\Sheyenne\Normalized_Indices\Composited\\'
for ndvi, ndii, rgr, swir32, satvi in zip(NDVIrasters, NDIIrasters,RGRrasters, SWIR32rasters, SATVIrasters):
    outraster= ndvi +ndii + rgr + swir32 +satvi
    outpath=outpath + ndvi
    outraster.save(outpath)
    print outpath

but the out path is using iteration to ad the raster names to the pathway. So the first outpath is the first raster in my list. But the second one is the first and second combined, and the third one is the first through third combined etc.

5

These nested loops will give you every combination of every raster:

for ndviraster in NDVIrasters:
    ras1 = Raster(ndviraster)
    for ndiiraster in NDIIrasters:
        ras2=Raster(ndiiraster)

Instead you probably want to do something like:

for idx in range(0, len(NDVIrasters) - 1):
  ras1 = NDVIrasters[idx]
  ras2 = NDIIrasters[idx]
  # etc

This will take the first NDVI raster, the first NDII raster (etc) the first time it runs, then the second of each type of raster the second time it runs (etc)

  • something is still off with the outpath location. When I print it the first time it runs it shows this, ` F:\Sheyenne\Normalized_Indices\Composited\Sheyenne_19840517.tif which is my first raster, the second time it runs it shows this, F:\Sheyenne\Normalized_Indices\Composited\Sheyenne_19840517.tifSheyenne_19840704.tif which is my first and second raster, and this continues until all 80 are listed in the final loop through – Stefano Potter Mar 1 '16 at 3:51
  • 1
    Right answer. Better implementation is something like for ndvi, ndii in zip(NDVIrasters, NDIIrasters): .... – sgillies Mar 1 '16 at 3:55
  • these seems to work too, but again, I can't figure out the naming for outpath – Stefano Potter Mar 1 '16 at 3:58
  • You're setting outpath outside of the loops, then sequentially adding the dataset name to it. Instead, try setting outpath within the for loop – Stephen Lead Mar 1 '16 at 5:25

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.