[GIS] Deleting selected features with ArcPy

arcpycopydeleteselectselect-by-location

I am having trouble deleting the features I have selected using 'arcpy.SelectLayerByLocation_management' tool. To use the select layer by location tool, you much pass in a .lyr file. My script SHOULD create a .lyr file of the input data, select by location on the .lyr file, make a copy of the selected features and save to a .shp file, and then delete the selected features in my input data. Instead, it creates a .lyr file, selects features in the layer file and makes a copy of the layer file (correct up until this point). Then it deletes the .lyr file and im stuck at with an unchanged input data set. Should i make a copy of the .lyr file after i have used the arcpy.DeleteFeatures_management tool?

I guess the question in fewer words is:

When i call the arcpy.DeleteFeatures_management tool on a .lyr file that has selected features, does it delete the entire .lyr file or just the selected features?

Here is my code:

def dBaseComparison(self):
        '''This function compairs the input data to the database and selects
            the features that already exist'''
        try:
            # Create a feature layer to select the points that already exist in      the database. 
            arcpy.MakeFeatureLayer_management(self.MakeXYeventLayer(), 'PointInFile.lyr')
            arcpy.SelectLayerByLocation_management('PointInFile.lyr', "ARE_IDENTICAL_TO", self.dBase)        
            # Make a copy of the existing features and delete them from the new file.
            arcpy.CopyFeatures_management('PointInFile.lyr', self.MakeXYeventLayer()[:-4] + "_selected.shp")
            arcpy.DeleteFeatures_management('PointInFile.lyr')
            arcpy.CopyFeatures_management('PointInFile.lyr', self.MakeXYeventLayer()[:-4] + "_NewData.shp")
            return self.MakeXYeventLayer()

        except arcpy.ExecuteError:
            return arcpy.GetMessages( )

Here is a stand alone script that works great! what is the difference?:

import arcpy
arcpy.env.workspace = 'C:\Temp\Project_Output'
arcpy.env.overwriteOutput = 1

dataBase = "C:\Temp\Project_Output1\dBase.shp"
infile = "C:\Temp\Project_Output1\NC_CSV_TEST_Point.shp"

# Create a feature layer to select the points that already exist in the database.
arcpy.MakeFeatureLayer_management(infile, 'PointInFile.lyr')
arcpy.SelectLayerByLocation_management('PointInFile.lyr', "ARE_IDENTICAL_TO", dataBase)        
# Make a copy of the existing features and delete them from the new file.
arcpy.CopyFeatures_management('PointInFile.lyr', infile[:-4] + "_selected.shp")
arcpy.DeleteFeatures_management('PointInFile.lyr')
arcpy.CopyFeatures_management('PointInFile.lyr', infile[:-4] + "_NewData.shp")

Best Answer

If there is an active selection on a layer file, arcpy.DeleteFeatures_management will only delete those records selected.

See ArcGIS Help