Select to view content in your preferred language

da.walk can't get featureclasses in featuredataset?

1154
6
08-30-2023 02:32 AM
pyfans
by
Emerging Contributor

Can arcpy.da.Walk() function find featureclasses within a featuredataset in gdb?  For example, there  are a featuredataset 'DT' and a featureclass 'A' in gdb, and a featureclass 'B' in 'DT', after walk(gdb), it can only find DT and A, but no B.  Is it normal ?

I use 10.8.2.

Tags (2)
0 Kudos
6 Replies
AlfredBaldenweck
MVP Regular Contributor

Each feature dataset is considered its own environment by arcpy, so functions like Walk() and ListFeatureClasses() will ignore stuff in a feature dataset if run on a geodatabase.

What you have to do instead is iterate through what's returned by Walk, check if it's a featuredataset, then change the environment to the featuredataset and list those feature classes in it.

 

Here's some sample code I wrote. In this case, "fd" was originally the output of arcpy.ListFeatureDatasets(), but you could sub in the files from Walk()

 

 

 

dirP = #whatever gdb you were running Walk() on
desc= (arcpy.da.Describe(fd))
if 'datasetType' in desc:
    if desc['datasetType'] == "FeatureDataset":
        arcpy.env.workspace = os.path.join(dirP, fd)
        fdFCList = arcpy.ListFeatureClasses()
        if fdFCList:
           for fc in fdFCList:
                fList.append([os.path.join(
                             desc['catalogPath'],fc)])

        # Reset the workspace or else this breaks.
        arcpy.env.workspace = dirP

 

 

 

 

0 Kudos
Luke_Pinner
MVP Regular Contributor

@AlfredBaldenweck wrote:

Each feature dataset is considered its own environment by arcpy, so functions like Walk() and ListFeatureClasses() will ignore stuff in a feature dataset if run on a geodatabase.


Not quite right for Walk.  You just need to keep iterating through the Walk results.  Any feature datasets are considered workspaces, so Walk will recurse to them and return their contents.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Good to know, thanks for the clarification. 

I avoid Walk because it doesn't do quite what I want it to do.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Works for me in ArcMap 10.8.2.  Given a FGDB with 2 FCs, "A" and "B" with "B" in feature dataset "FD":

Luke_Pinner_0-1693431703274.png

import arcpy

for parent, workspaces, datasets in arcpy.da.Walk("Default.gdb"):
    print("parent: %s"%parent)
    print("workspaces: %s"%workspaces)
    print("datasets: %s"%datasets)

Output showing that "B" is listed correctly in the feature dataset "FD":

parent: Default.gdb
workspaces: [u'FD']
datasets: [u'A']
parent: Default.gdb\FD
workspaces: []
datasets: [u'B']

 

0 Kudos
pyfans
by
Emerging Contributor

Yes, after try again, I find it works when there are no any non-english (or say non-ascii?) character in gdb, but if there are Chinese character in gdb or featuredataset or featureclass's name, it just can't get featureclasses in featuredataset.

I tried 10.8.2 and 10.4.1.

0 Kudos
Luke_Pinner
MVP Regular Contributor

I think feature class names need to use ascii characters only. From the help:

Luke_Pinner_0-1693455792006.png

 

0 Kudos