0

I have created a bunch of grids for a city boundary area. I would like to know how to use the Arcpy module to look through a geodatabase and find the grid with the smallest number of rows (assuming that these grids are all the same size and the fewest is the most efficient grid) and then export only that feature out as a .SHP.

3
  • 1
    What have you tried so far? There are dozens of questions with answers that will get you 90% there.
    – Vince
    Commented Apr 30, 2015 at 19:32
  • I used both GetCount & SearchCursor methods to get the counts of the feature. I just can't wrap my head around writing the statement to select the feature with the smallest amount of rows to export as a .SHP.
    – pbach3
    Commented Apr 30, 2015 at 20:31
  • Please edit the question to add details (actual code, if possible) -- it's not fair to would be responders to have to filter the question through comments.
    – Vince
    Commented Apr 30, 2015 at 20:42

2 Answers 2

2

this might help:

import arcpy
arcpy.env.workspace=r"C:\temp\data.gdb"
fcs = arcpy.ListFeatureClasses()
rc_old = int(99999999999999)
export_fc = ""
for fc in fcs:
  rc = int(arcpy.GetCount_management(fc)[0])
  print fc
  print rc
  if rc < rc_old:
    export_fc = fc
    rc_old = rc
arcpy.FeatureClassToShapefile_conversion(export_fc,r"C:\temp")
2
  • My answer will do what you want.
    – laxman
    Commented Apr 30, 2015 at 20:45
  • Thank you @laxman. Sorry I did not respond to your answer. Works as intended.
    – pbach3
    Commented Jan 23, 2017 at 19:45
1

Try the below. Could do something with arcpy.ListFeatureClasses as well, but using arcpy.da.Walk will allow you to step into any feature datasets you might have in your geodatabase.

import arcpy
import os
from collections import OrderedDict


def find_smallest(gdb):
    d = {}
    walk = arcpy.da.Walk(gdb, datatype='FeatureClass')
    for p, dirnames, fcs in walk:
        for fc in fcs:
            fcpath = os.path.join(p, fc)
            d[fcpath] = int(arcpy.GetCount_management(fcpath)[0])
    return OrderedDict(sorted(d.items(), key=lambda t: t[1])).keys()[0]


if __name__ == '__main__':
    gdb = 'your_gdb'
    out_shapefile = 'your_output'

    arcpy.CopyFeatures_management(
            find_smallest(gdb),
            out_shapefile)

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.