Tuesday, 8 September 2015

cursor - Using UpdateCursor for joined field in ArcPy?


I have a huge point vector data (around 3 million point; in a gdb) with attributes and I have table (created with Near analysis.). I want to copy one of the field values to data. So I joined the table and tried the field calculator to do the trick, but after 16 hours I realized it just froze (it worked with similar data). So the next that could work I think is the UpdateCursor. Unfortunately I got an Error: "TypeError: cannot update join table"


tried this:


field1 = '!temp_Features.leftd!'

field2 = '!leftt:NEAR_DIST!'

with this:


cursor = arcpy.da.UpdateCursor("layer2",(field1,field2))

this:


fields = ['temp_Features:leftd', 'leftt:NEAR_DIST']

with this:


cursor = arcpy.da.UpdateCursor("layer2",fields)


Same error every time.


(I have ArcGIS with advanced license)



Answer



Some time ago I've made this replacement of field calculator. Saved me awful amount of time:


import arcpy, traceback, os, sys
from arcpy import env
env.overwriteOutput = True
layoutFC=arcpy.GetParameterAsText(0)
joinFieldS=arcpy.GetParameterAsText(1)

sourseField=arcpy.GetParameterAsText(2)

destFC=arcpy.GetParameterAsText(3)
joinFieldD=arcpy.GetParameterAsText(4)
destField=arcpy.GetParameterAsText(5)

result=arcpy.GetCount_management(destFC)
nF=int(result.getOutput(0))
arcpy.SetProgressor("step", "", 0, nF,1)


try:
def showPyMessage():
arcpy.AddMessage(str(time.ctime()) + " - " + message)
destFields=arcpy.ListFields(destFC)
sTable=arcpy.da.TableToNumPyArray(layoutFC,(joinFieldS,sourseField))
dictFeatures = {}
for j,s in sTable:dictFeatures[j]=s
with arcpy.da.UpdateCursor(destFC, (joinFieldD,destField)) as cursor:
for j,s in cursor:
arcpy.SetProgressorPosition()

try:
v=dictFeatures[j]
cursor.updateRow((j,v))
except: pass
except:
message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()

Interface:



enter image description here enter image description here


Please note if no join found for some record, value in calculated field kept unchanged.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...