Arcpy / Arcgis 10: Automatizar Compress Y Analyze

Una de las tareas que todo administrador de bases de datos geográficasArcGis/SDE debería realizar con frecuencia (e incluso a diario), es la ejecución el proceso de compresión (Compress). Dicho proceso permite optimizar el almacenamiento de los registros en el motor de bases de datos relacional.

Con el siguiente script de python utilizando el paquete de programación de arcgis conocido como arcpy se puede además de realizar el compress (Compress_management) , permite ejecutar el análisis de datos para un esquema de base de datos específico . (Según la documentación oficial El proceso de Analyze_management consiste en: “Actualiza las estadísticas de la base de datos de las tablas de negocios, las tablas de entidades y las tablas delta junto con las estadísticas de los índices de esas tablas.” )

Ver script en github:

#-*- coding: UTF-8 -*-
#Script para ejecutar el compress y analyze de la base de datos
#Basado en el Script original para arcgis 9.3.1 por Mike Long - ml56067@gmail.com
import string, os, arcpy, time
import traceback,sys
from arcpy import env
# Set the date.
Date = time.strftime("%m-%d-%Y", time.localtime())
# Set the time.
Time = time.strftime("%I:%M:%S %p", time.localtime())
print "Process started at " + str(Date) + " " + str(Time) + "." + "\n"
sdeConnFilePath = "C:\\temp\\my_sde_conn.sde"
analyzeworkspace = "C:\\temp\\my_schema.sde"
logFilePath = 'c:\\temp\\GIS-' + Date + '.txt'
# Set up the log file.
LogFile = file(logFilePath, 'w') #Creates a log file with todays date.
output = open(logFilePath, 'w') #Path to log file.
output.write(str("Process started at " + str(Date) + " " + str(Time) + "." + "\n")) # Write the start time to the log file.
try:
# Compress the database
print "Begining Compress..." + "\n"
output.write("Begining Compress..." + "\n")
#gp.toolbox = "management"
# For this script to work it will need the full path to the .sde connection file.
arcpy.Compress_management(sdeConnFilePath)
print arcpy.GetMessages() + "\n"
output.write(arcpy.GetMessages() + "\n")
except Exception as e:
print arcpy.GetMessages()
output.write("Error Compressing the GIS Database")
output.write(arcpy.GetMessages())
output.write(e)
output.write(e.args[0])
output.write(arcpy.GetMessages())
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
output.write( tbinfo )
output.write( sys.exc_type )
output.write( sys.exc_value )
try:
output.write("Begining analyze..." + "\n")
env.workspace = analyzeworkspace
# Loop through and analyze all the Tables.
# The if-else statements set the script to skip over Multi-Versioned Views.
#gp.Workspace = analyzeworkspace
output.write("\nAnalyzing tables\n")
for TB in arcpy.ListTables("*", "all"):
output.write("Analyzing table:"+TB)
if '_MVVIEW' in TB:
output.write("Skipping Multi-Versioned View")
elif '_MVView' in TB:
output.write("Skipping Multi-Versioned View")
elif 'SDE.' in TB:
output.write("\nSkipping table SDE\n")
else:
arcpy.Analyze_management(TB, "BUSINESS;FEATURE;ADDS;DELETES")
print arcpy.GetMessages() + "\n"
output.write(arcpy.GetMessages())
# Loop through and analyze all Feature datasets
output.write("\nAnalyzing featureClasses\n")
for FC in arcpy.ListFeatureClasses("*", "all"):
output.write("\nAnalyzing featureClass:"+FC)
if 'VV_' in FC:
print "Skipping View"
output.write("\nSkipping View\n")
continue
arcpy.Analyze_management(FC, "BUSINESS;FEATURE;ADDS;DELETES")
print arcpy.GetMessages() + "\n"
output.write(arcpy.GetMessages() + "\n")
# Loop through and analyze all the Feature classes
#gp.Workspace = analyzeworkspace
output.write("\nAnalyzing datasets\n")
for FD in arcpy.ListDatasets("*", "all"):
output.write("\nAnalyzing dataset:"+FD)
arcpy.Analyze_management(FD, "BUSINESS;FEATURE;ADDS;DELETES")
print arcpy.GetMessages() + "\n"
output.write(arcpy.GetMessages())
except Exception as e:
output.write("\nError...\n")
print arcpy.GetMessages()
output.write("Error Analyzing the GIS Database\n")
output.write(arcpy.GetMessages())
output.write(str(e))
output.write(e.args[0])
output.write(arcpy.GetMessages())
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
output.write( tbinfo )
output.write( str(sys.exc_type) )
output.write( str(sys.exc_value ))
# 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) + "."

Comentarios