Select to view content in your preferred language

Searching for relate cursor example in Python

995
2
07-24-2011 06:59 AM
HollyCopeland
Deactivated User
Hi Folks,
I've searched the archives and ESRI help for example code that does a relate using cursors and can't seem to find one. Here's my problem. Perhaps someone can share code or give an example of how you would code this? It seems like this should be simple enough...

I have a ArcGIS data table with an id code "parcelid". I thought that I would use a SearchCursor to walk through each row and then relate through "parcelid" to a featureclass to grab the records that match that parcelid. I then need to do a series of calculations on fields in the table and featureclass and put the result in a new table.

Any help would be greatly appreciated! Thanks, Holly
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Frequent Contributor
Holly, this should get you started... The #'s are Python comments...

What you do at the end (writing to new table) really depends on what you calculate and where it ends up. In this little example I created a table, but you could also copy rows or something like that.

Also, I haven't tested it out (there could be typo's, etc.).

in_tablePath = arcpy.GetParameterAsText(0) # full path to input table (includes name)
out_tablePath = arcpy.GetParameterAsText(1) # path to output table
out_tableName = arcpy.GetParameterAsText(2) # name of output table
featureClass_dataset = arcpy.GetParameterAsText(3) # path to the .gdb (or Folder if .shp) of the feature classes

if not arcpy.Exists(in_tablePath): # check in case table doesn't exist...
 arcpy.AddError('Table, ' + in_tablePath + ', does not exist...')
 
else: # table does exist, continue
 arcpy.AddMessage('Using table: ' + in_tablePath) # let the user know the table path
 
 arcpy.CreateTable_management(out_tablePath, out_tableName) # create the output table...
 
 arcpy.AddField_management(out_tablePath+'\\'+out_tableName, "outputData", "DOUBLE") # add a field to the new table
 arcpy.CalculateField_management(out_tablePath+'\\'+out_tableName, "outputData", 0, "PYTHON") # calculate its value as 0 for now... (optional)
 
 rowList = arcpy.SearchCursor(in_tablePath)
 for row in rowList: # for every row
  
  parcelID = row.getValue("parcelid")
  
  # i.e. if the feature classes are in C:\GIS\Data\parcels.gdb, and parcelID = 7
  #  will return the feature class C:\GIS\Data\parcels.gdb\7
  featureClass = featureClass_dataset + '\\' + str(parcelID) # make sure parcelID is changed to a string as well
  
  if not arcpy.Exists(featureClass): # check in case feature class doesn't exist...
   arcpy.AddError('Feature Class, ' + featureClass + ', does not exist...')
   
  else: # feature class exists, continue...
   
   ## Do calculations here...
   
   # don't know how you want to write the results, you could use an insert cursor
   #  or copy selected rows to a new table (or the new table...)


Good luck!
Stacy
0 Kudos
HollyCopeland
Deactivated User
Thanks, Stacy. I don't think the code you sent was quite what I was looking for (but I could be wrong as I am relatively new to Python). This code from a friend was more along the lines of what I needed. Thanks, though!

import arcpy
arcpy.env.workspace = r'C:\WorkSpace\Projects\Code\Special Request\Copeland\src\SageGrouseTestCode.gdb'
parcelIDTable = 'ParcelNBTable'
lekFC = 'LekParcelFC'
try:
    parcelRows = arcpy.SearchCursor(parcelIDTable, "", "", "ParcelNBTable_PARCELNB", "")
    for parcelRow in parcelRows:
        print 'Parcel Number: %s' %parcelRow.ParcelNBTable_PARCELNB
        try:
            if len(parcelRow.ParcelNBTable_PARCELNB) > 0:
                expression = '"PARCELNB" = ' + "'" + parcelRow.ParcelNBTable_PARCELNB + "'"
                lekRows = arcpy.SearchCursor(lekFC, expression, "", "", "Complex A")
                for lekRow in lekRows:
                    print '\tLek complex name: %s\tCount %i' %(lekRow.Complex, lekRow.MAX_MostRecentPeakCount)
        except:
            print arcpy.GetMessages(2)
except:
    print arcpy.GetMessages(2)
print 'fin'
0 Kudos