1

I have 31 rasters that represent different watersheds and I would like to use the Fill (Spatial Analysis) tool to fill the sinks of all rasters at once in Python. All the rasters have the same name followed by a number (0-30).

Here is my code so far:

Import arcpy
From arcpy import env
From arcpy.sa import *
Arcpy.env.workspace = (r“Workspace\Path”)

For I in range (31):

outFill = Fill(“raster_name”+str(i)+“.tif”)
outFill.save (“Workspace\Path”)

So far this code only fills sinks of the raster_name0 and does not put it in the folder location I specify. Any ideas on how to modify this code to work on all 31 rasters and save them in a specficed location?

I have ArcGIS 10.1 with an advanced arcinfo license.

2 Answers 2

3

you should be carefull that python is case sensitive. There is also no indentation to define your "for" loop, but I guess you have it in your code. Try this :

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.env.workspace = (r“Workspace/Path”)

for i in range (31):
    outFill = Fill(“raster_name”+str(i)+“.tif”)
    outFill.save (“Save/Location/Folder/out_raster_name”+str(i)+“.tif”) 
1
  • Thank you radouxju! I'm new to python (a few days in) so there will be small errors.
    – JC11
    Commented Feb 5, 2014 at 14:20
1

New to Python is a good thing! A couple more tips:

"from arcpy.sa import *" will lead to name collision problems sooner or later. So do "from arcpy import sa" and then call "sa.Fill()".

You can also make your program insensitive to the number of files by doing this:

import glob
from arcpy import sa
arcpy.env.workspace = (r“Workspace/Path”)
for filename in glob.glob("to_be_filled*.tif"):
    outFill = sa.Fill(filename)

No need to edit the script if the number of files on disk changes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.