6

In an effort to improve my Python skills, I am trying to build code to automate the Compress and Analyze commands for our SQL 2008 R2 SDE database (code below). The Compress code works as does the Analyze, save for one exception. The Analyze function (and the remaining Python code) quits if it encounters a feature class or table that is not registered.

Is there a way in Python to determine if a feature class or table is registered so that I can exempt\skip unregistered items? Failing that, what would be the best way to 'on failure, resume next' so my code doesn't just quit.


# Date:    Jan, 2014
# Version: ArcGIS 10.0
# This script is designed to compress an SDE Geodatabase and then
# loop through and analyze the statistics ofeach feature dataset, feature class and table.
# Code is designed to log the entire process to a text file 
# Created by Mike Long - ml56067@gmail.com - 7/16/09
# Edited by HGil - 1/2014

# Import system, Geoprocessing, time modules
import sys, string, os, arcgisscripting, time, smtplib

# Set the date.
Date = time.strftime("%m-%d-%Y", time.localtime())

# Set the time.
Time = time.strftime("%I:%M:%S %p", time.localtime())

# Create the Geoprocessor object
gp = arcgisscripting.create()

gp.CheckProduct("ArcEditor") #Checks the license level.

gp.SetProduct("arceditor") #Sets the license level.

print "Process started at " + str(Date) + " " + str(Time) + "." + "\n"

# Set up the log file.
LogFile = file('C:\\GIS\\DB_LogFiles\\SDE_GISPROD-' + Date + '.txt', 'w') #Creates a log file with current date.
output = open('C:\\GIS\\DB_LogFiles\\SDE_GISPROD-' + Date + '.txt', 'w') #Path to log file.
output.write(str("Process started at " + str(Date) + " " + str(Time) + "." + "\n")) #Write the start time to the log file.

# Load required toolboxes
gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")

#----------------------------
try:
    # Compress the database
    print "Begining Compress..." + "\n"
    gp.toolbox = "management"

    gp.compress("C:\\...<datasource path>...\\ArcCatalog\\TEST_PROD.sde") 

    print gp.GetMessages() + "\n"
    output.write(gp.GetMessages()+ "\n")

except:
    print gp.GetMessages() + "\n"
    output.write(gp.GetMessages()+ "\n")
#----------------------------

analyzeworkspace = "C:\\...<datasource path>...\\Desktop10.0\\ArcCatalog\\TEST_PROD.sde"

try:
    # Loop through and Analyze all feature datasets
    gp.Workspace = analyzeworkspace
    FCList = gp.ListFeatureClasses ("*", "all")
    FC = FCList.Next()
    while FC:
        gp.Analyze_management(FC, "BUSINESS;FEATURE;ADDS;DELETES")
        print gp.GetMessages() + "\n"
        output.write(gp.GetMessages() + "\n")
        FC = FCList.Next()
    # Loop through and analyze all the Feature classes
    gp.Workspace = analyzeworkspace
    FDList = gp.ListDatasets ("*", "all")
    FD = FDList.Next()
    while FD:
        gp.Analyze_management(FD, "BUSINESS;FEATURE;ADDS;DELETES")
        print gp.GetMessages() + "\n"
        output.write(gp.GetMessages() + "\n")
        FD = FDList.Next()
    # Loop through and analyze all the Tables.
    # The if-else statements set the script to skip over Multi-Versioned views.
    TBList = gp.ListTables ("*", "all")
    TB = TBList.Next()
    while TB:
        if '_MV' in TB:
            print "Skipping Multi-Versioned View"
            output.write("Skipping Multi-Versioned View " + TB + "\n")
        else:
            gp.Analyze_management(TB, "BUSINESS;FEATURE;ADDS;DELETES")
            print gp.GetMessages() + "\n"
            output.write(gp.GetMessages() + "\n")
        TB = TBList.Next()

except:
    print gp.GetMessages()
    output.write(gp.GetMessages())
#----------------------------

# Sets the Date & Time since the script started.
Date = time.strftime("%m-%d-%Y", time.localtime())# Set the date.
Time = time.strftime("%I:%M:%S %p", time.localtime()) # Set the time.
output.write(str("Process completed at " + str(Date) + " " + str(Time) + "." + "\n")) # Write the start time to the log file.

output.close() # Closes the log file.

print "Process completed at " + str(Date) + " " + str(Time) + "."
2
  • 1
    this question may be the same gis.stackexchange.com/questions/16936/… – geogeek Dec 30 '13 at 21:08
  • Thanks for the link. Question is similar, but not quite the same...I am not looking for versioning info, but if the data is registered with the database. I don't see that as an option in the Describe() function. – hgil Dec 30 '13 at 21:22
7

The objects that are registered with geodatabase are listed in several system tables.

Registering a table with the geodatabase adds a record to the following geodatabase system tables:

GDB_ITEMS
GDB_ITEMRELATIONSHIPS
TABLE_REGISTRY (or sde_table_registry)
COLUMN_REGISTRY (or sde_column_registry)

If the table contains a spatial column, a record is added to these geodatabase system tables as well:

LAYERS (or sde_layers)
GEOMETRY_COLUMNS (or sde_geometry_columns)

This is true since 10.0 which you have. So, the option that makes most sense to me is to query these system tables (provided that you have permissions to read the schema in which these tables were created in) and see if the objects with those names present there or not. You could do this with arcpy.da.SearchCursor (I would prefer this), ArcSDESQLExecute or just any method capable of doing selections on a table.

Another thing is that all datasets registered with geodatabase will have a special unique field called ObjectID. So, you might also just check if this field exists. If not >> the table is not registered with geodatabase.

More general info on registering with geodatabase

PS. Just in case you would like to register a table with geodatabase, there is a special GP tool called Register with Geodatabase available since 10.0.

3
  • This looks like it will do the trick. Up voted and checked. Thanks! – hgil Dec 31 '13 at 15:06
  • 1
    You can create an OBJECTID column that isn't registered with ArcSDE, and you can create a registered rowid column that isn't "OBJECTID", so these aren't good indicators of ArcSDE registration. – Vince Dec 31 '13 at 16:04
  • 1
    Another thing: Presence in GDB_ITEMS is a good indicator that the table has been registered, but not if the table exists, since the GDB registry could be corrupted by a SQL 'DROP' or ArcSDE 'sdetable -o delete'. You could have a condition where the table exists and has been registered with the geodatabase, but not with ArcSDE (because it was improperly deleted then recreated). – Vince Dec 31 '13 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.