<?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: how can I optimize a script that requires spatial queries, polygon intersects polygon? in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/how-can-i-optimize-a-script-that-requires-spatial/m-p/1187205#M56661</link>
    <description>&lt;P&gt;Cursors do take quite some time, so you should minimize them. You only need to instantiate a cursor 3 times:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;read the zones&lt;/LI&gt;&lt;LI&gt;read the provinces&lt;/LI&gt;&lt;LI&gt;update the zones&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;You can do everything else without cursors:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# [ [Zones.OBJECTID, Zones.Shape] ]
zones = [ [oid, shp] for oid, shp in arcpy.da.SearchCursor("Zones", ["OBJECTID", "SHAPE@"])]
# [ [Provinces.Name, Provinces.Shape] ]
provinces = [ [name, shp] for name, shp in arcpy.da.SearchCursor("Provinces", ["Name", "SHAPE@"])]

# {Zones.OBJECTID: Zones.ProvinceNames}
zone_dict = dict()

for z_oid, z_shp in zones:
    p_names = [p_name for p_name, p_shp in provinces if z_shp.overlaps(p_shp)]
    zone_dict[z_oid] = ", ".join(p_names)

with arcpy.da.UpdateCursor("Zones", ["OBJECTID", "ProvinceNames"]) as cursor:
    for oid, names in cursor:
        try:
            names = zone_dict[oid]
            cursor.updateRow([oid, names])
        except KeyError:
            print(f"could not find any province names for zone with OID {oid}")&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 28 Jun 2022 10:23:17 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-06-28T10:23:17Z</dc:date>
    <item>
      <title>how can I optimize a script that requires spatial queries, polygon intersects polygon?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-can-i-optimize-a-script-that-requires-spatial/m-p/1187105#M56645</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I want to make a script in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;ArcGis desktop 10.8&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for a simple procedure, write for each zone the province that intersects.&lt;BR /&gt;The desired result is for the zone layer to have a text field with the names of the province(s) it intersects.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The zone layer has 1008 records&lt;/LI&gt;&lt;LI&gt;the province layer has 51 records.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So far, the options have not been optimal, &lt;STRONG&gt;I detail them below&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;These are my questions:&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is there an optimal way to perform this procedure and to program it?&lt;/LI&gt;&lt;LI&gt;Does the evaluation of the spatial relations for example: polygon.contains(polygon) use the spatial index or should I activate it?&lt;/LI&gt;&lt;LI&gt;How can I perform this process using the spatial index?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;For this I am writing two scripts,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;the first attempt was&lt;/STRONG&gt;:&lt;BR /&gt;- create a cursor on the zones layer&lt;BR /&gt;- select the feature with using its ID with arcpy.SelectLayerByAttribute_management&lt;BR /&gt;- then select by location with the provinces&lt;BR /&gt;- apply a cursor over the selected to get the list of provinces that will write in the attribute&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;The problem:&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;this takes too long&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Second attempt:&lt;/STRONG&gt;&lt;BR /&gt;- I create an empty dictionary&lt;BR /&gt;- apply a cursor on zones getting the shape field and OBJECTID&lt;BR /&gt;- Iterate with a for&lt;BR /&gt;- for each case I create a province cursor&lt;BR /&gt;- iterate on the provinces&lt;BR /&gt;- I evaluate which provinces intersect the zone, in the dictionary I store the id of the zone as a key with a list of the provinces.&amp;nbsp;For this I evaluate if the geometry of zones intersects the province geometry, For example: zona.overlap(province)&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;The problem:&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;it still takes a long time, 1.8 minutes for 40 zone and they are 1008&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 00:59:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-can-i-optimize-a-script-that-requires-spatial/m-p/1187105#M56645</guid>
      <dc:creator>LuisEduardoPerezGraterol</dc:creator>
      <dc:date>2022-06-28T00:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: how can I optimize a script that requires spatial queries, polygon intersects polygon?</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/how-can-i-optimize-a-script-that-requires-spatial/m-p/1187205#M56661</link>
      <description>&lt;P&gt;Cursors do take quite some time, so you should minimize them. You only need to instantiate a cursor 3 times:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;read the zones&lt;/LI&gt;&lt;LI&gt;read the provinces&lt;/LI&gt;&lt;LI&gt;update the zones&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;You can do everything else without cursors:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# [ [Zones.OBJECTID, Zones.Shape] ]
zones = [ [oid, shp] for oid, shp in arcpy.da.SearchCursor("Zones", ["OBJECTID", "SHAPE@"])]
# [ [Provinces.Name, Provinces.Shape] ]
provinces = [ [name, shp] for name, shp in arcpy.da.SearchCursor("Provinces", ["Name", "SHAPE@"])]

# {Zones.OBJECTID: Zones.ProvinceNames}
zone_dict = dict()

for z_oid, z_shp in zones:
    p_names = [p_name for p_name, p_shp in provinces if z_shp.overlaps(p_shp)]
    zone_dict[z_oid] = ", ".join(p_names)

with arcpy.da.UpdateCursor("Zones", ["OBJECTID", "ProvinceNames"]) as cursor:
    for oid, names in cursor:
        try:
            names = zone_dict[oid]
            cursor.updateRow([oid, names])
        except KeyError:
            print(f"could not find any province names for zone with OID {oid}")&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 28 Jun 2022 10:23:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/how-can-i-optimize-a-script-that-requires-spatial/m-p/1187205#M56661</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-06-28T10:23:17Z</dc:date>
    </item>
  </channel>
</rss>

