3

How do I ask a "for loop" to operate on groups of three files folder (e.x. files A, B, C), then files B, C, D, and then files C, D, E, and so on?

I have a folder full of rasters (.tif): RasterA.tif, RasterB.tif, RasterC.tif, RasterD.tif...and so on. I’ve got a python script that can run the batch conditional test I want. However, it only runs through one raster at a time, and I’d like it to run through multiple.

I think it’s easier to show via code. Please see below.

# Omitted the top part of the script for readability. 
# I have all the imports (arcpy and os), set the environment, set overwrite, set cell size
# Please let me know if I need to add this top part back in to the question and I will
rasters = arcpy.ListRasters("*", "ALL")
for raster in rasters:
 inRas = Raster(raster) #pulls the first raster (ex. Raster A)
 inRas2 = Raster(raster) 
 # Right now I know this means inRas and inRas2 are the same thing. 
 # What I'd like to do is have inRas2 be the next raster "in line" (ex. Raster B) 
 inRas3 = Raster(raster) 
 # Same deal as above, would like it to be Raster C
 # The CON test itself 
 output = arcpy.sa.Con(inRas >= 25, 1, 0) + arcpy.sa.Con(inRas2 >= 20, 1, 0) + arcpy.sa.Con(inRas3 >= 15, 1, 0)
 # My output raster needs to be identified by the first file in the group
 # This part runs fine, can ignore in context of question 
 RasName, RasExt = os.path.splitext(raster)
 output.save(outWorkspace + RasName + ".tif")

The fixed script is below. Everything ran without problem.

# Get a list of Rasters 
rasters = arcpy.ListRasters("*", "ALL")
# set it so can pull group of rasters with indexing 
n = len(rasters)
# run the con test on batch, in groups of 3
for i in range(2,n):
 rasterA = Raster(rasters[i-2])
 rasterB = Raster(rasters[i-1])
 rasterC = Raster(rasters[i])
 output = arcpy.sa.Con(rasterA >= 25, 1, 0) + arcpy.sa.Con(rasterB >= 20, 1, 0) + arcpy.sa.Con(rasterC >= 15, 1, 0)
 RasName, RasExt = os.path.splitext(rasters[i-2])
 output.save(outWorkspace + RasName + ".tif")
 # I have it print just to make sure naming files goes right
 print RasName
 print RasExt
 print "Done"
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 2, 2014 at 15:08
2
  • [rasters[i:(i+3)] for i in range(0, len(rasters) - 2)] Commented Sep 3, 2014 at 2:12
  • FelixP and PolyGeo suggested a similar fix, which worked. Thank you! Commented Sep 3, 2014 at 14:28

2 Answers 2

2
n= len(rasters)
for i in range(2,n):
 rasterA=rasters[i-2]
 rasterB=rasters[i-1]
 rasterC=raster[i]
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Sep 3, 2014 at 1:47
2
  • This worked! Thank you PolyGeo and FelixP. I will add the fixed script to my original question. Commented Sep 3, 2014 at 14:23
  • If you work a lot with SA use from arcpy.sa import *. Code will look nicer, e.g. output=Con(raster>=25,1,0). Your code can be simplified output=(rasterA>=25)+(raster>=20)+(raster>=15) Commented Sep 3, 2014 at 22:09
1

I do most of my programming in C++, but have some knowledge of python and arcpy. Have you tried this?

for raster in rasters:
 if raster < rasters-2
 inRas = Raster(raster) #pulls the first raster (ex. Raster A)
 inRas2 = Raster(raster+1) 
 # Right now I know this means inRas and inRas2 are the same thing. 
 # What I'd like to do is have inRas2 be the next raster "in line" (ex. Raster B) 
 inRas3 = Raster(raster+2) 
answered Sep 2, 2014 at 20:03
3
  • Thank you for the suggestion. Unfortunately it didn't seem to work. I just get the error: if raster < rasters-2: TypeError: unsupported operand type(s) for -: 'list' and 'int' Commented Sep 2, 2014 at 21:26
  • Ok, I see. What you need to do when you assign inRas2 and inRas3 is assign them an index 1 or 2 ahead of the current raster. Apparently I'm a little rusty with my python syntax. The if statement is just to stop the loop from assigning rasters outside of the list index. You can remove the if statement and try to run it, but the code will crash when it gets to the third to last raster in the folder Commented Sep 2, 2014 at 21:51
  • The fix suggested below worked, so we're all good. Indexed, just as you said. Commented Sep 3, 2014 at 14:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.