3

I want to clip multiple rasters to my study area, so I'm writing a script that's using the arcpy.Clip_management tool. However, the tool throws an 999999 error with no explanation when executing.

I suspect that I'm mucking up the syntax somehow since the tool runs fine when executed from the toolbox with the same inputs, but I can't figure out what I might be doing wrong.

Tool help for reference: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/clip.htm

This is the script as is:

import arcpy, numpy

arcpy.env.overwriteOutput = True

try:
# set workspace
arcpy.env.workspace = r"C:\Users\Johnnie\Desktop\exarb\gis\pilotstudie"

# set inputs
inLocation = "in.gdb/"
outLocation = "ut2.gdb/"
studyArea = inLocation + "studieomrade"
vectorInputs = ["by_get", "my_get", "vl_get"]
rasterInputs = ["hojd2m3006_6589746_685960"]


# process: clip all vector inputs to study area
#for i in vectorInputs:
    #outName = i + "_clip"
    #arcpy.Clip_analysis(inLocation + i, inLocation + studyArea, outLocation + outName)

# process: clip all raster inputs to study area
for i in rasterInputs:
    outName = outLocation + i + "_clip"
    inName = inLocation + i
    arcpy.Clip_management(inName, "", outName, studyArea, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")



except Exception as err:
print err.args[0]

This works with no problems for the commented out arcpy.Clip_analysis part.

This is what it looks like when I try to run the tool directly from the python console:

>>> arcpy.Clip_management("in.gdb/hojd2m3006_6589746_685960", "", "ut2.gdb/test2", "in.gdb/studieomrade", "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 13594, in Clip     raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (Clip).

I've tried with both empty quotes and "#" for default values as in the help sections code examples, but seems to make no difference.

Running the tool from the toolbox with the same inputs works just fine.

Running the tool from the toolbox works just fine.

2
  • 1
    according to Clip Desktop Help "Rectangle" (second tool input) is not an optional field
    – Midavalo
    Feb 15, 2016 at 3:33
  • OK ignore my previous comment - I have just tested with your script (changing just the workspace, in/out location, and rasterInputs) and it works no problem for me. (including with no input for "rectangle" as I mentioned above).
    – Midavalo
    Feb 15, 2016 at 6:59

1 Answer 1

2

@Midavalo is right - a rectangle must be set. if you don't want to give specific values, use # as follows:

arcpy.Clip_management("in.gdb/hojd2m3006_6589746_685960", "#", "ut2.gdb/test2", "in.gdb/studieomrade", "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

General remark:

  1. Try to use the r"Path" whereever you can. You did so for your workspace, add it to your in- and out-paths too. Sometimes you get strange errors if you don't use r.
  2. Also, try to use the built-in .format() function for putting together strings, as it's much easier to prevent syntax errors. Instead of studyArea = inLocation + "studieomrade" use studyArea = """{0}studieomrade""".format(inLocation). Everything inside the triple """ will be treated as strings, even spaces and such. Put all variables inside {} and define them in the brackets of the .format() function.
3
  • it appears that it will accept an empty "" for rectangle - I've just run @iamlukesky 's code as-is without error.
    – Midavalo
    Feb 15, 2016 at 7:02
  • ArcPy works in mysterious ways :) I've had the same problem as @iamlukesky so i think it's safer to use #
    – dru87
    Feb 15, 2016 at 7:11
  • Ok, so it turns out that the script runs fine when I run it as a standalone outside of ArcMap, but not when I run it from the python console within ArcMap. What gives?
    – Iamlukesky
    Feb 15, 2016 at 9:57

Your Answer

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

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