Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. ### Copies, clips and creates symbology of all visibile layers in mxd's in the current directory. Also creates a text file with metadata for use.
  2. # Author: George Corea, Atherton Tablelands GIS
  3. # georgec@atgis.com.au; info@atgis.com.au
  4.  
  5. # Licence:Creative Commons
  6.  
  7. import arcpy, string, datetime, shutil, os, glob
  8. import arcpy.mapping as MAP
  9. from arcpy import env
  10.  
  11. arcpy.env.workspace = os.getcwd()
  12. arcpy.env.overwriteOutput = True
  13.  
  14. MXDList=string.split(arcpy.GetParameterAsText(0), ";")
  15. ProjectPath=arcpy.GetParameterAsText(1)
  16. CreateData=arcpy.GetParameterAsText(2)
  17. clip_features=arcpy.GetParameterAsText(3) # polygon to clip data to AOI
  18. AOI=arcpy.GetParameterAsText(4) #appended as suffix to dataset
  19. arcpy.env.outputCoordinateSystem=arcpy.GetParameterAsText(5) #to maintain all datasets in the same projection
  20. createdir=arcpy.GetParameterAsText(6)
  21.  
  22. rootPath=ProjectPath
  23.  
  24. #MXDList=glob.glob('*.mxd')
  25. #ProjectPath=r'P:2012183_TownPlanning_SymbologyWorking' # root output directory
  26. #clip_features=r'L:Vector_DataAdministrativeBoundariesLocal_GovtTRCtrc_boundary_Polygon.shp' # polygon to clip data to AOI
  27. #AOI='_trc' #appended as suffix to dataset
  28. #arcpy.env.outputCoordinateSystem=r'L:Vector_DataAdministrativeBoundariesLocal_GovtTRCtrc_boundary_Polygon.prj' #to maintain all datasets in the same projection
  29.  
  30. xy_tolerance=1 #clip tolerance
  31.  
  32. # No edits should be required below this line.
  33.  
  34. VisibleLyrList = []
  35. count=1
  36.  
  37.  
  38. def layer_details(outPath, outFileN, lyrFile, type): #Generates the metadata
  39.  
  40. descLayer = arcpy.Describe(lyrFile.dataSource)
  41. ReviewLog=outPath+'\'+type+'_'+outFileN+'_log.txt'
  42. f = open(ReviewLog, 'a')
  43. try:
  44. f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+
  45. '}; query{'+str(lyrFile.definitionQuery)+
  46. '}; source{'+str(lyrFile.dataSource)+
  47. '}; description{'+str(lyrFile.description)+
  48. '}; symbology{'+ str(lyrFile.symbologyType)+
  49. '}; original projection{'+str(descLayer.spatialReference.name)+
  50. '}; extent(x,y){'+str(descLayer.extent.XMax)+','+str(descLayer.extent.XMin)+','+str(descLayer.extent.YMax)+','+str(descLayer.extent.YMin)+
  51. '}; format{'+str(descLayer.shapeType)+
  52. '}; size(bytes) ~{'+str(os.path.getsize(lyrFile.dataSource))+
  53. '} @{'+str(datetime.datetime.now())+'}'
  54. )
  55. f.close()
  56. except:
  57. pass
  58.  
  59. print 'starting...'+str(MXDList)
  60.  
  61. for MXDFile in MXDList:
  62.  
  63. mxd=arcpy.mapping.MapDocument(MXDFile)
  64. if createdir=="false":
  65. outPath = ProjectPath
  66. else:
  67. outPath = ProjectPath+'\'+mxd.filePath[mxd.filePath.rfind('\')+1:mxd.filePath.rfind('.')]
  68.  
  69. arcpy.AddMessage ('Working on file #' + str(count) +' ...'+str(mxd.filePath))
  70. try:
  71. os.mkdir(outPath)
  72. except:
  73. pass
  74. dfList = arcpy.mapping.ListDataFrames(mxd)
  75. #print arcpy.GetMessages()
  76. #print 'working on 2...'+str(dfList)+str(outPath)
  77.  
  78. for df in dfList:
  79. #msd = outPath+'.msd'
  80. #arcpy.mapping.ConvertToMSD(mxd, msd, df, "NORMAL", "NORMAL")
  81. #print (str(count)+'...' +str(outPath)+'...'+'n')
  82. arcpy.AddMessage ('Working on dataframe ... ' +str(df.name))
  83. lyrList=arcpy.mapping.ListLayers(mxd, "", df)
  84. outPath = ProjectPath+'\'+mxd.filePath[mxd.filePath.rfind('\')+1:mxd.filePath.rfind('.')]+'\'+str(df.name)
  85. try:
  86. os.mkdir(outPath)
  87. except:
  88. pass
  89. print ('working on 3...in: '+str(outPath))
  90. for lyrFile in lyrList:
  91. #print (str(lyrFile))
  92. if lyrFile.isFeatureLayer == True:
  93. if lyrFile.visible == True:
  94. if lyrFile.name not in VisibleLyrList:
  95. VisibleLyrList.append(lyrFile.name)
  96. arcpy.AddMessage (str(lyrFile)+' is visible PROCESSING...')
  97. outFileN=str(arcpy.ValidateTableName(lyrFile.longName[lyrFile.longName.rfind('\')+1:]))
  98. try:
  99. rows = arcpy.SearchCursor(lyrFile.dataSource)
  100. row = rows.next()
  101. if row:
  102. if CreateData=="true":
  103. arcpy.FeatureClassToFeatureClass_conversion(lyrFile.dataSource, outPath, outFileN)
  104. #arcpy.Copy_management(lyrFile.dataSource, outPath+'//'+outFileN, "")
  105. arcpy.Clip_analysis(outPath+'\'+outFileN+'.shp', clip_features, outPath+'\'+outFileN+AOI+'.shp', xy_tolerance)
  106. #updateLayer = outPath+'\'+outFileN+'.lyr'
  107. sourceLayer = arcpy.mapping.Layer(outPath+'\'+outFileN+'.shp')
  108. sourceLayer_AOI = arcpy.mapping.Layer(outPath+'\'+outFileN+AOI+'.shp')
  109. #arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
  110. arcpy.SaveToLayerFile_management(lyrFile,outPath+'\'+outFileN+'_sym.lyr', "ABSOLUTE")
  111. arcpy.SaveToLayerFile_management(sourceLayer_AOI,outPath+'\'+outFileN+AOI+'.lyr', "ABSOLUTE")
  112. arcpy.ApplySymbologyFromLayer_management (outPath+'\'+outFileN+AOI+'.lyr', outPath+'\'+outFileN+'_sym.lyr')
  113. descLayer = arcpy.Describe(sourceLayer)
  114. #QA Row Count
  115. QA_rowCount_InFile = arcpy.GetCount_management(lyrFile)
  116. outLyrFile=arcpy.mapping.Layer(outPath+'\'+outFileN+AOI+'.shp')
  117. QA_rowCount_OutFile = arcpy.GetCount_management(outLyrFile)
  118. if int(str(QA_rowCount_InFile))==int(str(QA_rowCount_OutFile)):
  119. arcpy.AddMessage("Count same - QA should not be Required..."+str(QA_rowCount_InFile)+" Out="+str(QA_rowCount_OutFile))
  120. else:
  121. arcpy.AddMessage("Count NOT same - QA IS Required...In="+str(QA_rowCount_InFile)+" Out="+str(QA_rowCount_OutFile))
  122. layer_details(outPath, outFileN,lyrFile,"Row Count ISSUE --- Count NOT same - QA IS Required...In="+str(QA_rowCount_InFile)+" Out="+str(QA_rowCount_OutFile))
  123. IssueLog=rootPath+'\'+'Issue_log.txt'
  124. f = open(IssueLog, 'a')
  125. f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+ '}; source{'+str(lyrFile.dataSource)+'}'+'n')
  126. f.close()
  127. #QA Row Count process
  128. layer_details(outPath, outFileN, lyrFile,"COMPLETED")
  129. arcpy.Delete_management(outPath+'\'+outFileN+'_sym.lyr')
  130. arcpy.Delete_management(outPath+'\'+outFileN+'.shp')
  131. else:
  132. layer_details(outPath, outFileN, lyrFile,"SIMULATED")
  133. #arcpy.LayerToKML_conversion(outPath+'\'+outFileN+'.lyr', outPath+'\'+outFileN+'.kmz')
  134. else:
  135. arcpy.AddMessage ("!!!Datasource Issue!!!...continuing")
  136. layer_details(outPath, outFileN,lyrFile,"ISSUE")
  137. IssueLog=rootPath+'\'+'Issue_log.txt'
  138. f = open(IssueLog, 'a')
  139. f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+ '}; source{'+str(lyrFile.dataSource)+'}'+'n')
  140. f.close()
  141. except:
  142. errorm=arcpy.GetMessages()
  143. arcpy.AddMessage ('!!! ERROR !!!...'+str(errorm)+' ...continuing')
  144. try:
  145. layer_details(outPath, outFileN, lyrFile,"ERROR")
  146. except:
  147. pass
  148. #break
  149. ErrorLog=rootPath+'\'+'Error_log.txt'
  150. f = open(ErrorLog, 'a')
  151. f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+ '}; source{'+str(lyrFile.dataSource)+'}'+'n')
  152. f.close()
  153.  
  154.  
  155. else:
  156. pass
  157. else:
  158. arcpy.AddMessage (str(lyrFile)+' Is NOT Visible. Not Processing')
  159. else:
  160. arcpy.AddMessage (str(lyrFile)+' Is NOT Feature Layer. Not Processing')
  161. count=count+1
  162. #print str(lyrFile)+' is not visible'
  163.  
  164. #Remove variable reference to file
  165. del mxd, outPath, lyrFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement