<?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: Faster way to iterate through Feature Datasets and Add to Map? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581422#M73695</link>
    <description>&lt;P&gt;In addition, something that is also quickly done, is with the search cursor there is the option to use sql queries within the cursor that will allow for you to filter data based on a certain criteria. This might be faster than using the cursor without a sql statement.&lt;/P&gt;&lt;P&gt;Note: Script updated on 2/4/2025&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import SearchCursor as Search

feature = r'&amp;lt;filepath&amp;gt;'
fieldnames = [&amp;lt;list of fieldnames&amp;gt;]
query = 'WHERE &amp;lt;fieldname&amp;gt; IS NOT NULL'
# You can also set the query to as an f string to f"WHERE NONE NOT IN {tuple(fieldnames)}"

limit = 25
hasvalues = False
with Search( feature , fieldnames , query ) as cursor:
	for row in cursor:
		if len( set(row).difference({None}) ) &amp;gt; 0: hasvalues = True
        if limit == 0 or hasvalues is True: break
		limit = limit - 1
if hasvalues is True: x = 'Do something'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Feb 2025 19:52:14 GMT</pubDate>
    <dc:creator>RPGIS</dc:creator>
    <dc:date>2025-02-04T19:52:14Z</dc:date>
    <item>
      <title>Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579908#M73664</link>
      <description>&lt;P&gt;Hi community! Wondering if y'all have any ideas to help speed this up.&lt;/P&gt;&lt;P&gt;(ArcGIS Pro toolbox with my arcpy script embedded, accessing data on a faraway server)&lt;/P&gt;&lt;P&gt;I have a data model (GDB) with 15+ feature datasets and 240+ feature classes. I'm doing is iterating through each feature class in each feature dataset, check if there is data by counting the rows, and if that count is greater than 0 adding that feature class to the map.&lt;/P&gt;&lt;P&gt;It adds non-empty feature classes to the map but takes 20+ minutes for large datasets (where feature classes contain more than 1000+ rows) and 15 minutes for small datasets (&amp;lt;50 rows) when data is run from the server. Less than 5 minutes when the GDB is copied to C: drive, but that's still quite a while.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using a cursor instead, and I've been unsuccessful looking for methods other than GetCount_management(feature_class).getOutput to figure out if a feature class is empty or not-- I am guessing this slows me down since the iteration itself isn't the slowest part. Most of the slow is when counting the rows and adding them to the map. I've also been looking into multiprocessing but can't seem to wrap my head around that yet.&lt;/P&gt;&lt;P&gt;If this turns out to be a 'your data is too far away / connectivity' issue, which I'm afraid it is, at least I'll have a second opinion on that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

gdb_path = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Connection to GDB successful.")

# Set the path to your ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.AddMessage("Current project selected.")

# Get the first map in project
map = aprx.listMaps()[0]
arcpy.AddMessage("Getting first map in project.")
arcpy.env.workspace = gdb_path
arcpy.AddMessage("Made GDB your current workspace.")

feature_datasets = arcpy.ListDatasets(feature_type='Feature')

