Select to view content in your preferred language

Get a list of feature classes without setting env

5309
7
Jump to solution
08-04-2015 09:12 AM
JimO_Leary1
Deactivated User

I would like to get a list of feature classes in multiple GDBs. This is inconvenient because I have to set env.workspace on each one in order to use ListFeatureClasses(). When I set the environment on the second GDB, I lose access to the feature classes in the first GDB.

Is there a way to get access to feature classes in each GDB without having to set the environment or use ListFeatureClasses()?

Thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Honored Contributor

Using arcpy.da.Walk, you never have to set the workspace environment setting, just give a string path for the root directory you want checked.  If you use that with the os module, you can join together the filepath and filename with os.path.join.

Example from the help: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Ex. Use the Walk function to catalog polygon feature classes.

import arcpy
import os

workspace = "c:/data"
feature_classes = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

for dirpath, dirnames, filenames in walk:
   for filename in filenames:
   feature_classes.append(os.path.join(dirpath, filename))

Of course you could leave it open so it check all file types. 

View solution in original post

7 Replies
IanMurray
Honored Contributor

Why not make an empty list and append the output from each ListFeatureClasses() to it?  That way you don't lose access to them.

Or you could use arcpy.da.Walk and go through multiple geodatabases.

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

BlakeTerhune
MVP Regular Contributor
When I set the environment on the second GDB, I lose access to the feature classes in the first GDB.

Could you clarify exactly what you're experiencing here? As long as you have the file name and path for the feature class, you can always "access" it. Setting the environment workspace or using ListFeatureClasses() doesn't cause the feature class to be unavailable to anything. You should be able to use any of the suggestions from Ian Murray​.

0 Kudos
JimO_Leary1
Deactivated User

This code return  "IOError: Roads does not exist."

----------------code BEGINS----------------------

from arcpy import *

env.workspace = r"C:\folder1\USA.gdb"

# Get a list of the feature classes in the current workspace

lstFeatureClass = arcpy.ListFeatureClasses()

# Make an empty list to hold the feature classes in the current GDB

lstFeatureClassStored = []

# Populate the list with the feature classes from the current workspace

for featureClass in lstFeatureClass:

    lstFeatureClassStored.append(featureClass)

# Change the workspace

env.workspace = r"C:\folder2\Iowa.gdb"

# Loop through the stored feature classes. Here is where the exception is raised

for featureClassStored in lstFeatureClassStored:

    desc = arcpy.Describe(featureClassStored)

    print desc.Name

--------------------code ENDS---------------------------

However, I think it is because the lstFeatureClassStored only stores the name of the feature class, not the feature class object itself, and when I change the environment it doesn't know "Roads".

By adding the whole path to the feature class as below, I can then loop through the list and access each feature class by the absolute file name.

-------------------code BEGINS---------------------------

import arcpy

from arcpy import *

pathToFirstWorkspace = r"C:\folder1\USA.gdb"

env.workspace = pathToFirstWorkspace

lstFeatureClass = arcpy.ListFeatureClasses()

lstFeatureClassStored = []

for featureClass in lstFeatureClass:

    # Store the absolute path to the feature class inside the GDB

    pathToFeatureClass = r"%s\%s" % (pathToFirstWorkspace,featureClass)

    lstFeatureClassStored.append(pathToFeatureClass)

# Change the workspace

env.workspace = r"C:\folder2\Iowa.gdb"

# Access the feature class by the absolute path to the GDB

for pathToFeatureClass in lstFeatureClassStored:

    desc = arcpy.Describe(pathToFeatureClass)

    print desc.Name

--------------------code ENDS-----------------------------

So this works, but it still doesn't answer my question, is there a way to get a listing of classes without using env.workspace and ListFeatureClasses()? It seems very limiting to have to set the environment when you want to know what is in a geodatabase.

Thanks

0 Kudos
IanMurray
Honored Contributor

Using arcpy.da.Walk, you never have to set the workspace environment setting, just give a string path for the root directory you want checked.  If you use that with the os module, you can join together the filepath and filename with os.path.join.

Example from the help: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Ex. Use the Walk function to catalog polygon feature classes.

import arcpy
import os

workspace = "c:/data"
feature_classes = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

for dirpath, dirnames, filenames in walk:
   for filename in filenames:
   feature_classes.append(os.path.join(dirpath, filename))

Of course you could leave it open so it check all file types. 

JimO_Leary1
Deactivated User

Yes, that works. You still have to join the full path name to the feature class name, but you don't have to set the env.workspace.

Thanks

0 Kudos
BlakeTerhune
MVP Regular Contributor

Yes, that's exactly what I was getting at. Thanks for taking the time to put that together, Ian.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I deleted my response with the addin since you found a simpler solution and mine wasn't complete.  No use having a partial out there.  I you ever want to try it let me know.  Once done I'll be posting....but slow going with other higher priority tasks.

0 Kudos