<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: slow performance using cursor to read/write data in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107683#M8318</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again guys...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;kimo: do you mean using arcpy.CopyFeatures_management?&amp;nbsp; I just tried that and it ran slower than using a cursor to read all the geometries.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;jscheirer: do I need an EDN license to use arcobjects?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 08 Mar 2011 11:04:37 GMT</pubDate>
    <dc:creator>CrispinCooper</dc:creator>
    <dc:date>2011-03-08T11:04:37Z</dc:date>
    <item>
      <title>slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107678#M8313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wonder if anyone here can point me in the right direction?&amp;nbsp; 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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this really the fastest way to get data in/out of arc in python, or is there some way of improving performance?&amp;nbsp; Or do I need to get an EDN license and use C++ or such like?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance, C.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
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):
&amp;nbsp;&amp;nbsp;&amp;nbsp; point_array_x = (ctypes.c_double*len(points))()
&amp;nbsp;&amp;nbsp;&amp;nbsp; point_array_y = (ctypes.c_double*len(points))()
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i,(x,y) in enumerate(points):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; point_array_x&lt;I&gt; = x
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; point_array_y&lt;I&gt; = y
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # read id and shape
&amp;nbsp;&amp;nbsp;&amp;nbsp; shape = row.getValue(shapefieldname)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcid = row.getValue(in_arc_idfield)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # extract points from shape
&amp;nbsp;&amp;nbsp;&amp;nbsp; pointlist = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(shape.partCount):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for point in shape.getPart(i):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointlist.append((point.X,point.Y))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # send data
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue('my field name',dll.get_output(handle,row.getValue(idfield)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row) 
del row
del rows
&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Mar 2011 09:09:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107678#M8313</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-03-04T09:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107679#M8314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Crispin,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm surprised no one already answered this. yes, slowness with python cursors has always been my experience. Using .NET SDK should be faster (at least it always is for me using C#.NET).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Depending on what you're doing and where the data is stored (type of database or shapefile) I'd recommend GDAL OGR Python bindings (&lt;/SPAN&gt;&lt;A href="http://pypi.python.org/pypi/GDAL/"&gt;http://pypi.python.org/pypi/GDAL/&lt;/A&gt;&lt;SPAN&gt;). They read shapefiles and postgresql/postgis dbs among other formats waaaayy faster. Or if just accessing the geometries for point arrays, then try Shapely (&lt;/SPAN&gt;&lt;A href="http://trac.gispython.org/lab/wiki/Shapely"&gt;http://trac.gispython.org/lab/wiki/Shapely&lt;/A&gt;&lt;SPAN&gt;). Or if you're into JVM scripting languages like Jython or Groovy try GeoScript (&lt;/SPAN&gt;&lt;A href="http://geoscript.org/"&gt;http://geoscript.org/&lt;/A&gt;&lt;SPAN&gt;). None of these talk with PGDBs or FGDBs...yet (though FGDB is now open).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But it's a minor tweak (and easier in most cases) to export your data to run a process, then after it is done upload into ESRI proprietary data structs after. At least that's my experience...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;long live open source&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Mar 2011 16:19:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107679#M8314</guid>
      <dc:creator>GregCorradini</dc:creator>
      <dc:date>2011-03-04T16:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107680#M8315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Greg,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks for your informative reply.&amp;nbsp; Unfortunately I need to be able to work with PGDBs/FGDBs.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried exporting my data using arctoolbox FeatureClassToShapefile; that took twice as long for the export as my cursor reading routine.&amp;nbsp; Is there a quicker way?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Crispin&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 Mar 2011 14:56:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107680#M8315</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-03-07T14:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107681#M8316</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I see you are using a cursor and looping through each feature to reconstruct a geometry object. At 10.0 you can read a whole featureclass into an array of geometry objects in one step without a cursor. You can also write out an array to a featureclass as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That avoids the cursor both ways, but the disadvantage is that the attributes are separated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You don't say what you are doing with the featureclass in the DLL, I am curious what you have to do that is not already available in the geoprocessing tools.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 Mar 2011 18:16:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107681#M8316</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2011-03-07T18:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107682#M8317</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You don't need to use the InsertCursor in Python -- you can do it in ArcObjects in your DLL. Please note the C++ code on &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001500000013000000.htm"&gt;this help page&lt;/A&gt;&lt;SPAN&gt; for opening a cursor from C++.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 Mar 2011 19:59:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107682#M8317</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2011-03-07T19:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107683#M8318</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again guys...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;kimo: do you mean using arcpy.CopyFeatures_management?&amp;nbsp; I just tried that and it ran slower than using a cursor to read all the geometries.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;jscheirer: do I need an EDN license to use arcobjects?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Mar 2011 11:04:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107683#M8318</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-03-08T11:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107684#M8319</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;No, you can use ArcObjects at any license level. All you will require is Visual Studio or Visual Studio Express.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Mar 2011 14:15:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107684#M8319</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2011-03-08T14:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107685#M8320</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I tried implementing the C++ code, but it's even slower!&amp;nbsp; 38 seconds to read through a large-ish (17,000 line) feature class instead of 20 for the original python cursor.&amp;nbsp; It's compiled in release configuration not debug.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think I did this in native C++ not .NET (I hope this is what visual studio 2008 does if you select 'no CLR support', because the project target cannot be changed from '.NET framework'...) - but, just to be sure I also tried with the wrapper method on &lt;/SPAN&gt;&lt;A href="http://edndoc.esri.com/arcobjects/8.3/GettingStarted/ThePerformanceofArcObjectsinDotNet.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;this page&lt;/A&gt;&lt;SPAN&gt;.&amp;nbsp; Still the same result; 38 seconds.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The inner c++ loop is shown below, for test purposes it retrieves the OID of each line and writes it to a new field (also reads the geometry but does nothing with it):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 esriGeometryType gt;
 for (ipCursor-&amp;gt;NextFeature(&amp;amp;ipRow); ipRow != NULL; ipCursor-&amp;gt;NextFeature(&amp;amp;ipRow))
 {
&amp;nbsp;&amp;nbsp;&amp;nbsp; ipRow-&amp;gt;get_Shape(&amp;amp;ipShape);

&amp;nbsp;&amp;nbsp;&amp;nbsp; long oid;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ipRow-&amp;gt;get_OID(&amp;amp;oid);

&amp;nbsp;&amp;nbsp;&amp;nbsp; VARIANT value;
&amp;nbsp;&amp;nbsp;&amp;nbsp; value.vt = VT_R8;
&amp;nbsp;&amp;nbsp;&amp;nbsp; value.dblVal = (double)oid;

&amp;nbsp;&amp;nbsp;&amp;nbsp; ipRow-&amp;gt;put_Value(outfieldIndex, value);

&amp;nbsp;&amp;nbsp;&amp;nbsp; ipRow-&amp;gt;Store();
 }
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this as good as it gets, or have I done something else wrong?&amp;nbsp; Thanks again, C.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:31:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107685#M8320</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2021-12-11T06:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107686#M8321</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&lt;BLOCKQUOTE&gt;kimo;83348 wrote:&lt;BR /&gt;I see you are using a cursor and looping through each feature to reconstruct a geometry object. At 10.0 you can read a whole featureclass into an array of geometry objects in one step without a cursor. You can also write out an array to a featureclass as well.&lt;/BLOCKQUOTE&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That avoids the cursor both ways, but the disadvantage is that the attributes are separated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How do you read a feature class into an arcpy.Arrray() without using Cursors?&amp;nbsp; Doe this work with a polygon feature class??&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Apr 2011 19:26:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107686#M8321</guid>
      <dc:creator>DaveVerbyla</dc:creator>
      <dc:date>2011-04-01T19:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107687#M8322</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I see you are using a cursor and looping through each feature to reconstruct a geometry object. At 10.0 you can read a whole featureclass into an array of geometry objects in one step without a cursor. You can also write out an array to a featureclass as well.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would also like to know how to do this. I've been given the task of speeding up a script which makes heavy use of cursors inside nested 'while' loops. I'm still reading some of the suggestions from this forum, and this sounds like potentially the cleanest one.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 14:43:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107687#M8322</guid>
      <dc:creator>MichaelHoward1</dc:creator>
      <dc:date>2011-06-24T14:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: slow performance using cursor to read/write data</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107688#M8323</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Geometry Objects in v10: &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001z000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001z000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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!).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 15:52:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-using-cursor-to-read-write-data/m-p/107688#M8323</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2011-06-24T15:52:06Z</dc:date>
    </item>
  </channel>
</rss>

