Select to view content in your preferred language

Best Practices for selecting a feature from a feature class that was just created.

161
4
Jump to solution
a week ago
ZacharyKasson
Occasional Contributor

I'm wondering what's the best practice is 4 selecting features of a feature class that was just created in the same tool.

This code works perfectly fine when I am running it in a notebook in pro. I get the expected amount of features in my output feature class. I presume this works because it adds the feature to my map and is able to select the desired features.

    arcpy.conversion.ExportFeatures(
        in_features=in_features,
        out_features= output_path,
        where_clause="Status = 'Confirmed'",
        use_field_alias_as_name="NOT_USE_ALIAS",
        field_mapping= field_mapping
    )
    arcpy.AddMessage('Exported routes')

    arcpy.management.SelectLayerByLocation(
        in_layer= f"{output_file_name}",
        overlap_type="INTERSECT",
        select_features=f"{feature1}",
        search_distance="5 Meters",
        selection_type="NEW_SELECTION",
        invert_spatial_relationship="NOT_INVERT"
    )
    arcpy.management.DeleteRows(output_file_name)

    arcpy.management.SelectLayerByLocation(
        in_layer= f"{output_file_name}",
        overlap_type="INTERSECT",
        select_features=f"{feature2}",
        search_distance="5 Meters",
        selection_type="NEW_SELECTION",
        invert_spatial_relationship="INVERT"
    )
    arcpy.management.DeleteRows(output_file_name)

 but when I turn this into a tool the output feature class has 0 features in it. I'm guessing this does not work because it cannot select features of a feature class that is not in my map.

I have tried to add the newly created feature class to my map using a couple different methods: Screenshot 2025-05-16 145837.jpg

These return a layer object but the layer is not added to my map.

 

I guess the overall question is: what is the best way to approach making a selection from a feature class that has just been created?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
MVP Regular Contributor

Use arcpy.MakeFeatureLayer_managment prior to SelectLayerByLocation

Something like this,

# Create selection feature layer (your exact format)
Sel_Point = arcpy.MakeFeatureLayer_management(
    selection_feature, 
    "point_layer", 
    "\"FID\"={}".format(str(fid_value)) #or OID
    
# Perform selection
arcpy.management.SelectLayerByLocation(
    in_layer=output_file_name,
    overlap_type="INTERSECT",
    select_features=Sel_Point,  # Using the variable you created
    search_distance="5 Meters",
    selection_type="NEW_SELECTION",
    invert_spatial_relationship="NOT_INVERT"
)

 

View solution in original post

4 Replies
TonyAlmeida
MVP Regular Contributor

Use arcpy.MakeFeatureLayer_managment prior to SelectLayerByLocation

Something like this,

# Create selection feature layer (your exact format)
Sel_Point = arcpy.MakeFeatureLayer_management(
    selection_feature, 
    "point_layer", 
    "\"FID\"={}".format(str(fid_value)) #or OID
    
# Perform selection
arcpy.management.SelectLayerByLocation(
    in_layer=output_file_name,
    overlap_type="INTERSECT",
    select_features=Sel_Point,  # Using the variable you created
    search_distance="5 Meters",
    selection_type="NEW_SELECTION",
    invert_spatial_relationship="NOT_INVERT"
)

 

ZacharyKasson
Occasional Contributor

I noticed that you assigned the result of arcpy.MakeFeatureLayer_management to a variable named Sel_Point.

Is it considered best practice to store the output of a tool in a variable like this? Does assigning it to a variable improve the tool’s performance or is it mainly for readability and convenience?

0 Kudos
TonyAlmeida
MVP Regular Contributor

Yes, it is best practice. It would be more for readability than anything, in my opinion.  it does nothing for performance that I am aware of.

0 Kudos
GISDepartmentMFM
Occasional Contributor

it also lets you add it to the map

map.addLayer(Sel_Point.getOutput(0))
0 Kudos