<?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 Speeding up Spatial Select? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401768#M31640</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am working with a large dataset that I need to cursor through and retain the results of some spatial selects to generate another dataset for use outside of the GIS. The input are the junctions and edges of a road network. The process flow, idea anyway, is to grab an edge, perform a spatial select, figure out what junctions are connected, store this information and move on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This works very well, but is VERY slow. I have a spatial overlay approach to generate the same data but this is not satisfactory when the data are connected based on vertical elevation values. The code looks something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
arcpy.MakeFeatureLayer_management(edgeSource, 'edge_lyr')
arcpy.MakeFeatureLayer_management(junctionSource, 'junc_lyr')

ff = T.time()
edgeList = {}
nodePos = {}
goNogo = {}
edges = arcpy.SearchCursor(edgeSource, "", "")
for edge in edges:
&amp;nbsp;&amp;nbsp;&amp;nbsp; newID = edge.getValue(edgeIDItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cost = edge.getValue(edgeCostItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp; expr =&amp;nbsp; edgeIDItem + " = %s " % newID
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("edge_lyr", "NEW_SELECTION", expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')

&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; juncs = arcpy.SearchCursor('junc_lyr')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for junc in juncs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jid = junc.getValue(juncIDItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; geom = junc.Shape.getPart()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge.append(jid)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nodePos[jid] = (geom.X, geom.Y)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; goNogo[jid] = [junc.getValue(nogoIDItem), junc.getValue(nogoDistItem)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge.append(cost)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(newEdge) == 3:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; edgeList[newID] = newEdge

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone have any ideas how a process like this might be sped up?&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;Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 03 Apr 2012 19:15:14 GMT</pubDate>
    <dc:creator>StevenPrager</dc:creator>
    <dc:date>2012-04-03T19:15:14Z</dc:date>
    <item>
      <title>Speeding up Spatial Select?</title>
      <link>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401768#M31640</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am working with a large dataset that I need to cursor through and retain the results of some spatial selects to generate another dataset for use outside of the GIS. The input are the junctions and edges of a road network. The process flow, idea anyway, is to grab an edge, perform a spatial select, figure out what junctions are connected, store this information and move on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This works very well, but is VERY slow. I have a spatial overlay approach to generate the same data but this is not satisfactory when the data are connected based on vertical elevation values. The code looks something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
arcpy.MakeFeatureLayer_management(edgeSource, 'edge_lyr')
arcpy.MakeFeatureLayer_management(junctionSource, 'junc_lyr')

ff = T.time()
edgeList = {}
nodePos = {}
goNogo = {}
edges = arcpy.SearchCursor(edgeSource, "", "")
for edge in edges:
&amp;nbsp;&amp;nbsp;&amp;nbsp; newID = edge.getValue(edgeIDItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cost = edge.getValue(edgeCostItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp; expr =&amp;nbsp; edgeIDItem + " = %s " % newID
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("edge_lyr", "NEW_SELECTION", expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')

&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; juncs = arcpy.SearchCursor('junc_lyr')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for junc in juncs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jid = junc.getValue(juncIDItem)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; geom = junc.Shape.getPart()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge.append(jid)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nodePos[jid] = (geom.X, geom.Y)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; goNogo[jid] = [junc.getValue(nogoIDItem), junc.getValue(nogoDistItem)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; newEdge.append(cost)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(newEdge) == 3:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; edgeList[newID] = newEdge

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone have any ideas how a process like this might be sped up?&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;Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Apr 2012 19:15:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401768#M31640</guid>
      <dc:creator>StevenPrager</dc:creator>
      <dc:date>2012-04-03T19:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Speeding up Spatial Select?</title>
      <link>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401769#M31641</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Instead of doing the double selections, try using a feature layer with a where clause.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So replace this&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("edge_lyr", "NEW_SELECTION", expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;With something more like this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(edgeSource, 'edge_lyr', expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, having nested cursors and GP tools inside a cursor are generally bad ideas. Is there no way you can avoid processing all this line by line?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:19:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401769#M31641</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T18:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: Speeding up Spatial Select?</title>
      <link>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401770#M31642</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Instead of doing the double selections, try using a feature layer with a where clause.&lt;BR /&gt;&lt;BR /&gt;So replace this&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("edge_lyr", "NEW_SELECTION", expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')&lt;/PRE&gt;&lt;BR /&gt;With something more like this.&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(edgeSource, 'edge_lyr', expr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('junc_lyr', 'intersect', 'edge_lyr')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Also, having nested cursors and GP tools inside a cursor are generally bad ideas. Is there no way you can avoid processing all this line by line?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, Mathew, I will definitely try that. Your sense is that it would be faster to make a new feature layer every time using the expression, rather than building the selection off of an existing feature layer. Worth a shot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As mentioned, I have a technique that makes this infinitely faster using spatial overlay then processing the results. However, it only works when the data are planar embedded. If the data have a Z, I can use a 3d spatial select an junctions in one plane will select only the edges in that plane (e.g., a hwy overpass). In sort, I need the network topology that Arc is storing internally, but I cannot see any way to get that via the Python API...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:19:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/speeding-up-spatial-select/m-p/401770#M31642</guid>
      <dc:creator>StevenPrager</dc:creator>
      <dc:date>2021-12-11T18:19:10Z</dc:date>
    </item>
  </channel>
</rss>