# Iterate through each feature dataset in the geodatabase
for dataset in feature_datasets:
    arcpy.env.workspace = f"{gdb_path}/{dataset}"
    feature_classes = arcpy.ListFeatureClasses()
    arcpy.AddMessage(f"Checked {dataset}")

    # Iterate through each feature class in the feature dataset
    for feature_class in feature_classes:
        # Check if the feature class has any rows
        if int(arcpy.GetCount_management(feature_class).getOutput(0)) &amp;gt; 0:
            # Add the feature class to the map
            data_file = f"{gdb_path}/{dataset}/{feature_class}"
            map.addDataFromPath(data_file)
            arcpy.AddMessage(f"Added {feature_class}.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 14:17:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579908#M73664</guid>
      <dc:creator>madyson-bradford</dc:creator>
      <dc:date>2025-01-29T14:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579933#M73665</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/604297"&gt;@madyson-bradford&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;One thing that I may suggest is using a counter to identify if there are any records in a feature class. If you are looking to see if a feature class is empty, rather than lopping through to get a count of all records, you can simply set a count limit. If the count limit is exceeded then you can add that feature class to the map. This should significantly speed up the process.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy,
from arcpy import ListFields
from arcpy import SearchCursor as Search

Feature = r'&amp;lt;feature class file path&amp;gt;'
fields = [ field.name for field in ListFields( F )]

counter = 0
limit = 25
with Search( Feature , fields ) as cursor:
    for row in cursor:
        poprecords = len([ x for x in row if x is not None])
        if poprecords &amp;gt; 0: counter += 1
        if counter &amp;gt; limit: break&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 29 Jan 2025 14:56:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579933#M73665</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2025-01-29T14:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579939#M73666</link>
      <description>&lt;P&gt;Wow, thank you! Let me give that a shot and I'll let you know how it goes. What a great idea!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 15:04:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579939#M73666</guid>
      <dc:creator>madyson-bradford</dc:creator>
      <dc:date>2025-01-29T15:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579946#M73667</link>
      <description>&lt;P&gt;I was curious, perhaps you could compare with a larger dataset.&amp;nbsp; This one only has 500ish records but several million points.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%%timeit
fc00 = r"C:\arcpro_npg\Project_npg\npgeom.gdb\Ontario_LCConic"
result = arcpy.management.GetCount(fc00)
n = int(result[0])

60.9 ms ± 1.75 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%%timeit
fc00 = r"C:\arcpro_npg\Project_npg\npgeom.gdb\Ontario_LCConic"
ont = FeatureClassToNumPyArray(fc00, ['OID@'])
N = ont.shape[0]

814 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)&lt;/LI-CODE&gt;&lt;P&gt;The difference in speed may make it worthwhile doing a quick check using numpy before attempting to load it.&amp;nbsp; Worth a thought.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 15:17:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579946#M73667</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-01-29T15:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579971#M73669</link>
      <description>&lt;P&gt;almost forgot what is buried with FeatureClassToNumPyArray (I suspect)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%%timeit
fc00 = r"C:\arcpro_npg\Project_npg\npgeom.gdb\Ontario_LCConic"
with arcpy.da.SearchCursor(fc00, "OID@") as cursor:
    a = cursor._as_narray()
    n = len(a)

828 µs ± 19 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)&lt;/LI-CODE&gt;&lt;P&gt;if you like the "with" thing&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 15:57:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1579971#M73669</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-01-29T15:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581251#M73686</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/604297"&gt;@madyson-bradford&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I often find the switch to workspaces using the arcpy.env.workspace setting to add duration to scripts. Alternatively you could use the da.Walk (&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/walk.htm" target="_blank" rel="noopener"&gt;docs&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;The GetCount is another time sink and you could try replacing with the da.Describe (&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/describe.htm" target="_blank" rel="noopener"&gt;docs&lt;/A&gt;) and accessing the extent property which is an Extent object. If the XMin (or other values) are greater than 0, then there are features in the dataset, if not greater than zero there is no extent and no features present. Each component of the Extent is a float (&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/extent.htm" target="_blank" rel="noopener"&gt;docs&lt;/A&gt;)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;desc = arcpy.da.Describe(fc)

if desc["extent"].XMin &amp;gt; 0:
    print("Extent Found")
    map.addDataFromPath(desc["catalogPath"])
else:
    print("Empty")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 11:19:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581251#M73686</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-02-03T11:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581422#M73695</link>
      <description>&lt;P&gt;In addition, something that is also quickly done, is with the search cursor there is the option to use sql queries within the cursor that will allow for you to filter data based on a certain criteria. This might be faster than using the cursor without a sql statement.&lt;/P&gt;&lt;P&gt;Note: Script updated on 2/4/2025&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import SearchCursor as Search

