Batch Raster Clipping

I have been searching for answers in the web but haven't find any plausible answers yet. What I want to do is automate the clipping of my raster files using a set of shapefiles. Basically, what I want to do is:

My initial code is like the one below which I got from exporting the model builder to a python script.

How can I automate my clipping process in such a way that it will loop to all my raster data and match each raster data with a specified shapefile. I am currently using ArcGIS 10.2. Can you help me build the code using the initial code that I have below?

This can all be done in model builder no need to create a python script. You need to read the help file about iterators and inline substitution. Read those pages to understand the model below which will do what you want.

Attachments

If I use the model that you suggested, can I input multiple raster layers (*.tif)? and when I run the model, it will automatically match the first shapefile in my folders with my first raster?

I have tried something similar before, but what it does is the first shapefile in my folder reiterates through all of my rasters in my Raster List ( i have used raster layer and enable "List of values" option) like:

Thanks for your reply and sharing the code. I would like to modify the code to incorporate calculation of cell statistics after the clipping process. Would you suggest incorporating it in the code that you gave me, or it will be much simpler to write another code just for calculating cell statistics.

I modified the code a bit to incorporate computation of cell statistics in the code. Basically what I have done so far is to create a container >>> outputlist = [] that will store the output of the clipping. Then used this list to compute for cell statistics. I think I got the code correctly until Line 28 >>> outputList.append(out_raster) but I have a problem in computing the cell statistics. The error says:

Traceback (most recent call last): File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in __main__.__dict__ File "E:\Python_scripts\Clip\rasterClip_with_cellstats.py", line 29, in <module> for raster in outputList():TypeError: 'list' object is not callable

I tried revising the code this morning and run it again, but the process is stuck on sumRaster = CellStatistics(ouputList, "SUM", "DATA"). Is this an indication that we cannot use list of raster values in Cellstatistics function?

When you say "stuck on the the sum raster process" what do you mean? If it just looks unresponsive that is probably because you are running this analysis on 37 rasters, so this could take a long time depending on the resolution of your rasters.

I tried revising the code this morning and run it again, but the process is stuck on sumRaster = CellStatistics(ouputList, "SUM", "DATA"). Is this an indication that we cannot use list of raster values in Cellstatistics function?

[in_raster_or_constant,...] A list of input rasters for which a statistic will be calculated for each cell within the Analysis window.

This code:

# output list is already a list, so no need to wrap it in [ ]
sumRaster = CellStatistics(outputList, "SUM", "DATA")
sumRaster.save(r'E:\Test_awd')

Should be correct because it is passing in a list of rasters for the sum analysis.

This part of your code is not correct:

# you do not want to have this in a loop, remove the for loop
for raster in outputList: # take this out
sumRaster = CellStatistics(outputList, "SUM", "DATA")
sumRaster.save(r'E:\Test_awd\test.tif')

because you are running the the cell statistics on the entire list of rasters for every iteration of your list (which would be 37 times if there are still 37 rasters in your list). You are also overwriting the test.tif every time as well.

Because you were checking the license in before running the tool, therefore you were now allowing access to spatial analyst tools. The full code I supplied in my last post should do the trick. I would suggest trying that and if it is hanging up on the sum statistics part, try running that as a separate script.

I am almost corrected the code. It seems to work properly. But my problem is the naming of the file. I am having trouble removing the .shp extension in my features list. For instance my filename input is out_prov + "clip.tif". When it saved the file it carries the .shp extension name of my feature list.

Sorry for this late reply. Why is it in the thread there is no option to mark the reply as best (check mark). I can only vote your reply but not mark it as best to signify that my query has been resolved.