5

I am trying to select n rows then skip n rows and then select the next n rows until the completion of all rows in a feature class in a file-geodatabase. Below is an example where n =3:

  1. Select
  2. Select
  3. Select
  4. Not selected
  5. Not selected
  6. Not selected
  7. Select
  8. Select
  9. Select

I basically need to make a selection like this and populate the rows that are selected.

  • Cursor through them, keep the OIDs then select by OID, so long as this isn't a shapefile that's being edited/saved the OID values are static and unique. What are you trying to do with the selected features? Is there any possibility of doing this in ArcObjects? IFeatureSelection would be easier to work with than multiple select by attributes (with add to selection). – Michael Stimson May 12 '16 at 5:30
  • No chance with arcobjects I don't even have visual studio installed on my computer, and have no experience with arcobjects. However python is definitely an option. – Brian Brokling May 12 '16 at 5:57
  • Auto-increment should work in Python. This answer might help get you started. (gis.stackexchange.com/questions/16752/…). Using the same variables as in the link, set p interval to n then select between rec and rec + n. – AnserGIS May 12 '16 at 6:39
  • 4
    Select by attribute using Mod (Fid,6) < 3 – FelixIP May 12 '16 at 7:21
  • @FelixIP that's a good suggestion but that works for shapefiles where the FID is compressed every save.. on fGDB, pGDB and SDE there is a possibility for 'gaps' in the OID. Though one way to do this is to copy the OID to a static field, export to shape (or dbf), join and select on the joined field as you say. Another would be to add a field, field calculate an ascending contiguous series and select from that using mod. I've done both and they both work; I've used the method you suggest in shapefiles to split into groups (eg 4 groups Mod(FID,4) = 0,Mod(FID,4) = 1...) and can sayit works well. – Michael Stimson May 12 '16 at 21:39
3

Here is a small python script which will serve your purpose.

# required import
import arcpy

# inputs
# feature class path
fc = r'C:\GISData\File Geodatabase.gdb\feature_class'

# interval
interval = 3

# --------------------- script ------------------------- #
selected_oid = []
ready_to_add = False
interval = abs(interval)

fc = arcpy.MakeFeatureLayer_management(fc, r'in_memory\fc_name')
with arcpy.da.SearchCursor(fc, 'OID@') as cur:
    for i, row in enumerate(cur):
        if i % interval == 0:
            ready_to_add = not ready_to_add
        if ready_to_add:
            selected_oid.append(unicode(row[0]))

# where clause to select by attribute value
where = 'OBJECTID in ({})'.format(', '.join(selected_oid))

# selected features as required
selected_fc = arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", where)

Hope, this will help :)

  • That's how I would do it in python, +1 from me – Michael Stimson May 12 '16 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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