0

I have a nationwide shapefile I want clipped into regions. There's not so many regions that I need to script iterating through each region, but there are enough where I want to batch process this. I explored using the Python interpreter built into ArcMap 10.5.1 to do so.

First I tested the process with one region and the following code works:

arcpy.Clip_analysis(in_features="WBDHU12", clip_features="07", out_feature_class="F:/GIS/07_HUC12s.shp", cluster_tolerance="")

It returns:

<Result 'F:\\GIS\\09_HUC12s.shp'>

Then, following instructions on entering multiple commands using the ArcMap Python window, I tried to do two clips for two different regions with one script execution:

arcpy.Clip_analysis(in_features="WBDHU12", clip_features="08", out_feature_class="F:/GIS/08_HUC12s.shp", cluster_tolerance="")
... arcpy.Clip_analysis(in_features="WBDHU12", clip_features="09", out_feature_class="F:/GIS/09_HUC12s.shp", cluster_tolerance="")
... 

I'd expect that just as I could clip HUC12s to region 7 using that code successfully, I could clip HUC12s to region 8, and then again to region 9 one after the other. If this worked, I'd just setup the script to run all regions of interest and then let it geoprocess them all at once ('set it and forget it', I hoped). However, I hit this error:

Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\analysis.py", line 62, in Clip     raise e ExecuteError: ERROR 000725: Output Feature Class: Dataset F:\GIS\08_HUC12s.shp already exists. 

I've tried a few different regions like this and anytime I run one region at a time, it takes a couple of minutes then completes the clip, but if I run two regions at a time, it instantly gives me this error and oddly enough it also instantly successfully clips the first region (e.g. in the example above, I'll have a new file in my catalog and map for HUC12s clipped to region 8). What I don't understand is:

  • Why is the clipping so fast when trying to do 2 regions in one script execution, whereas when doing 1 region per execution it takes longer?

  • Why am I getting an error about the out_feature_class already existing when I have set different out_feature_classes for each region / instance of clipping?

1

•Why is the clipping so fast when trying to do 2 regions in one script execution, whereas when doing 1 region per execution it takes longer? Have you loaded the regions that generated from running 2 regions, is the one that gets clipped valid? The process might be faster because it's not fully clipping.

•Why am I getting an error about the out_feature_class already existing when I have set different out_feature_classes for each region / instance of clipping?

I would create a variable out_feature_class_8=F:/GIS/08_HUC12s.shp, and out_feature_class_9=F:/GIS/09_HUC12s.shp, then change the code to:

arcpy.Clip_analysis(in_features="WBDHU12", clip_features="08", out_feature_class=out_feature_class_8, cluster_tolerance="") ... arcpy.Clip_analysis(in_features="WBDHU12", clip_features="09", out_feature_class=out_feature_class_9, cluster_tolerance="")

or clear the value of out_feature_class. It seems like while processing the python engine is caching the value and not writing to the new location that you specified.

|improve this answer|||||
  • I will try to use variables & loops in the script, and clear out_feature_class at the end of each loop, to see if that helps. And yes, I loaded the region generated from running 2 region (only one actually processed) and surprisingly it looks correct! Digging into it deeper I might find problems with it though, we'll see. – cr0 Jul 18 '18 at 16:45
  • As this is the most complete answer, not just recommending to use a loop, I'll accept this if no one else posts something better. Will let you know how it goes also. – cr0 Jul 18 '18 at 16:45
1

I don't know why it is not working as the documentation suggests, but you could achieve the same results by creating a loop:

for i in ['07','08','09']: arcpy.Clip_analysis(in_features="WBDHU12", clip_features=i, out_feature_class="F:/GIS/{}_HUC12s.shp".format(i), cluster_tolerance="")

|improve this answer|||||
1

I'm not sure of an answer to either of your actual posted questions as to why it is behaving the way it is for you. However, that said, I would try setting up all of your parameters in a list, and then just telling the list to run a clip for each set of parameters in your list. Ex:

>>>allClipFeatures = ["08","09"]
>>>for clipFeature in allClipFeatures:
...    arcpy.Clip_analysis(in_features="WBDHU12", clip_features=clipFeature, out_feature_class="F:/GIS/{}_HUC12s.shp".format(clipFeature), cluster_tolerance="")

That should loop through each value in the allClipFeatures list and do a clip changing the clip_features and the output file name each time.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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