Select to view content in your preferred language

Selecting By Attribute and preforming function

1957
5
03-22-2021 09:12 AM
KeoniJames-Ashkewe
New Contributor

Hello,

I'm pretty new to ArcGIS Python scripting, I hope this question isn't dumb. How would clip/intersect after selecting that layer in ArcPy based on a specific attribute? . When I write out the statement to select by a specific attribute, then write out a clip or intersect statement right after, it seems to completely ignore that I just wrote a selection by attribute statement above it. The function just completes on the entire layer rather than the selection.

I've already looked at the help section for both to see if I was messing up in the syntax, to no avail. Any suggestions? 

0 Kudos
5 Replies
DavidPike
MVP Frequent Contributor

Sharing your code would be a good first step

KeoniJames-Ashkewe
New Contributor

Here it is - my question is how to carry on the selected layer? Or if there's a was I was write in the clip statement to select a specific attribute in the table.

import arcpy

print("ArcPy Imported ")

#set variables

# chose an output database
arcpy.env.workspace = r'C:\Users\JAMES KEONI\Documents\ArcGIS\Projects\Flow Map\Flow Map.gdb'
print("Workspace Created")

# arcpy.env.workspace = "C:/data" arcpy.Buffer_analysis("roads", "C:/output/majorrdsBuffered", "100 Feet", "FULL",
# "ROUND", "LIST", "Distance")
path = r'C:\Users\JAMES KEONI\Documents\ArcGIS\Projects\Flow Map\Buffer'
print("Path Created")

# Create Buffer Layer
arcpy.Buffer_analysis(Centroid_Map_XYTableToPoint, buffer, "200 kilometers")
print("Buffer Layer Created")


# arcpy.management.SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
arcpy.SelectLayerByAttribute_management("Lakehead_Forest_Processing", "NEW_SELECTION", "FMU_NAME = 'Lakehead Forest' ")

print("Layer Selected")

# arcpy.analysis.Clip(in_features, clip_features, out_feature_class, {cluster_tolerance})
arcpy.analysis.Clip("Lakehead_Forest_Processing", "Buffer", "Clipped_Layer")

print("clip successful")

 

0 Kudos
MatthewDriscoll
MVP Alum

Use variables.   It's also a good practice to use Feature layers.

 

 

# Create Buffer Layer
buff = arcpy.Buffer_analysis(Centroid_Map_XYTableToPoint, buffer, "200 kilometers")
buffFeatureLayer = arcpy.MakeFeatureLayer_management(buff, "buff_layer")
print("Buffer Layer Created")


# arcpy.management.SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
selectLayer = arcpy.SelectLayerByAttribute_management("Lakehead_Forest_Processing", "NEW_SELECTION", "FMU_NAME = 'Lakehead Forest' ")

print("Layer Selected")

# arcpy.analysis.Clip(in_features, clip_features, out_feature_class, {cluster_tolerance})
arcpy.analysis.Clip(selectLayer, buffFeatureLayer, "Clipped_Layer")

print("clip successful")

 

 

Luke_Pinner
MVP Regular Contributor

You are passing a feature class to SelectLayerByAttribute and not using the returned feature layer (which has the selection applied). And then passing the original feature class to the next tool.

Feature classes are the actual data (on disk, in a geodatabase, in the cloud, etc.) and in ArcGIS can't have selections (or symbology, joins, etc.). Feature layers are a representation of a feature class that are a link to the feature class and can also have selections (and symbology, joins, etc.).

JavierBarboza
New Contributor

I am new to using arcpy. I have a very similar question, how would be a code to intersect by attributes?
thank you

0 Kudos