Select to view content in your preferred language

Use a cursor to select each row for input into another function.

936
2
05-18-2012 07:29 AM
GerryGabrisch
Frequent Contributor
I have a python tool that uses a select by attribute to get each record of a feature class and then feed that selection into a zonal statistics function.
I thought there was a way to use a search cursor and something like row.Shape to select each row, then feed that row into another tool, thereby speeding up the code.
I can't find any example of using a cursor to select each row then use that selection as in input parameter into another function.
Help please.
Tags (2)
0 Kudos
2 Replies
ChrisSnyder
Honored Contributor
I think this is what you are looking for: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Using_geometry_objects_with_geoprocess...

I've done some limited stuff with this, and it seems to work well... The idea is the geometry stuff is evaluated in RAM anyway... why not just keep it there as an object you can use instead of always reading/writting a featureclass from disk?

Here is a practical example... an elaboration of some ESRI-authored code (I wrote the search tree part to get at a solution quicker)... Builds approximate maximum inscribed circles inside a polygon featureclass. Note that there is a bug with the ESRI buffer tool in v10.0 that prevents this from "always" being the actual maximum inscribed circle. Also, this code needs some refining on my part, but it should work well as-is.

try:
    import arcpy, os, sys, traceback

    arcpy.env.overwriteOutput = True
  
    #Get the input feature class or layer
##    inFeatures = arcpy.GetParameterAsText(0)
##    outFeatures = arcpy.GetParameterAsText(1)

    inFeatures = r"C:\csny490\habitat_frag_analysis_20110919\circles.gdb\mature_interior_forest"
    outFeatures = r"C:\csny490\habitat_frag_analysis_20110919\circles.gdb\out_polys"

    #Some housekeeping
    inDesc = arcpy.Describe(inFeatures)
    oidName = str(inDesc.OIDFieldName)
    if inDesc.dataType == "FeatureClass":
        inFeatures = arcpy.MakeFeatureLayer_management(inFeatures)
        
    sR = inDesc.spatialReference
    xyTol = sR.XYTolerance
    arcpy.env.overwriteOutput = True
                                  
    #Create the stub output feature class
    arcpy.CopyFeatures_management(inFeatures,outFeatures)
    arcpy.AddField_management(outFeatures, "BUFFDIST", "DOUBLE")
    OIDFieldName = arcpy.Describe(outFeatures).OIDFieldName
    arcpy.MinimumBoundingGeometry_management(outFeatures, "in_memory\\br", "RECTANGLE_BY_WIDTH", "NONE", "", "MBG_FIELDS")
    #Create dictionary of ORIG_FID,MBR_WIDTH
    fidWidthDict = dict([(r.ORIG_FID, r.MBG_WIDTH) for r in arcpy.SearchCursor("in_memory\\br")]) #
    arcpy.Delete_management("in_memory\\br")
    #Calculate the inscribed circles
    rows = arcpy.UpdateCursor(outFeatures)
    for row in rows:
        print "Processing polygon " + str(row.getValue(OIDFieldName))
        inShape = row.shape
        aGeom = arcpy.Geometry()
        maxBuffDist = fidWidthDict[row.getValue(OIDFieldName)] / 2
        minBuffDist = xyTol
        geomList = []
        while (maxBuffDist - minBuffDist) >= xyTol:
            midBuffDist = (minBuffDist + maxBuffDist) / 2.0
            print "Checking buffer distance = " + str(midBuffDist * -1)
            geomList = arcpy.Buffer_analysis(inShape, aGeom, midBuffDist * -1, "FULL","", "NONE", "")
            if len(geomList) > 0:
                minBuffDist = midBuffDist
            else:
                maxBuffDist = midBuffDist
        geomList = arcpy.Buffer_analysis(inShape, aGeom, (midBuffDist - (2 * xyTol)) * -1, "FULL","", "NONE", "")
        inPoint = arcpy.FeatureToPoint_management(geomList[0], aGeom, "INSIDE")[0]        
        geomList = arcpy.Buffer_analysis(inPoint, aGeom, (midBuffDist - xyTol), "FULL" ,"", "NONE", "")
        row.BUFFDIST = midBuffDist * -1
        row.shape = geomList[0]
        rows.updateRow(row)                
    del row, rows
    print "Done!!!"

except:
    print "\n*** LAST GEOPROCESSOR MESSAGE (may not be source of the error)***"
    print arcpy.GetMessages()
    print "\n*** PYTHON ERRORS *** "
    print "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]
    print "Python Error Info: " +  str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
0 Kudos
BruceNielsen
Frequent Contributor
Gerry,

In v9.2(where I'm currently stuck), the Describe object has an OIDFieldName property. You could use that for building the where clause of a SelectLayerByAttribute.
0 Kudos