feature = r'&amp;lt;filepath&amp;gt;'
fieldnames = [&amp;lt;list of fieldnames&amp;gt;]
query = 'WHERE &amp;lt;fieldname&amp;gt; IS NOT NULL'
# You can also set the query to as an f string to f"WHERE NONE NOT IN {tuple(fieldnames)}"

limit = 25
hasvalues = False
with Search( feature , fieldnames , query ) as cursor:
	for row in cursor:
		if len( set(row).difference({None}) ) &amp;gt; 0: hasvalues = True
        if limit == 0 or hasvalues is True: break
		limit = limit - 1
if hasvalues is True: x = 'Do something'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2025 19:52:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581422#M73695</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2025-02-04T19:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581918#M73702</link>
      <description>&lt;P&gt;Haven't benchmarked this, but throwing my potential solution into the ring. There's a chance this will be faster since it doesn't look beyond the first row of the feature class (as opposed to loading the entire thing into an array).&lt;/P&gt;&lt;P&gt;I updated my example to fit into your da.Walk solution (2/11/25)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def has_data(fc):
    with arcpy.da.SearchCursor(fc, ["OID@"]) as cursor:
        return next(cursor, None) is not None

for dirpath, workspaces, datatypes in arcpy.da.Walk(gdb_path, datatype="FeatureClass"): 
    for datatype in datatypes: 
        data_file = f"{dirpath}/{datatype}"  
        if has_data(data_file):
            map.addDataFromPath(data_file)  
            arcpy.AddMessage(f"Added {datatype}.")
        else:
            arcpy.AddMessage(f"Did not add {datatype}, nothing there.") &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2025 17:31:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1581918#M73702</guid>
      <dc:creator>BrennanSmith1</dc:creator>
      <dc:date>2025-02-11T17:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1583786#M73725</link>
      <description>&lt;P&gt;Thank you everyone for your input! I've been mixing and matching solutions, and I settled on a solution that is a mix of a few.&amp;nbsp;&lt;STRONG&gt;I have gotten down to a lightning 27.17 seconds when connecting to a GDB in my C: Drive (under 10 seconds for small projects!), and a considerably faster 5 minutes 23 seconds for data on the faraway server (which is amazing considering it used to take 20+ minutes)!&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I used da.Walk as recommended by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/417766"&gt;@Clubdebambos&lt;/a&gt;&amp;nbsp;&amp;nbsp;since turns out you were right, setting my workspace environment for each dataset iteration was taking up a considerable amount of time. I stuck with the cursor idea from&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/22623"&gt;@RPGIS&lt;/a&gt;, and combined that with the suggestion to use cursor._as_narray() from&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;, which worked really well and funnily enough the only docs I could find on using that was your 4/2017 blog on &lt;A href="https://community.esri.com/t5/python-blog/cursors-a-cursory-overview/ba-p/902592" target="_self"&gt;Cursors... a cursory overview.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Thanks everyone for all your help!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for dirpath, workspaces, datatypes in arcpy.da.Walk(gdb_path, datatype="FeatureClass"): 
    for datatype in datatypes: 
        data_file = f"{dirpath}/{datatype}"  
        with arcpy.da.SearchCursor(data_file, "OID@") as cursor:  
            a = cursor._as_narray()   
            if len(a) &amp;gt; 0:  
                # Add the feature class to the map  
                map.addDataFromPath(data_file)  
                arcpy.AddMessage(f"Added {datatype}.")
            else:
                arcpy.AddMessage(f"Did not add {datatype}, nothing there.") &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 21:29:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1583786#M73725</guid>
      <dc:creator>madyson-bradford</dc:creator>
      <dc:date>2025-02-10T21:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Faster way to iterate through Feature Datasets and Add to Map?</title>
      <link>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1583867#M73726</link>
      <description>&lt;P&gt;one can learn from history, although&amp;nbsp;4/2017 isn't that long ago.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_squinting_face:"&gt;😆&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2025 00:16:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/faster-way-to-iterate-through-feature-datasets-and/m-p/1583867#M73726</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-02-11T00:16:11Z</dc:date>
    </item>
  </channel>
</rss>

