Select to view content in your preferred language

slow performance using cursor to read/write data

5469
10
03-04-2011 01:09 AM
CrispinCooper
Emerging Contributor
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 = x
        point_array_y = 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
Tags (2)
0 Kudos
10 Replies
ChrisSnyder
Honored Contributor
Geometry Objects in v10: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001z000000.htm

I think Kim originally was suggesting (per an understanding of what the .dll was doing), that perhaps the entire workflow could be accomplished entirely in arcpy, and the inefficiency of reading and writing the data with cursors could be eliminated. Perhaps the function of the .dll could be replicated in Spatial Analyst (assuming you have a license!).
0 Kudos