3

I'm having some issues with a Python script for ArcGIS 10. The idea is to delete points from a single shapefile according to the values of two columns ("Tracks" and "Tracks_1"). Therefore, I want to:

  1. Select features who share a certain ID
  2. Unselect some of the features depending on their values
  3. Delete the remaining selection from the shapefile
  4. Proceed with the next ID

.

import arcpy
env.overwriteOutput = True
centroids_thinned="O:\\centroids_thinned.shp"
arcpy.MakeFeatureLayer_management(centroids_thinned, "centroids_thinned_layer")
rows=arcpy.SearchCursor(centroids_thinned)
for row in rows:
    ID_centroids = row.getValue("FID_centro")
    ID_buffer = row.getValue("FID_cent_1")
    Tracks_centroids = row.getValue("Tracks")
    Tracks_buffer = row.getValue("Tracks_1")
    if ID_buffer == ID_centroids:
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "NEW_SELECTION", "FID_centro = " + str(ID_buffer))
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "REMOVE_FROM_SELECTION","FID_centro = FID_cent_1")
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "REMOVE_FROM_SELECTION","Tracks_1 > " +  str(Tracks_centroids))
        count = int(arcpy.GetCount_management("centroids_thinned_layer").getOutput(0))
        if count > 0:
                arcpy.DeleteFeatures_management("centroids_thinned_layer")
                print str(count) + " Attributes Deleted."

The selection process works fine but for some reason those selected features are not deleted from the attribute table. How can i solve this problem?

4
  • 1
    Are the 5 lines after if ID_buffer == ID_centroids: really at the same indentation level as that statement?
    – nmpeterson
    Apr 17, 2013 at 16:20
  • 1
    No. That must have happened when i copied the code. I'm gonna correct that. Thanks.
    – C.Riedel
    Apr 17, 2013 at 16:48
  • 1
    When you say the selection process works fine, how exactly do you know that? Is it simply not crashing, or are you actually inspecting the values of count?
    – nmpeterson
    Apr 17, 2013 at 19:34
  • Yes, I was inspecting the count values.
    – C.Riedel
    Apr 18, 2013 at 16:07

2 Answers 2

4

I don't have the time right now to revise your code, but I think your problem is that you're trying to delete features from the very same shapefile you're in the middle of looping through (albeit as a feature layer).

I would suggest that you only call arcpy.DeleteFeatures_management() once, after the search cursor has gone through the entire thing; you can keep track of all of the features to delete as the cursor progresses using a python list, and then construct a query to re-select and delete them all in one go at the end.

1
  • Thanks. That did the trick. DeleteFeatures doesn't work when it's within a SearchCursor.
    – C.Riedel
    Apr 18, 2013 at 16:09
2

Like @nmpeterson said, it's likely because you're doing this: open SearchCursor on centroids_thinned.shp, you run DeleteFeatures while your searchCursor is still open on centroids_thinned.shp.

I don't know exactly what your data looks like, but a common approach is to open a arcpy.da.UpdateCursor and get the records sorted based on some fields (for you it's Tracks & Tracks_1).

sql_clause  = ('','ORDER BY ID_centroids, ID_buffer ')

Then basically keeping track of when the values change from the previous record to determine if it meets your "delete" criteria or not.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.