Type something to search...

Code

This page shows some of the useful code that we share with our clients to help them with automating geoprocessing and other tasks. An example of these other tasks includes data auditing by creating data inventory lists that can be output to CSV files. For example, we have included a script here that lists all of the metadata for spatial data in multiple file geodatabases in a directory, then outputs the results to a CSV file. A second script can then be run to batch update any missing metadata.

If there is a script that you would like to see in the list below, then please send us a message because we have written hundreds of scripts like these, and we may already have a script that does what you want, or we could write the script for you. Likewise many of these scripts use ArcPy, however, we can rewrite any of these scripts without ArcPy if needed, and in some cases, we have already written some of the scripts without ArcPy, so that you can use them if you do not have ArcGIS software.

All of our Python scripts use Python 3, and we use Visual Studio Code for our IDE. Please let us know if you have any issues running any of the scripts, and remember when running any of these scripts that modify data, it is important to back up your data in case you need to revert your changes back.

List all Enterprise Geodatabases and Feature Classes, and their Metadata

#

This script is useful for auditing metadata. The script can be run across multiple enterprise geodatabases, and will create an output CSV file with these columns: 'Geodatabase', 'Feature Dataset', 'Feature Class', 'Spatial Reference', 'Row Count', 'Column Count', 'Metadata Title', 'Metadata Description', 'Metadata Summary', 'Metadata Credits', 'Metadata Access Constraints'.

To use this script, you need to change Line 19 to point to your SDE connection files.

import csv
import os

from arcpy import Describe
from arcpy import env
from arcpy import GetCount_management
from arcpy import ListDatasets
from arcpy import ListFeatureClasses
from arcpy import ListFields
from arcpy import metadata as md

# the output CSV file name
output_csv_file = 'enterprise_gdbs_results.csv'

# or use this to make the output CSV file name the same as the script name
# output_csv_file = os.path.basename(__file__).replace('.py','.csv')

# change this path to use your SDE connection files
sde_connection_files = 'c:/gis/sde_connection_files'

with open(output_csv_file,'w',newline='') as csv_file:
    csv_writer = csv.writer(csv_file, delimiter=',')
    column_headers = [
        'Geodatabase', 'Feature Dataset', 'Feature Class', 
        'Spatial Reference', 'Row Count', 'Column Count', 
        'Metadata Title', 'Metadata Description', 'Metadata Summary',
        'Metadata Credits', 'Metadata Access Constraints'
    ]
    
    csv_writer.writerow(column_headers)

    for (folder, dirs, files) in os.walk(sde_connection_files):
        for file in files:
            if(not file.startswith('_') and file.endswith('.sde')):
                geodatabase = file.split('@')[1]

                wksp = os.path.join(folder,file)
                env.workspace = wksp

                datasets = ListDatasets(feature_type='feature')
                datasets = [''] + datasets if datasets is not None else []

                for dataset in datasets:
                    for feature_class in ListFeatureClasses(dataset):
                        desc = Describe(feature_class)
                        spatial_reference = desc.SpatialReference.factoryCode
                        count = GetCount_management(feature_class)[0]
                        column_count = len(ListFields(feature_class))
                        metadata = md.Metadata(feature_class)

                        row = [
                            geodatabase,dataset,feature_class,
                            spatial_reference,count,column_count,
                            metadata.title,metadata.description,
                            metadata.summary,metadata.credits,
                            metadata.accessConstraints    
                        ]

                        print(row)
                        
                        csv_writer.writerow(row)