Stop repeating other processes while using iterator

673
2
07-23-2020 02:21 PM
MichaelJust
New Contributor

In model builder I am attempting to Clip a Feature which has Selected Attributes by each polygon in another feature. This model will get the desired output. But, it will "Select Layer By Attribute" each time. Making the process slower.

model builder diagram I've tried variations of putting either the 'top' or 'bottom' commands before clip into submodels, but this has resulted in the same behavior.  I've tried different iterators (e.g., use split by attributes on the clipping feature and then a feature class iterator) to no avail. If export to python code I can see that 'arcpy.SelectLayerByAttribute_management" is in the same for loop as 'clip'. How might I adjust this model to no longer "Select Layer By Attribute" for each clipping polygon?

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor

Not sure, don't really use Model Builder for these kind of reasons.  I'd probably suggest it's a result of the selection output being an input alongside the iterator but it doesn't make a great deal of sense.

Perhaps copy the selected feature in the model.

select layer by attribute -->  selected feature --> copy feature

0 Kudos
MichaelJust
New Contributor

Thanks.

I tried 'select layer by attribute -->  selected feature --> copy feature' as well as sticking that and the rest of the <select layer by attribute> into a submodel. But, unfortunately, <select layer by attribute> is run for each polygon still. 

Scripting is likely the way to go. But, I am now eager to see if this could work with model builder.

Exporting the model to Python script isn't functional, but does reveal the <select layer by attribute> within the 'for loop' as I mentioned above. Here is that script for completeness:

import arcpy
def #  NOT  IMPLEMENTED# Function Body not implemented

def Model13():  

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    Input_Feature = "Water"
    Clipping_Feature = "Locations"

    for Selected_Polygon, Name in #  NOT  IMPLEMENTED(Clipping_Feature, [["String_ID", ""]], False):

        # Process: Select Layer By Attribute (Select Layer By Attribute) 
        Selected_Feature, Count = arcpy.SelectLayerByAttribute_management(in_layer_or_view=Input_Feature, selection_type="NEW_SELECTION"where_clause="FCODE NOT IN (3, 6, 15, 85)"invert_where_clause="")

        # Process: Copy Features (Copy Features) 
        Output_Feature_Class = "C:\\Foo\\Bar\\Water_CopyFeatures1"
        arcpy.CopyFeatures_management(in_features=Selected_Feature, out_feature_class=Output_Feature_Class, config_keyword=""spatial_grid_1=Nonespatial_grid_2=Nonespatial_grid_3=None)

        # Process: Clip (Clip) 
        Name = "86"
        Clip_Name_shp = fr"C:\Foo\Bar\Water_Clip_{Name}.shp"
        arcpy.Clip_analysis(in_features=Output_Feature_Class, clip_features=Selected_Polygon, out_feature_class=Clip_Name_shp, cluster_tolerance="")

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Foo\Bar\MB_Test.gdb"workspace=r"C:\Foo\Bar\MB_Test.gdb"😞
        Model13()
0 Kudos