Monday, January 19, 2015

Python (ArcPy) - Export list of Feature datasets and classes from a geodatabase to CSV

These are 35 geodatabases and each geodatabases have several feature datasets and feature classes. Each feature datasets has several feature classes as well.


Open an ArcPy window in ArcGIS and type the below mentioned python code.


import arcpy

dir = r'I:\GDBs'
arcpy.env.workspace = dir

gdbList = arcpy.ListWorkspaces('*','FileGDB')

for gdb in gdbList:
    arcpy.env.workspace = gdb               #--change working directory to each GDB in list
    datasetList = arcpy.ListDatasets('*','Feature')     #--make a list of all (if any) feature datasets that exist in current GDB
    fcList = arcpy.ListFeatureClasses()         #--make a list of all feature in current GDB (root)
    for fc in fcList:
        print arcpy.env.workspace,fc            #--print directory,fc name
    for dataset in datasetList:
        arcpy.env.workspace = dataset   #--change working directory to each dataset (if any) in list
        fcList = arcpy.ListFeatureClasses()     #--make a list of all feature in current GDB (current dataset)
        for fc in fcList:
            print arcpy.env.workspace,fc        #--print directory,fc name
        arcpy.env.workspace = gdb

It will give you the output like this:


Copy the output in a text pad abd saved as GDB2csv.txt. I have only copied 30 lines out of approximately 1000 lines present in the textpad. Each line represent one feature class.



Here you will observe that each line has 2 characters. One is " " and second one is "\". We need to bring this file into this format: Geodatabase Name, Feature Dataset name, Feature Class Name. 

After this please refer to the post "Split a String at 2 characters into 3 columns"



No comments:

Post a Comment