Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Mastering arcpy: A Guide to Listing Features and Objects in Geodatabases

Geodatabases and Arcpy

Geodatabases are commonly used to store spatial data, and arcpy is a Python module used to work with geospatial data in ArcGIS. One of the fundamental tasks in working with geodatabases is to list features and objects contained within them. This allows you to get a better understanding of the data in the geodatabase and perform further analysis or manipulation.

In this blog post, we will explore some tips and tricks for listing features and objects in geodatabases using arcpy. We will cover how to filter and select feature classes, list all features within a feature class, and list all tables in a geodatabase. We will also provide examples of how to list all feature classes and tables with specific fields or names, and how to list all feature classes and tables within a specific dataset.

By the end of this post, you’ll have a solid understanding of how to effectively list features and objects in geodatabases using arcpy, and be able to incorporate these techniques into your own geospatial data workflows.

Want to stay ahead of the geospatial curve? Listen to our podcast!

How to list all feature classes in a File Geodatabase and all features within the feature classes

Here is an example script that demonstrates how to do this:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# List all feature classes in the geodatabase
feature_classes = arcpy.ListFeatureClasses()

# List all feature classes within feature datasets in the geodatabase
for dataset in arcpy.ListDatasets("", "Feature"):
    for fc in arcpy.ListFeatureClasses("", "All", dataset):
        feature_classes.append(fc)

# Print the list of feature classes
for fc in feature_classes:
    print(fc)

In this example, we first set the workspace to the File Geodatabase using the arcpy.env.workspace function. We then use the arcpy.ListFeatureClasses() function to list all feature classes in the geodatabase.

To list feature classes within feature datasets, we use a nested loop that first lists all feature datasets in the geodatabase using arcpy.ListDatasets("", "Feature"), and then lists all feature classes within each dataset using arcpy.ListFeatureClasses("", "All", dataset). We append each feature class to the feature_classes list.

Finally, we print the list of feature classes using a for loop. This will output a list of all feature classes in the geodatabase, including those within feature datasets.

How to list all features in multiple Geodatabases

To list all features in multiple Geodatabases, you can modify the previous script to include a loop that iterates through a list of geodatabases. Here’s an example of how you can modify the script:

import arcpy

# List of geodatabases to search for feature classes
gdb_list = ["C:/data/geodatabase1.gdb", "C:/data/geodatabase2.gdb", "C:/data/geodatabase3.gdb"]

for gdb in gdb_list:
    print("Geodatabase: " + gdb)
    
    # Set the workspace to the geodatabase
    arcpy.env.workspace = gdb
    
    # List all feature classes in the geodatabase
    feature_classes = arcpy.ListFeatureClasses()
    
    # List all feature classes within feature datasets in the geodatabase
    for dataset in arcpy.ListDatasets("", "Feature"):
        for fc in arcpy.ListFeatureClasses("", "All", dataset):
            feature_classes.append(fc)
    
    # Print the list of feature classes
    for fc in feature_classes:
        print(fc)

In this modified script, we first create a list of geodatabase paths gdb_list that we want to search for feature classes. We then use a for loop to iterate through each geodatabase in the list.

Inside the loop, we set the workspace to the current geodatabase using arcpy.env.workspace = gdb. We then list all feature classes in the geodatabase using arcpy.ListFeatureClasses(), and list all feature classes within feature datasets in the geodatabase using a nested loop as before.

Finally, we print the list of feature classes for the current geodatabase using a for loop. This will output a list of all feature classes in each geodatabase, including those within feature datasets.

What if you want to filter and select based on some criteria?

Here are a few examples of how to filter and select feature classes in a geodatabase using arcpy:

Filter feature classes by name:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# List all feature classes in the geodatabase
feature_classes = arcpy.ListFeatureClasses()

# Filter feature classes by name
filtered_fcs = [fc for fc in feature_classes if "roads" in fc]

# Print the list of filtered feature classes
for fc in filtered_fcs:
    print(fc)

In this example, we first list all feature classes in the geodatabase using arcpy.ListFeatureClasses(). We then filter the feature classes by name using a list comprehension, and select only those feature classes that contain the string “roads” in their name. Finally, we print the list of filtered feature classes using a for loop.

Select feature class by type:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# Select all point feature classes in the geodatabase
point_fcs = arcpy.ListFeatureClasses(feature_type="Point")

# Print the list of point feature classes
for fc in point_fcs:
    print(fc)

