Select to view content in your preferred language

getting started with Python

1469
6
08-08-2011 08:16 PM
WadeGivens
Emerging Contributor
Well, finally breaking down and trying to get going with Python.  My first attempt is to create a batch raster clip tool that loops through a set of rasters, clips them, and saves then to another directory with "_clip" appended to the file name.  The script below runs with no errors.  Problem is no results either.  Any ideas, or thoughts to get me going in the right direction?

import arcpy
import os

inDIR = "C:/Temp/imagery"
outDIR = "C:/Temp/imagery/clipped"
clipFC = "C:/Temp/imagery/clip_feature.shp"

arcpy.env.workspace = inDIR

rasters = arcpy.ListRasters("*", "TIFF")

for raster in rasters:
 print raster
 outname = raster[:-4] + "_clip.tif"
 arcpy.Compression= "LZW"
 arcpy.Clip_Management(raster, "#", os.path.join(outDIR, raster), clipFC, "#", "ClippingGeometry")



Thanks!
Wade
Tags (2)
0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
I believe the problem is with the 'arcpy.ListRasters' function.  You will want to specify "TIF" instead of "TIFF".  Also, I believe you will want to specify 'os.path.join(OutDir, outname)' within the 'arcpy.Clip_management' function.  Ex:

rasters = arcpy.ListRasters("*", "TIF")

for raster in rasters:
 print raster
 outname = raster[:-4] + "_clip.tif"
 arcpy.Compression= "LZW"
 arcpy.Clip_management(raster, "", os.path.join(outDIR, outname), clipFC, "", "ClippingGeometry")
0 Kudos
WadeGivens
Emerging Contributor
Jake:

Thanks!  That did the trick.  Been kinda hard to get going with some of this.  Lots of posts out there that give examples using gp. calls.  Not so many using arcpy.  Trying to get going with arcpy since that is preferred syntax now.

Thanks again!

Wade
0 Kudos
WadeGivens
Emerging Contributor
Jake:

Now that I have this going, I've added the script to a toolbox in ArcMap and I'm trying to pass the workspace directories and clip shape as parameters (see below).  I get the following error when trying to run the tool from the toolbox:

Executing: Clip C:\Temp\imagery C:\Temp\imagery\clipped\clipped C:\Temp\imagery\all_corn_IMPACT_2011.shp
Start Time: Tue Aug 09 09:50:33 2011
Running script Clip...
ERROR 000714: Error in script Clip.
Error in executing: cmd.exe /C C:\GIS_PR~1\PYTHON~1\BATCH_~1  "C:\Temp\imagery" "C:\Temp\imagery\clipped\clipped" "C:\Temp\imagery\all_corn_IMPACT_2011.shp"

Failed to execute (Clip).
Failed at Tue Aug 09 09:50:33 2011 (Elapsed Time: 0.00 seconds)

import arcpy
import os

inDIR = arcpy.GetParameterAsText(0)
outDIR = arcpy.GetParameterAsText(1)
clipFC = arcpy.GetParameterAsText(2)

# inDIR = "C:/Temp/imagery"
# outDIR = "C:/Temp/imagery/clipped"
# clipFC = "C:/Temp/imagery/all_corn_IMPACT_2011.shp"

arcpy.env.workspace = inDIR

rasters = arcpy.ListRasters("*", "TIF")

for raster in rasters:
 print raster
 outname = raster[:-4] + "_clip.tif"
 arcpy.Compression= "LZW"
 arcpy.Clip_management(raster, "", os.path.join(outDIR, outname), clipFC, "", "ClippingGeometry")



Any ideas on what may be causing this error?

Thanks,
Wade
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Make sure you have the correct data types setup for the parameters.
0 Kudos
WadeGivens
Emerging Contributor
Still getting the same error:

Executing: Script C:\GIS_Projects\2011\CE_BU_RS\RapidEye\Aug_1st_week\images\5_band C:\GIS_Projects\2011\CE_BU_RS\RapidEye\Aug_1st_week\images\5_band\clipped C:\GIS_Projects\2011\CE_BU_RS\RapidEye\shapefile_bnds\all_corn_IMPACT_2011.shp
Start Time: Tue Aug 09 10:30:53 2011
Running script Script...
ERROR 000714: Error in script Script.
Error in executing: cmd.exe /C C:\GIS_PR~1\PYTHON~1\BATCH_~1  "C:\GIS_Projects\2011\CE_BU_RS\RapidEye\Aug_1st_week\images\5_band" "C:\GIS_Projects\2011\CE_BU_RS\RapidEye\Aug_1st_week\images\5_band\clipped" "C:\GIS_Projects\2011\CE_BU_RS\RapidEye\shapefile_bnds\all_corn_IMPACT_2011.shp"

Failed to execute (Script).
Failed at Tue Aug 09 10:30:53 2011 (Elapsed Time: 0.00 seconds)



Screenshot of my script parameters attached.  I can copy and paste the paths into the script and it works fine



import arcpy
import os

inDIR = arcpy.GetParameterAsText(0)
outDIR = arcpy.GetParameterAsText(1)
clipFC = arcpy.GetParameterAsText(2)

arcpy.env.workspace = inDIR

rasters = arcpy.ListRasters("*", "TIF")

for raster in rasters:
 print raster
 outname = raster[:-4] + "_clip.tif"
 arcpy.Compression= "LZW"
 arcpy.Clip_management(raster, "", os.path.join(outDIR, outname), clipFC, "", "ClippingGeometry")


0 Kudos
WadeGivens
Emerging Contributor
Well I feel stupid.  My script wasn't saved with a .py extension.  That solved the issue

Wade
0 Kudos