Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcpy, os, shutil, time
- from sannetlogger import SannetLogger
- from datetime import datetime
- def formatTime(x):
- """ formats timestamp as readable format"""
- minutes, seconds_rem = divmod(x, 60)
- if minutes >= 60:
- hours, minutes_rem = divmod(minutes, 60)
- return "%02d:%02d:%02d" % (hours, minutes_rem, seconds_rem)
- else:
- minutes, seconds_rem = divmod(x, 60)
- return "00:%02d:%02d" % (minutes, seconds_rem)
- def getDatabaseItemCount(workspace):
- """ gets a count of feature classes a prespecified geodatabase / workspace """
- arcpy.env.workspace = workspace
- feature_classes = []
- for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype="Any",type="Any"):
- for filename in filenames:
- feature_classes.append(os.path.join(dirpath, filename))
- return feature_classes, len(feature_classes)
- def copyData(targetName, targetPath):
- try:
- print "Atempting to Copy %s to %s" %(targetName, targetPath)
- log.info("Atempting to Copy %s to %s" %(targetName, targetPath))
- #arcpy.Copy_management(sourcePath, targetPath)
- print "Finished copying %s to %s" %(targetName, targetPath)
- log.info("Finished copying %s to %s" %(targetName, targetPath))
- except Exception as e:
- print "Unable to copy %s to %s" %(targetName, targetPath)
- print e
- log.info("Unable to copy %s to %s" %(targetName, targetPath))
- log.info(e)
- def schemaDiff(source, target):
- log.info("Comparing %s to %s" %(sourcePath, target))
- result = arcpy.FeatureCompare_management(sourcePath, target, '', 'SCHEMA_ONLY')
- changes = result.getOutput(1)
- change = str(changes)
- if change == "true":
- return True
- if change == "false":
- return False
- else:
- raise Exception("Could determine comparison value between input and target")
- def attributeDiff(source, target):
- log.info("Comparing %s to %s" %(sourcePath, target))
- result = arcpy.FeatureCompare_management(sourcePath, target, '', 'ALL', 'IGNORE_M;IGNORE_Z', '0.001 METERS', 0, 0, 'Shape_Length 0.001', '#', 'NO_CONTINUE_COMPARE', r'..\%s_compare.txt' %(targetName))
- changes = result.getOutput(1)
- change = str(changes)
- if change == "true":
- return True
- if change == "false":
- return False
- else:
- raise Exception("Could determine comparison value between input and target")
- def replicateDatabase(dbConnection, targetGDB, truncAppendIfExists = True):
- """ function to replicate featureclass objects from one workspace to another """
- startTime = time.time()
- featSDE,cntSDE = getDatabaseItemCount(dbConnection)
- featGDB,cntGDB = getDatabaseItemCount(targetGDB)
- print "Source Geodatabase: %s -- Feature Count: %s" %(dbConnection, cntSDE)
- log.info("Geodatabase being copied: %s -- Feature Count: %s" %(dbConnection, cntSDE))
- print "Target Geodatabase: %s -- Feature Count: %s" %(targetGDB, cntGDB)
- log.info("Old Target Geodatabase: %s -- Feature Count: %s" %(targetGDB, cntGDB))
- arcpy.env.workspace = dbConnection
- datasetList = [arcpy.Describe(a).name for a in arcpy.ListDatasets()]
- featureClasses = [arcpy.Describe(a).name for a in arcpy.ListFeatureClasses()]
- tables = [arcpy.Describe(a).name for a in arcpy.ListTables()]
- #Compiles a list of the previous three lists to iterate over
- allDbData = datasetList + featureClasses + tables
- for sourcePath in allDbData:
- targetName = sourcePath.split('.')[-1]
- targetPath = targetGDB + '\\' + targetName
- print targetPath
- if arcpy.Exists(targetPath)==False:
- copyData(targetName, targetPath)
- elif truncAppendIfExists:
- try:
- schemaDiff = schemaDiff(sourcePath, targetPath)
- if (schemaDiff):
- log.info("Schema difference detected for %s" %(targetName, targetPath))
- log.info("deleting %s" %(targetName, targetPath))
- #arcpy.Delete_management(targetPath)
- copyData(targetName, targetPath)
- break
- attrDiff = attributeDiff(sourcePath, targetPath)
- if (attrDiff):
- log.info("Atempting to Truncate %s at %s" %(targetName, targetPath))
- #arcpy.TruncateTable_management(targetPath + "\\" + targetName)
- log.info("Atempting to append %s at %s" %(targetName, targetPath))
- #Append_management(sourcePath, target, "NO TEST")
- log.info("%s append complete" %(targetName))
- else:
- log.info("truncate append skipped for %s - no changes detected" %(targetName))
- except Exception as e:
- print "Unable to copy %s to %s" %(targetName, targetPath)
- print e
- log.info("Unable to copy %s to %s" %(targetName, targetPath))
- log.info(e)
- else:
- print "%s already exists....skipping....." %(targetName)
- log.info("%s already exists....skipping....." %(targetName))
- featGDB,cntGDB = getDatabaseItemCount(targetGDB)
- print "Completed replication of %s -- Feature Count: %s" %(dbConnection, cntGDB)
- log.info("Completed replication of %s -- Feature Count: %s" %(dbConnection, cntGDB))
- totalTime = (time.time() - startTime)
- totalTime = formatTime(totalTime)
- log.info("Script Run Time: %s" %(totalTime))
- if __name__== "__main__":
- now = datetime.now()
- logName = now.strftime("SDE_REPLICATE_SCRIPT_%Y-%m-%d_%H-%M-%S.log")
- log = SannetLogger(name=logName, print_to_console=True)
- targetGDB = r"PATH_TO_SDE_1.sde"
- databaseConnection = r"PATH_TO_SDE_2.sde"
- replicateDatabase(databaseConnection, targetGDB, True)
Add Comment
Please, Sign In to add comment