In this example, we use the feature_type parameter of the arcpy.ListFeatureClasses() function to select only point feature classes in the geodatabase. We then print the list of point feature classes using a for loop.

Filter feature classes by spatial reference:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# Create a spatial reference object for WGS84
sr_wgs84 = arcpy.SpatialReference(4326)

# List all feature classes in the geodatabase
feature_classes = arcpy.ListFeatureClasses()

# Filter feature classes by spatial reference
filtered_fcs = []
for fc in feature_classes:
    desc = arcpy.Describe(fc)
    if desc.spatialReference == sr_wgs84:
        filtered_fcs.append(fc)

# Print the list of filtered feature classes
for fc in filtered_fcs:
    print(fc)

In this example, we create a spatial reference object for WGS84 using the arcpy.SpatialReference() function. We then list all feature classes in the geodatabase using arcpy.ListFeatureClasses(), and filter the feature classes by spatial reference by checking the spatial reference of each feature class using the arcpy.Describe() function. We append only those feature classes that have the same spatial reference as WGS84 to the filtered_fcs list. Finally, we print the list of filtered feature classes using a for loop.

listing all the features within a featerclass

Using a search cursor to iterate over features:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# Feature class to list features for
fc = "roads"

# Use a search cursor to iterate over features
with arcpy.da.SearchCursor(fc, ["OID@"]) as cursor:
    for row in cursor:
        print(row[0])

In this example, we first set the workspace to the geodatabase using arcpy.env.workspace. We then specify the feature class we want to list features for using the fc variable.

We use a search cursor to iterate over features in the feature class. The cursor is created using the arcpy.da.SearchCursor() function and the "OID@" field is specified to return the Object IDs of each feature. We then use a for loop to iterate over the cursor and print the Object ID of each feature.

Using the arcpy.GetCount_management() function to count features:

import arcpy

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# Feature class to list features for
fc = "roads"

# Get the count of features in the feature class
count = arcpy.GetCount_management(fc)[0]

# Print the count of features
print("There are " + count + " features in " + fc)

In this example, we again set the workspace to the geodatabase using arcpy.env.workspace, and specify the feature class we want to list features for using the fc variable.

We use the arcpy.GetCount_management() function to get the count of features in the feature class, and store the result in the count variable. We then print the count of features using a string concatenation.

Using the arcpy.FeatureClassToNumPyArray() function to create a numpy array of features:

import arcpy
import numpy as np

# Set the workspace to the geodatabase
arcpy.env.workspace = "C:/data/mygeodatabase.gdb"

# Feature class to list features for
fc = "roads"

# Use the FeatureClassToNumPyArray function to create a numpy array of features
arr = arcpy.FeatureClassToNumPyArray(fc)

# Print the Object IDs of the features
print(arr["OBJECTID"])

In this example, we set the workspace to the geodatabase using arcpy.env.workspace, and specify the feature class we want to list features for using the fc variable.

We use the arcpy.FeatureClassToNumPyArray() function to create a numpy array of features from the feature class, and store the result in the arr variable. We then print the Object IDs of the features using arr["OBJECTID"]. You can replace "OBJECTID" with any field name in the feature class to list values for that field for each feature.

FAQ’s : listing features or objects in geodatabases using arcpy

How do I list all tables in a geodatabase?

You can list all tables in a geodatabase using the arcpy.ListTables() function. This function returns a list of table names in the workspace.

How do I list all feature classes and tables in a geodatabase?

You can list all feature classes and tables in a geodatabase using the arcpy.ListFeatureClasses() and arcpy.ListTables() functions, respectively. These functions return lists of feature class and table names in the workspace.

How do I list all feature classes and tables with a specific field?

You can list all feature classes and tables with a specific field using the arcpy.ListFeatureClasses() and arcpy.ListTables() functions with the optional field_type parameter. Set the field_type parameter to the data type of the field you are searching for (e.g. “TEXT”, “DOUBLE”, etc.). This will return a list of feature classes or tables that have at least one field of the specified data type.

How do I list all feature classes and tables with a specific field name?

You can list all feature classes and tables with a specific field name using the arcpy.ListFeatureClasses() and arcpy.ListTables() functions with the optional wildcard parameter. Set the wildcard parameter to the name of the field you are searching for (e.g. “name”, “*_name”, etc.). This will return a list of feature classes or tables that have at least one field with a matching name.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.