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)
asked Feb 28, 2019 at 5:25
5
  • 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) Commented Feb 28, 2019 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! Commented Feb 28, 2019 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. Commented Feb 28, 2019 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. Commented Feb 28, 2019 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. Commented Feb 28, 2019 at 6:14

1 Answer 1

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.

answered Feb 28, 2019 at 6:21
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.