0

I feel like I've tried so many iterations of this, can anyone assist with how to use ArcPy to selected layer by attribute then delete selected layer? I think my SQL query is incorrect?

 import arcpy
 import pandas as pd
 import numpy as np

 pid_field = ["pid"]
 target = arcpy.da.TableToNumPyArray(Master_Subjects, pid_field)

    Master_Subjects = r"C:\Users\dsser\Documents\ArcGIS\Projects\MyProject1\Master_Subjects.shp"
    target_pid = target[0]["pid"]
    # just selecting a random pid here from the Master_Subjects shapefile to see if I can get the select by attribute to work.This is a shapefile feature class with point geometry.
    # Also, target_pid is an integer
    target_pid = target[0]["pid"]
    query = "'" "subject_pid" = target_pid "'"
    arcpy.SelectLayerByAttribute_management(Master_Subjects, "NEW_SELECTION", query)
    arcpy.DeleteFeatures_management(Master_Subjects)
  • 1
    I dont know what target_pid is, but if it is an integer try: AddFieldDelimiters: query = """{0}={1}""".format(arcpy.AddFieldDelimiters(datasource=somelayer, field="subject_pid"),target_pid) – BERA Feb 28 at 5:42
  • To delete the layer object use arcpy.Delete_management, what you have in your code is to delete the selected features from the feature class permanently, most of the time it's not neccesary to delete the layer object though as it will be destroyed at the end of the script anyway. Your query would work just fine as "subject_pid = target_pid", field delimiters aren't necessary for selection with a query. Master_Subjects isn't a layer, it's a feature class, use MakeFeatureLayer to create a layer first! – Michael Stimson Feb 28 at 6:06
  • @BERA target_pid is an integer and so is the subject_pid. I tried using this for the query, it didn't work and returns invalid expression error. I added the additional code to try and help clarify. The feature class is a shapefile with point geometry and the attributes are integers. – David Seroy Feb 28 at 6:08
  • @MichaelStimson Unfortunately the selection does not work at all. Regardless, in both instances the query = "subject_pid = target_pid" or the delimiters both result in errors saying the expressions are invalid. – David Seroy Feb 28 at 6:12
  • 1
    Whats the print type(target_pid)? SelectLayerByAttribute wants a layer as input, not shapefile. Use MakeFeatureLayer and then Select on this. Your Query has syntax errors, try a = "'" "subject_pid" = target_pid "'" in console and see. Use format like i showed you. – BERA Feb 28 at 6:14
1

I see, your target_pid is a number and not a field, in which case the comment by BERA is correct.

Try it like this:

pid_field = "pid" # don't need brackets here
target = arcpy.da.TableToNumPyArray(Master_Subjects, pid_field)

Master_Subjects = r"C:\Users\dsser\Documents\ArcGIS\Projects\MyProject1\Master_Subjects.shp"
Master_Layer    = arcpy.MakeFeatureLayer_management(Master_Subjects) # Must be a layer for Select Layer by Attribute
# just selecting a random pid here from the Master_Subjects shapefile to see if I can get the select by attribute to work.This is a shapefile feature class with point geometry.
# Also, target_pid is an integer
target_pid = target[0]["pid"]
query = "{} = {}".format(pid_field,target_pid)
print "{} features before selection".format(arcpy.GetCount_management(Master_Layer).getOutput(0))
arcpy.SelectLayerByAttribute_management(Master_Layer, "NEW_SELECTION", query)
print "{} features after selection".format(arcpy.GetCount_management(Master_Layer).getOutput(0))
arcpy.Delete_management(Master_Layer) # layer object gone now

To select from a layer the input object must be a layer and not a feature class, use arcpy.MakeFeatureLayer_management to derive a layer from the feature class; I've added GetCounts to show the count of features before and after selection.

If you truly intend to delete the features permanently from the feature class selected by your query then the tool you are using (delete features) is correct.

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.