Wednesday 21 February 2018

arcgis 10.0 - How to determine using Python whether ArcSDE table is registered with geodatabase?


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:\\......\\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:\\......\\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) + "."


Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...