I have a Layer of Streams called "My_Channels" with the following source
Data Type: File Geodatabase Feature Class
Database: C:\Users\user\Documents\ArcGIS\Default.gdb
Feature Class: My_Channels
Feature Type: Simple
Geometry Type: Line
Coordinates have Z values: No
Coordinates have measures: No
I Then Copy the layer so I don't destroy it and create "My_Test_Channels"
I'm attempting to use the arcpy.SelectLayerByAttribute_management({layer},{selection},{WHERE})
I'm executing the code populated with the following SQL WHERE CLAUSE
arcpy.SelectLayerByAttribute_management("MY_Test_Channels","NEW_SELECTION","UNIT_NO= 'E%'")
So far I've tried looking at the arcPy Docs for this tool.
I then want to copy the specific selections to a new layer and put that in my Default.gdb file. To be Clear I'm trying to extract Streams starting with the UNIT_NO "E". I have been able to execute the next code which is arcpy.CopyFeatures_management("My_Test_Channels","cropedLayer")
The reason I would like to do these types of operations is I'd like to study those specific stream types and currently our layer is setup with all of the streams for our area."E" as their UNIT_NO There are other stream groupings with other letter designations
Am I using the wrong tool to do this since the feature is a line and not necessarily a layer?
Or is it simply that my SQL expression is not formatted correctly?
I've also looked at using Select Analysis Tool but am unsure which is easiest, I plan to do this on other datasets what may be polygons, polylines, lines, points, etc. The current license that I have does not include the Split tool.
2 Answers 2
You can include the query in the creation of a feature layer that only includes the streams you want, then copy them to the new feature class.
arcpy.MakeFeatureLayer_management('My_Channels', 'My_Temp_Layer', "\"UNIT_NO\" LIKE 'E%'")
arcpy.CopyFeatures_management('My_Temp_Layer', 'My_Test_Channels')
A SQL selection using wild-cards needs to have a LIKE
operator, not a =
.
Your statement would be "\"UNIT_NO\" LIKE 'E%'"
arcpy.SelectLayerByAttribute_management("MY_Test_Channels", "NEW_SELECTION", "\"UNIT_NO\" LIKE 'E%'")
See SQL Wildcards
"\"UNIT_NO\" = 'E%'"
.