print "Starting function" # Define and setup variables, tables, key field etc calc_table = arcpy.MakeTableView_management(table_path) vol_tab = join_table_path strata_tab = "in_memory/temp" arcpy.MakeTableView_management(vol_tab, strata_tab) joinField = "STRATA" # Create list of value fields, leaving out OID field and key/join field flistObj = arcpy.ListFields(strata_tab) flist = [] for f in flistObj: if f.type != "OID" and f.name != joinField: flist.append(f.name) # Create empty dict object then populate each key with a sub dict by row using value fields as keys strataDict = {} for r in arcpy.SearchCursor(strata_tab): fieldvaldict = {} for field in flist: fieldvaldict[field] = r.getValue(field) strataDict[r.getValue(joinField)] = fieldvaldict del strata_tab, flistObj
rows = arcpy.UpdateCursor(calc_table, "\"%s\" IS NOT NULL" % joinField) for row in rows: strata = row.getValue(joinField) variable = strataDict[strata]["sub_key_field"]
species = [ ("C","Fb","FB_STEMS"),("C","Sw","SW_STEMS"),("C","Pj","PJ_STEMS"), # 0,1,2 ("C","Pl","PJ_STEMS"),("C","Lt","LT_STEMS"),("C","Sb","SB_STEMS"), # 3,4,5 ("D","Bw","BW_STEMS"),("D","Aw","AW_STEMS"),("D","Pb","PB_STEMS") # 6,7,8 ] sp_fields = [("SP1","SP1_PER"),("SP2","SP2_PER"),("SP3","SP3_PER"), ("SP4","SP4_PER"),("SP5","SP5_PER")] print "Beginning updates" rows = arcpy.UpdateCursor(calc_table, "\"%s\" IS NOT NULL" % joinField) for row in rows: strata = row.getValue(joinField) for sp, per in sp_fields: sp_type = row.getValue(sp) spp_f = float(row.getValue(per)) if spp_f > 0: for grp, spec, stem in species: stem_f = strataDict[strata][stem] (...)
Solved! Go to Solution.
eZones = r"C:\temp\NLF.gdb\NLF_EM_2009_Dissolve" eZoneName = str("UniqueID") iField = "All_EM_List" eIncidents = r"C:\temp\NLF.gdb\NLF_EM_2009_Identity" emNameField = ("E_MASS") joinField = "Dissolve_FID" arcpy.MakeFeatureLayer_management(eIncidents, "eIncidentsLayer") with arcpy.da.UpdateCursor(eZones, (eZoneName, iField)) as zoneRows: for zone in zoneRows: eZoneNameString = zone[0] queryString = '"' + eZoneName + '" = ' + "'" + eZoneNameString + "'" arcpy.MakeFeatureLayer_management(eZones, "CurrenteZonesLayer", queryString) try: arcpy.SelectLayerByLocation_management("eIncidentsLayer", "CONTAINED_BY", "CurrenteZonesLayer") emassDict = dict() for row in arcpy.SearchCursor("eIncidentsLayer"): emName = row.getValue(emNameField) snName = row.getValue(joinField) if snName in emassDict: emassDict[snName].append(emName) else: emassDict[snName] = [emName] print emassDict if eZoneNameString == [snName]: zone[1] = [emName] zoneRows.updateRow(zone) except arcpy.ExecuteError: print(arcpy.GetMessages(0)) finally: arcpy.Delete_management("CurrenteZonesLayer") arcpy.Delete_management("eIncidentsLayer") del zone, zoneRows
if eZoneNameString == [snName]:
This is how I've implemented the new DA cursors. The cursor is limited to the fields you want to update with the join field located at index 0, the dictionary is created with row[0] as the key and easily accessed by the update cursor.
#Define fields to update, and the field to use as join field
Fields = ['Direction', 'Cost', 'year', 'Color']
Key = "UniqueID"
Fields.insert(0, Key)
#Create Dictionaries ; The dictionaries store the values from the update table in memory
x = len(Fields)
UpdateDict = {}
#Iterates through all values in the table and stores them in the update dictionary
#Dictionary format; Join Field value : list of field values
with arcpy.da.SearchCursor(Table, Fields) as cursor:
for row in cursor:
FieldValDict = {}
for y in range(1,x):
FieldValDict
UpdateDict[row[0]] = FieldValDict
#Updates the FC from the Update Dictionary
#Uses the Join Field value to look up update values
with arcpy.da.UpdateCursor(Input, Fields) as cursor:
for row in cursor:
for y in range(1,x):
row
cursor.updateRow(row)
You could replace:
FieldValDict = {}
for y in range(1,x):
FieldValDict
= row
With:
FieldValDict = dict(zip(fields[1:], row[1:]))
Hi all,
Arcapi has these kind of functions for joining tables:
join_using_dict
https://github.com/NERC-CEH/arcapi/blob/master/arcapi.py/#L2052-2153
update_col_from_dict
https://github.com/NERC-CEH/arcapi/blob/master/arcapi.py/#L1207-1272
In many cases it is much faster than the Join Tool.
Filip.