Guest User

Untitled

a guest
Nov 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. import arcpy, os, shutil, time
  2. from sannetlogger import SannetLogger
  3. from datetime import datetime
  4.  
  5.  
  6. def formatTime(x):
  7. """ formats timestamp as readable format"""
  8. minutes, seconds_rem = divmod(x, 60)
  9. if minutes >= 60:
  10. hours, minutes_rem = divmod(minutes, 60)
  11. return "%02d:%02d:%02d" % (hours, minutes_rem, seconds_rem)
  12. else:
  13. minutes, seconds_rem = divmod(x, 60)
  14. return "00:%02d:%02d" % (minutes, seconds_rem)
  15.  
  16. def getDatabaseItemCount(workspace):
  17. """ gets a count of feature classes a prespecified geodatabase / workspace """
  18. arcpy.env.workspace = workspace
  19. feature_classes = []
  20. for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype="Any",type="Any"):
  21. for filename in filenames:
  22. feature_classes.append(os.path.join(dirpath, filename))
  23. return feature_classes, len(feature_classes)
  24.  
  25. def copyData(targetName, targetPath):
  26. try:
  27. print "Atempting to Copy %s to %s" %(targetName, targetPath)
  28. log.info("Atempting to Copy %s to %s" %(targetName, targetPath))
  29. #arcpy.Copy_management(sourcePath, targetPath)
  30. print "Finished copying %s to %s" %(targetName, targetPath)
  31. log.info("Finished copying %s to %s" %(targetName, targetPath))
  32. except Exception as e:
  33. print "Unable to copy %s to %s" %(targetName, targetPath)
  34. print e
  35. log.info("Unable to copy %s to %s" %(targetName, targetPath))
  36. log.info(e)
  37.  
  38. def schemaDiff(source, target):
  39. log.info("Comparing %s to %s" %(sourcePath, target))
  40. result = arcpy.FeatureCompare_management(sourcePath, target, '', 'SCHEMA_ONLY')
  41. changes = result.getOutput(1)
  42. change = str(changes)
  43. if change == "true":
  44. return True
  45. if change == "false":
  46. return False
  47. else:
  48. raise Exception("Could determine comparison value between input and target")
  49.  
  50. def attributeDiff(source, target):
  51. log.info("Comparing %s to %s" %(sourcePath, target))
  52. 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))
  53. changes = result.getOutput(1)
  54. change = str(changes)
  55. if change == "true":
  56. return True
  57. if change == "false":
  58. return False
  59. else:
  60. raise Exception("Could determine comparison value between input and target")
  61.  
  62. def replicateDatabase(dbConnection, targetGDB, truncAppendIfExists = True):
  63. """ function to replicate featureclass objects from one workspace to another """
  64.  
  65. startTime = time.time()
  66.  
  67. featSDE,cntSDE = getDatabaseItemCount(dbConnection)
  68. featGDB,cntGDB = getDatabaseItemCount(targetGDB)
  69.  
  70. print "Source Geodatabase: %s -- Feature Count: %s" %(dbConnection, cntSDE)
  71. log.info("Geodatabase being copied: %s -- Feature Count: %s" %(dbConnection, cntSDE))
  72. print "Target Geodatabase: %s -- Feature Count: %s" %(targetGDB, cntGDB)
  73. log.info("Old Target Geodatabase: %s -- Feature Count: %s" %(targetGDB, cntGDB))
  74.  
  75. arcpy.env.workspace = dbConnection
  76.  
  77. datasetList = [arcpy.Describe(a).name for a in arcpy.ListDatasets()]
  78. featureClasses = [arcpy.Describe(a).name for a in arcpy.ListFeatureClasses()]
  79. tables = [arcpy.Describe(a).name for a in arcpy.ListTables()]
  80.  
  81. #Compiles a list of the previous three lists to iterate over
  82. allDbData = datasetList + featureClasses + tables
  83.  
  84. for sourcePath in allDbData:
  85. targetName = sourcePath.split('.')[-1]
  86. targetPath = targetGDB + '\\' + targetName
  87. print targetPath
  88. if arcpy.Exists(targetPath)==False:
  89. copyData(targetName, targetPath)
  90. elif truncAppendIfExists:
  91. try:
  92. schemaDiff = schemaDiff(sourcePath, targetPath)
  93. if (schemaDiff):
  94. log.info("Schema difference detected for %s" %(targetName, targetPath))
  95. log.info("deleting %s" %(targetName, targetPath))
  96. #arcpy.Delete_management(targetPath)
  97. copyData(targetName, targetPath)
  98. break
  99.  
  100. attrDiff = attributeDiff(sourcePath, targetPath)
  101. if (attrDiff):
  102. log.info("Atempting to Truncate %s at %s" %(targetName, targetPath))
  103. #arcpy.TruncateTable_management(targetPath + "\\" + targetName)
  104. log.info("Atempting to append %s at %s" %(targetName, targetPath))
  105. #Append_management(sourcePath, target, "NO TEST")
  106. log.info("%s append complete" %(targetName))
  107. else:
  108. log.info("truncate append skipped for %s - no changes detected" %(targetName))
  109.  
  110. except Exception as e:
  111. print "Unable to copy %s to %s" %(targetName, targetPath)
  112. print e
  113. log.info("Unable to copy %s to %s" %(targetName, targetPath))
  114. log.info(e)
  115. else:
  116. print "%s already exists....skipping....." %(targetName)
  117. log.info("%s already exists....skipping....." %(targetName))
  118.  
  119. featGDB,cntGDB = getDatabaseItemCount(targetGDB)
  120. print "Completed replication of %s -- Feature Count: %s" %(dbConnection, cntGDB)
  121. log.info("Completed replication of %s -- Feature Count: %s" %(dbConnection, cntGDB))
  122. totalTime = (time.time() - startTime)
  123. totalTime = formatTime(totalTime)
  124. log.info("Script Run Time: %s" %(totalTime))
  125.  
  126. if __name__== "__main__":
  127. now = datetime.now()
  128. logName = now.strftime("SDE_REPLICATE_SCRIPT_%Y-%m-%d_%H-%M-%S.log")
  129. log = SannetLogger(name=logName, print_to_console=True)
  130. targetGDB = r"PATH_TO_SDE_1.sde"
  131. databaseConnection = r"PATH_TO_SDE_2.sde"
  132. replicateDatabase(databaseConnection, targetGDB, True)
Add Comment
Please, Sign In to add comment