ArcGIS 10 - LisctWorkspace / ListDataset - multiple types

769
2
04-25-2014 04:55 AM
tomhails
New Contributor
Hi,

Using PYTHON 2.6 & ArcGIS 10 - I'm writing a script in which I:

  1. Loop through all workspace under a given directory.

  2. Grab the details of all the Feature Classes, Rasters etc (both within and outside of Datasets).

  3. Populate a XLS with my results.


I'm finding that:

            workspaces = set(arcpy.ListWorkspaces('', 'FILEGDB'))-\
                         set(arcpy.ListWorkspaces('', 'COVERAGE'))


and

                rasters = set(arcpy.ListDatasets('','Raster'))-\
                          set(arcpy.ListDatasets('','Mosaic'))-\
                          set(arcpy.ListDatasets('','RasterCatalog'))
[

only provide access to the 1st type of Workspace/Dataset provided e.g. 'FILEGDB' and 'Raster'


        for root, dirs, files in os.walk(start_folder):
            folder_count += 1
            arcpy.env.workspace = root

            wspace = ""
            workspaces = set(arcpy.ListWorkspaces('', 'FILEGDB'))-\
                         set(arcpy.ListWorkspaces('', 'COVERAGE'))

            for wspace in workspaces:
                arcpy.env.workspace = os.path.join(root, wspace)

                # List out Raster Dataset & Raster Catalogs etc
                rasters = set(arcpy.ListDatasets('','Raster'))-\
                          set(arcpy.ListDatasets('','Mosaic'))-\
                          set(arcpy.ListDatasets('','RasterCatalog'))

                for rast in rasters:
                    inner_list_fcs = ['GDB Raster', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]


If I do the following I get all the datasets specified:

                rasters = set(arcpy.ListDatasets('','Raster'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Raster', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]

                rasters = set(arcpy.ListDatasets('','RasterCatalog'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Raster Catalog', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]

                rasters = set(arcpy.ListDatasets('','Mosaic'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Mosaic', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]
Tags (2)
0 Kudos
2 Replies
JoshuaChisholm
Frequent Contributor
Would something like this work for you Tom?
import os
import arcpy

p=r'C:\Path\To\StartFolder'

masterFCList=[]
masterRastList=[]

for root, dirs, files in os.walk(p):
    for name in dirs:
        arcpy.env.workspace = os.path.join(root, name)
        masterFCList += [os.path.join(root,name,fc) for fc in arcpy.ListFeatureClasses()]
        masterRastList += [os.path.join(root,name,rast) for rast in arcpy.ListRasters()]


You'll still need to get the lists to an XLS. I CSV would be a lot easier to do. Let me know if you need help with that.
Good luck!
0 Kudos
tomhails
New Contributor
Would something like this work for you Tom?
import os
import arcpy

p=r'C:\Path\To\StartFolder'

masterFCList=[]
masterRastList=[]

for root, dirs, files in os.walk(p):
    for name in dirs:
        arcpy.env.workspace = os.path.join(root, name)
        masterFCList += [os.path.join(root,name,fc) for fc in arcpy.ListFeatureClasses()]
        masterRastList += [os.path.join(root,name,rast) for rast in arcpy.ListRasters()]


You'll still need to get the lists to an XLS. I CSV would be a lot easier to do. Let me know if you need help with that.
Good luck!


Thanks Justin

Unfortunately that will only list Raster Datasets. I also would like list all the on Raster Catalogs/ Mosaics. Exporting to XLS is relatively straightforward with xlwt.

I've resorted to the following:

import os
import arcpy
import xlwt
from tempfile import TemporaryFile
from datetime import datetime, timedelta

# Function to create a Nested List of GIS datasets -
#   - File Trype, Workspace Path, Dataset Name (ESRI), File Name
#   - Including flat file (shp, dwg, etc)
#   - and ESRI File GDB datasets
#       - Highlight if the dataset is within an ESRI Dataset
def list_fcs(start_folder, file_wild_card=(), wild_card=None, feature_type=None):
    preexisting_wks = arcpy.env.workspace
    arcpy.env.workspace = start_folder
    folder_count = 0

    try:
        print 'Searched:\r',
        inner_list_fcs = ["File Type", "Workspace Path","Dataset Name","File Name"]
        list_fcs = [inner_list_fcs]

        for root, dirs, files in os.walk(start_folder):
            folder_count += 1
            arcpy.env.workspace = root

            # go through files e.g. shp, dwg etc
            for file in files:
                #if file.lower().endswith((file_wild_card)):
                if file.lower().endswith(file_wild_card):
                    inner_list_fcs = [file[-3:].upper(), root, "N/A", file]
                    list_fcs += [inner_list_fcs]

            # Get Access to File GDB datasets
            # Currently not working with Coverages
            wspace = ""
            workspaces = set(arcpy.ListWorkspaces('', 'FILEGDB'))-\
                         set(arcpy.ListWorkspaces('', 'COVERAGE'))

            for wspace in workspaces:
                arcpy.env.workspace = os.path.join(root, wspace)
                fcs = arcpy.ListFeatureClasses(wild_card,
                                               feature_type)

                if fcs:
                    for fc in fcs:
                        inner_list_fcs = ['GDB FC', root, "N/A", fc]
                        list_fcs += [inner_list_fcs]

                # Get the FCs in Feature Datasets
                for dataset in arcpy.ListDatasets('', 'FEATURE'):
                    ds_fcs = arcpy.ListFeatureClasses(wild_card,
                                                      feature_type,
                                                      dataset)
                    if ds_fcs:
                        for fc in ds_fcs:
                            inner_list_fcs = ['GDB FC', wspace, dataset, fc]

                            list_fcs += [inner_list_fcs]

                # List out Raster Dataset & Raster Catalogs
                # It appears that you can only access each dataset type
                # separately

##                rasters = set(arcpy.ListDatasets('','Raster'))-\
##                          set(arcpy.ListDatasets('','Mosaic'))-\
##                          set(arcpy.ListDatasets('','RasterCatalog'))

                rasters = set(arcpy.ListDatasets('','Raster'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Raster', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]

                rasters = set(arcpy.ListDatasets('','RasterCatalog'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Raster Catalog', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]

                rasters = set(arcpy.ListDatasets('','Mosaic'))
                for rast in rasters:
                    inner_list_fcs = ['GDB Mosaic', wspace, "N/A", rast]
                    list_fcs += [inner_list_fcs]


            # print the number of folder searched if divisible by 25
            if folder_count % 25 == 0:
                print 'Searched: %s folders\r' %folder_count,

    except Exception as err:
        print err.message
    finally:
        arcpy.env.workspace = preexisting_wks
        print 'Searched: %s folders' % (folder_count)

    return list_fcs, folder_count

# Set Local Variables
xls_wb = r"C:\Data\Code\ListFeatureClasses\List.xls"    # XLS Log file
start_folder = r"C:\Data\Code\ListFeatureClasses\TestData"    # Start Folder
file_ext = ('dwg', 'shp', 'xls')    # Tuple of Flat file extensions (lower case)
# Date & Time Formatts
fmt1 = '%Y-%m-%d %H:%M:%S'
fmt2 = '%Y%m%d%H%M'
fmt3 = '%a %d %b %H:%M'

# Get Date & Time at the start of the process
d1 = datetime.now()

print "Starting Feature Class List Process: %s" % (d1.strftime(fmt3))

# Populate a list with GIS dataset
outer_list, folder_count = list_fcs(start_folder, file_ext)

# Get Date & Time after creating the list
d2 = datetime.now()
walk_time = d2 - d1
t_minutes, t_seconds = divmod(walk_time.seconds, 60)
print "Walked through folders duration: %s:%s (mins:secs)" % (t_minutes, t_seconds)

# Export the list to XLS
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for x,inner_list in enumerate(outer_list):
    for y, e in enumerate(inner_list):
        sheet1.write(x,y,e)     # Row Column Value

book.save(xls_wb)
book.save(TemporaryFile())

# Get Date & Time after creating the XLS
d3 = datetime.now()
xls_time = d3 - d2
t_minutes, t_seconds = divmod(xls_time.seconds, 60)
print "Excel export duration: %d3:%d2 (mins:secs)" % (t_minutes, t_seconds)

total_time = d3 - d1
t_minutes, t_seconds = divmod(total_time.seconds, 60)
print "Total duration: %d3:%d2 (mins:secs)" % (t_minutes, t_seconds)
print "This process has searched %s directories" % (folder_count)
0 Kudos