1

I am trying to pull out a sample out of a geodatabase file. I have 100800 sample and I need to select 28 records, basically every 3600, I am going to start with FID=0 and go from there. Unfortunately I haven't been able to make it work. this is what I have done so far. anyone can give me a hint, it's gonna be a huge help.

import arcpy, sys, os

Input=sys.argv[1]
field=sys.argv[2]
counts=sys.argv[3]
output=sys.argv[4]

def deleteMe(fc):
    if arcpy.Exists(fc):
        arcpy.Delete_management(fc)
    return fc


cursor =arcpy.da.SearchCursor(Input,field)
for row in cursor:
    a=row[0]
    while row:
        x=a+3600

    arcpy.MakeFeatureLayer_management(Input, deleteMe("lyr"))
    arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION",a)

#arcpy.SelectLayerByAttribute_management ("lyr", "ADD_TO_SELECTION",x)



arcpy.CopyFeatures_management ("lyr", deleteMe(output))
3
  • 1
    Skip through with an incrementing variable, when you hit your target value keep that OID (OIDlist.append(str(row[0])) and reset the counter to 0. Then use the query string "OBJECTID IN (" + ",".join(OIDlist) + ")".. it's that or (if your OIDs are unique and ascending) OBECTID MOD 3600 = 0 Commented Apr 24, 2015 at 2:35
  • yes, just what I was thinking. OP mentioned a geodatabase so I wrote my answer for non-consecutive OBJECTIDs. I didn't think of using MOD in the query though, if it was a shapefile that would be great.
    – mr.adam
    Commented Apr 24, 2015 at 2:37
  • I know @mr.adam, the mod(FID,3600) would do it for sure in a shapefile as FIDs are compressed on save. Note the change of syntax for the mod operator in shapefiles. If you think that's fun try to select out a random 10% of features... now that was fun! Commented Apr 24, 2015 at 2:39

1 Answer 1

1

How about just making a list of the OBJECTID's you want and making it into a query?

## start with list of all objectids
all_oids = [row[0] for row in arcpy.da.SearchCursor(Input,"OBJECTID")]

## make list of every 3600th objectid
oids = []
for index, oid in enumerate(range(len(all_oids))):
    if index % 3600 == 0:
        oids.append(oid)

## make sql query
qry = '"OBJECTID" IN ({0})'.format(".join(oids))

## deal with the datasets
arcpy.MakeFeatureLayer_management(Input, deleteMe("lyr"))
arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION", qry)
arcpy.CopyFeatures_management ("lyr", deleteMe(output))

Enumerate is a super useful function. It iterates a list and also returns the index of each item. You're using it here to run through the list of OBJECTIDs and if the index is a factor of 3600 the oid appended to a list. Then use that list to create a query that you can apply to the feature layer.

2
  • 1
    Where does i come into it? That's basically what I said but you need to set i=0 to start and then i+=1 in the loop. Commented Apr 24, 2015 at 2:37
  • oops, was from an earlier version of the answer, will update...
    – mr.adam
    Commented Apr 24, 2015 at 2:38

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.