Script & Tool之Compress & Analyze

  

Arcpy(arcgis10+python2.6) 改编

# This script is designed to compress an SDE Geodatabase and then
# loop through and analyze the statistics of each feature dataset, view,feature class and table.
# It is designed to log the entire process to a text file and
# if there are any errors during the process the script will email a specified address.
# Created by tony.gu - tony.gu@gisinfo.com
# Last Modified on 11/08/11

import arcpy, time, smtplib
from arcpy import env
compress_workspace=arcpy.GetParameterAsText(0)
analyze_workspace=arcpy.GetParameterAsText(1)

env.workspace=compress_workspace
Date = time.strftime("%m-%d-%Y", time.localtime())# Set the date.
Time = time.strftime("%I:%M:%S %p", time.localtime())# Set the time.

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

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

try:
    # Compress the database
    print "Begining Compress..." + "\n"
    arcpy.Compress_management(compress_workspace)
    print arcpy.GetMessages() + "\n"
    output.write(arcpy.GetMessages()+ "\n")
except:
    #Sets up the email parameters
    From = "Geoprocessor"
    To = ["tony.gu@gisinfo.com"]
    Date = time.ctime(time.time())
    Subject = "Error Compressing the GIS Database"
    Text = arcpy.GetMessages()

    #Format mail message
    mMessage = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n%s\n' %
                (From, To, Date, Subject, Text))
    print 'Connecting to Server'
    s = smtplib.SMTP('smtp.ym.163.com') # This needs to be set to your email server.

    #Send Email
    rCode = s.sendmail(From, To, mMessage)
    s.quit()

    if rCode:
        print 'Error Sending Message'
    else:
        print 'Message sent sucessfully'

env.workspace=analyze_workspace
try:
    # Loop through and analyze all Feature datasets
    FCList = arcpy.ListFeatureClasses ("*", "all")
    for FC in FCList:
        arcpy.Analyze_management(FC, "BUSINESS;FEATURE;ADDS;DELETES")
        print arcpy.GetMessages() + "\n"
        output.write(arcpy.GetMessages()+ "\n")

       
    DSList = arcpy.ListDatasets ("*", "all")
    for DS in DSList:
        arcpy.Analyze_management(DS, "BUSINESS;FEATURE;ADDS;DELETES")
        print arcpy.GetMessages() + "\n"
        output.write(arcpy.GetMessages())

       
    # Loop through and analyze all the Tables.
    # The if-else statements set the script to skip over Multi-Versioned Views.
   
    TBList = arcpy.ListTables ("*", "all")
    for TB in TBList:
        if '_MVVIEW' in TB:
            print "Skipping Multi-Versioned View"
        elif '_MVView' in TB:
            print "Skipping Multi-Versioned View"
        else:
            arcpy.Analyze_management(TB, "BUSINESS;FEATURE;ADDS;DELETES")
            print arcpy.GetMessages() + "\n"
            output.write(arcpy.GetMessages())

except:
    print arcpy.GetMessages()
    output.write(arcpy.GetMessages())

    #Sets up the email parameters

    From = "Geoprocessor"
    To = ["tony.gu@gisinfo.com"]
    Date = time.ctime(time.time())
    Subject = "Error Analyzing the GIS Database"
    Text = arcpy.GetMessages()

    #Format mail message
    mMessage = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n%s\n' %
                (From, To, Date, Subject, Text))
    print 'Connecting to Server'
    s = smtplib.SMTP('smtp.ym.163.com')

    #Send Email
    rCode = s.sendmail(From, To, mMessage)
    s.quit()

    if rCode:
        print 'Error Sending Message'
    else:
        print 'Message sent sucessfully'

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

转载自:https://blog.csdn.net/gisinfo/article/details/6688257

You may also like...

退出移动版