Select to view content in your preferred language

deleting selected features (footprint shapes)

820
6
03-07-2012 11:01 AM
MatthiasBuehler1
Deactivated User
hi !


[ usually I am the person giving answers .. on CityEngine .. 🙂 ]



though I am new to Python coding in ArcGIS, so I'd be happy if anyone could help me with the following issue :

I have a layer with a few hundred shapes, where I am creating manual selections. based on those selected features, I'd like to delete n% of the selected shapes. e.g. 10% each time I run the script to 'thin out' the data in certain regions.



any input welcome .. !
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Honored Contributor
Would you want these shapes randomly selected or would every 10th shape in the selection be fine?
0 Kudos
MatthiasBuehler1
Deactivated User
hey !

better randomly, but every nth would already be great.

I'm having the issue with actually getting the list from the selection..
0 Kudos
MathewCoyle
Honored Contributor
This should get you started. Will delete every n row. The cursor will honour selections so just make sure you reference the feature layer you have the selection on.
import arcpy
flayer = #your feature layer with the selection, hardcode name or passed parameter
rows = arcpy.UpdateCursor(flayer)
count = 0
n = 10
for row in rows:
    count += 1
    if count == n:
        rows.deleteRow(row)
        count = 0


Deleting random rows while ensuring 10% is deleted is also possible, but would involve a bit more coding.
0 Kudos
MatthiasBuehler1
Deactivated User
hey !

thanks a lot for that code, it works so far !


to understand what actually happens here is that all features are actually stored as they were in a geometrical grid and then you delete them based on that grid ?


is it also possible to just use a for loop and (possibly) delete each object based on a probability ? or are list based structures not possible ?


thx again so far !
0 Kudos
ChrisSnyder
Honored Contributor
This code should give you a random 10% sample of the OBJECTIDs in a feature layer that have a selected set.

#Warning: Untested
import random, arcpy
samplePct = 0.1
arcpy.MakeFeatureLayer_managment(roadsFC, "roads", "")
arcpy.SelectLayerByAttribute_managment("roads", "NEW_SELECTION", "ROAD_TYPE = 'Highway')
fidSetList = [int(fid) for fid in arcpy.Describe("roads").fidSet.split(";")]
if len(fidSetList) * samplePct >= 1: #error check rto make sure you have enough selected features to get an appropriate sample
   randomSampleList = radom.sample(fidSetList, int(len(fidSetList) *samplePct))
else:
   print "Not enough selected features to constitute a " + str(samplePct * 100) +  "% sample!"


Then you could delete them like this:

arcpy.MakeFeatureLayer(roadsFC, "delete_me", "OBJECTID in (" + str(randomSampleList)[1:-1] + ")")
arcpy.DeleteFeatures_Managment("delete_me")


or use an update cursor... same thing.
0 Kudos
MatthiasBuehler1
Deactivated User
hi Chris !


thanks for that input ! appreciated.

I'll have a look at this.


matt
0 Kudos