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")
-
1"To use the select layer by location tool, you much[=must?] pass in a .lyr file" seems wrong. I usually/always pass in a layer name (from a map or Make Feature Layer) and rarely/never a layer file name. In your code you seem to be assigning a name of "PointInFile.lyr" to your layer but it is a name only, and not a layer file. As a test call it "PointInFile.xxx" and I think you'll see what I mean.PolyGeo– PolyGeo ♦2015年12月08日 01:03:54 +00:00Commented Dec 8, 2015 at 1:03
-
Thanks for the swift response! I see what you mean, pretty much what i have done above is create a soft copy of 'PointInFile.lyr'. Because this is pointed to in memory, when i call the Delete Features tool, is it removed from memory? In theory, shouldn't the features that were not selected still be part of 'PointInFile.lyr'?John– John2015年12月08日 01:16:04 +00:00Commented Dec 8, 2015 at 1:16
-
I'm not sure without testing. Instead of presenting a function with try/except statements that may mask any underlying errors I think you should work on providing a code snippet that we can run to more directly test what you are asking about.PolyGeo– PolyGeo ♦2015年12月08日 01:19:55 +00:00Commented Dec 8, 2015 at 1:19
-
# 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")John– John2015年12月08日 01:43:00 +00:00Commented Dec 8, 2015 at 1:43
-
1As PolyGeo mentioned, you should perform layer selection on the actual layer (shapefile or gdb fc) instead of the lyr. Make feature layer should point to the actual layer. Then, perform your selection, copy, and deletes on the same layer.artwork21– artwork212015年12月08日 01:51:56 +00:00Commented Dec 8, 2015 at 1:51
1 Answer 1
If there is an active selection on a layer file, arcpy.DeleteFeatures_management will only delete those records selected.
-
Thats what i thought. so wouldnt arcpy.CopyFeatures_management('PointInFile.lyr', self.MakeXYeventLayer()[:-4] + "_NewData.shp") make a copy of only the features remaining after the delete like my script reads?John– John2015年12月08日 03:05:01 +00:00Commented Dec 8, 2015 at 3:05
-
1Yes, it should. Another option would be to make the selection, then switch the selection and just do copy features on that.Adam– Adam2015年12月08日 03:07:45 +00:00Commented Dec 8, 2015 at 3:07
-
@PolyGeo would it be easier if i added the entire main script so you could test?John– John2015年12月08日 03:07:58 +00:00Commented Dec 8, 2015 at 3:07
-
@Adem how do you switch the selection in arcpy?John– John2015年12月08日 03:08:27 +00:00Commented Dec 8, 2015 at 3:08
-
1By the way, to solve it, i removed the Delete Feature function and preformed another Selection on 'PointinFile' and included the "SWITCH_SELECTION" parameter. Thanks again.John– John2015年12月08日 03:51:12 +00:00Commented Dec 8, 2015 at 3:51
Explore related questions
See similar questions with these tags.