crispincooper

slow performance using cursor to read/write data

Discussion created by crispincooper on Mar 4, 2011
Latest reply on Jun 24, 2011 by csny490
Hi

I wonder if anyone here can point me in the right direction?  My script reads data out of a feature class using a cursor (slow), processes it using an external dll (fast) and writes it back to the feature class again (slow).

Is this really the fastest way to get data in/out of arc in python, or is there some way of improving performance?  Or do I need to get an EDN license and use C++ or such like?

Thanks in advance, C.

import arcpy
import ctypes

#get input params
in_polyline_feature_class = arcpy.GetParameterAsText(0)
in_arc_idfield = arcpy.GetParameterAsText(1)
shapefieldname = arcpy.Describe(in_polyline_feature_class).ShapeFieldName

#set up dll
dll = ctypes.windll.LoadLibrary(u'c:\\path\\to\\my\\dll.dll')
dll.get_output.restype = ctypes.c_double
handle = ctypes.c_void_p(dll.init())

# define function for sending feature data to dll
def send_data(arcid,points,elev):
    point_array_x = (ctypes.c_double*len(points))()
    point_array_y = (ctypes.c_double*len(points))()
    for i,(x,y) in enumerate(points):
        point_array_x[i] = x
        point_array_y[i] = y
    dll.send_data(handle,arcid,len(points),point_array_x,point_array_y)

# read feature data with cursor, send to dll
rows = arcpy.SearchCursor(in_polyline_feature_class)
for row in rows:
    # read id and shape
    shape = row.getValue(shapefieldname)
    arcid = row.getValue(in_arc_idfield)
    
    # extract points from shape
    pointlist = []
    for i in range(shape.partCount):
        for point in shape.getPart(i):
            pointlist.append((point.X,point.Y))

    # send data
    send_data(arcid,pointlist,(startelev,endelev))

# process data
dll.process(handle)

# read back output into table
arcpy.AddField_management(in_polyline_feature_class,'my field name','DOUBLE')
rows = arcpy.UpdateCursor(in_polyline_feature_class)
for row in rows:
    row.setValue('my field name',dll.get_output(handle,row.getValue(idfield)))
    rows.updateRow(row) 
del row
del rows

Outcomes