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.

5
  • 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). May 12, 2016 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. May 12, 2016 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, 2016 at 6:39
  • 4
    Select by attribute using Mod (Fid,6) < 3
    – FelixIP
    May 12, 2016 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. May 12, 2016 at 21:39

1 Answer 1

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 :)

3
  • That's how I would do it in python, +1 from me May 12, 2016 at 21:34
  • I know this is an older post, but any way to do this for a stand-alone table?
    – GravitaZ
    Mar 14, 2021 at 11:18
  • 1
    @GravitaZ: In case of table, you have to give table path and instead of MakeFeatureLayer_management use MakeTableView. Every thing would be same
    – Surya
    May 6, 2021 at 4:40

Your Answer

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

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