Select to view content in your preferred language

Help with my code.

405
6
Jump to solution
04-09-2025 07:39 AM
AndiDelimurati
Occasional Contributor

I am writing a code to input a color ramp into a raster. I have made it work for a single file. When I try to insert two or more rasters, my for loop doesnt iterate. Even when i try to put a counter it does the counters number and then stops. Tried checking if it was a problem with a return statement but the same issue applies. 

0 Kudos
1 Solution

Accepted Solutions
AndiDelimurati
Occasional Contributor

I found a solution to this problem by including a while loop before the for loop. This helped by selecting each raster one by one when I did raster[i]. 

View solution in original post

0 Kudos
6 Replies
AlfredBaldenweck
MVP Regular Contributor

Please share your code

AndiDelimurati
Occasional Contributor

Gonna write only the part of execution:

I=0

For raster in input_rasters

Arcpy.addmessage(raster)

Rasters=arcpy. Raster(raster)

Output=arcpy.ia.apply(Rasters, xml_file)

Output.save("Raster_Changed")

i+=1

Return

0 Kudos
BrennanSmith1
Frequent Contributor

When sharing code samples, use the </> button for "Insert/Edit Code Sample" (you might need to click the three dots to open all the formatting options), this makes it a lot easier to read.

It looks like your issue is that inside your 'for loop', the output is being saved as the string "Raster_Changed" every time. So you'll only ever see the result of the final 'for loop'. I don't know what the entries in your 'input_rasters' list look like, but try saving the Output as something like raster+"_Changed" and see if that works. You might need to use the 'os' module to extract just the final raster name, if you are using complete file paths in your list.

David_Lindsey
Frequent Contributor

This was my thought as well. Looked like the same output was being overwritten each time. I'm also curious about the indentation of the return, whether it was inside the function's for loop or outside of the for loop. If the counter iterated once then stopped, that return could be triggering a premature exit from the for loop and function after the first pass.

0 Kudos
AndiDelimurati
Occasional Contributor

The idea was that it didnt pass to check the next item on the tuple. I thought of iterating it first but up to no luck until i added a while loop before the for loop. As for the output, I did it so it catched the name of the input raster with a str after the name. 

0 Kudos
AndiDelimurati
Occasional Contributor

I found a solution to this problem by including a while loop before the for loop. This helped by selecting each raster one by one when I did raster[i]. 

0 Kudos