Guest User

Untitled

a guest
Jul 1st, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. # import arcpy module
  2. import arcpy
  3.  
  4. # Ask user to select the Geodatabase workspace to use for data output
  5. userWorkspace = arcpy.GetParameterAsText(0)
  6.  
  7. # Set workspace environment based upon user's choice
  8. arcpy.env.workspace = userWorkspace
  9.  
  10. # Ask user to select an Oracle dB Connection
  11. oracleDB = arcpy.GetParameterAsText(1)
  12.  
  13. # Ask user to name the Query Layer
  14. queryLayer = arcpy.GetParameterAsText(2)
  15.  
  16. # Ask user if they want to overwrite previously named Query Layer
  17. overwriteQ = arcpy.GetParameterAsText(3)
  18.  
  19. # Ask user for an SQL Query Expression to be run against the selected Oracle dB
  20. sqlQuery = arcpy.GetParameterAsText(4)
  21.  
  22. # Ask user to name the output of excel file (this is optional, for those who want to do excel analysis)
  23. excelName = arcpy.GetParameterAsText(5)
  24.  
  25. # Ask user if they want to overwrite previously named Excel File
  26. overwriteE = arcpy.GetParameterAsText(6)
  27.  
  28. # Ask user to select the folder output of the excel file (optional as well, depending on if user wants an excel file)
  29. excelFolder = arcpy.GetParameterAsText(7)
  30.  
  31. # Create spatial reference variable to assign to queryLayer
  32. spatialRef = arcpy.SpatialReference("W:Coordinate SystemsLRS Lambert.prj")
  33.  
  34. # Process: 'Make Query Layer' - Creates a Query Layer using the user's Oracle dB and SQL query expression
  35. arcpy.MakeQueryLayer_management(oracleDB, "Temp_Layer", sqlQuery, "UNIQUE_ID", "POINT", "1050010", spatialRef)
  36.  
  37. # Set overwrite output for Query Layer to user's choice
  38. arcpy.env.overwriteOutput = overwriteQ
  39.  
  40. # Process: 'Copy Features' - Copies the temporary Query Layer and stores it as a permanent feature class
  41. arcpy.CopyFeatures_management("Temp_Layer", queryLayer)
  42.  
  43. # Process: 'Delete Features' - Deletes the temporary file Temp_Layer
  44. # This allows for multiple executions of this tool within the same ArcMap session without error
  45. arcpy.Delete_management("Temp_Layer")
  46.  
  47. # Process: 'Define Projection' - Defines the projection of queryLayer feature class output
  48. arcpy.DefineProjection_management(queryLayer, spatialRef)
  49.  
  50. # Process: 'Add Field' - Adds new column fields to queryLayer
  51. arcpy.AddField_management(queryLayer, "First_Time", "DATE") # The first LOGDT ping
  52. arcpy.AddField_management(queryLayer, "Last_Time", "DATE") # The last LOGDT ping
  53. arcpy.AddField_management(queryLayer, "Total_Time", "STRING") # Summation of the first to last ping in time
  54. arcpy.AddField_management(queryLayer, "Total_Pings", "INTEGER") # Total number of pings (rows)
  55. arcpy.AddField_management(queryLayer, "Possible_Pings", "INTEGER") # Total number of pings possible in given timeframe
  56. arcpy.AddField_management(queryLayer, "Time_to_Process", "DATE") # How long it took for each ping to process
  57.  
  58. # Calculates the total number of rows (pings) for the Total_Pings field
  59. numRows = int(arcpy.GetCount_management(queryLayer).getOutput(0))
  60.  
  61. # UpdateCursor that will write the value of numRows to the Total_Pings field
  62. cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Pings")
  63. for row in cursor:
  64. row[0] = numRows
  65. cursor.updateRow(row)
  66.  
  67. # SearchCursor that will read the values of LOGDT and return the first value (earliest)
  68. try: # try to read in values based on the user's SQL query
  69. firstLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][0]
  70. except: # exception error if the user's SQL query produces no return
  71. err = "No Results Found Via SQL QuerynSCRIPT EXITED" # Error message variable
  72. arcpy.AddError(err) # Print error message
  73. sys.exit() # Exits the script, does not attempt to run any further
  74.  
  75. # UpdateCursor that will write the first (earliest) LOGDT value to the First_Time field
  76. cursor = arcpy.da.UpdateCursor(queryLayer, "First_Time")
  77. for row in cursor:
  78. row[0] = firstLogdt
  79. cursor.updateRow(row)
  80.  
  81. # SearchCursor that will read the values of LOGDT and return the last value (latest)
  82. lastLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][-1]
  83.  
  84. # UpdateCursor that will write the last (latest) LOGDT value to the Last_Time field
  85. cursor = arcpy.da.UpdateCursor(queryLayer, "Last_Time")
  86. for row in cursor:
  87. row[0] = lastLogdt
  88. cursor.updateRow(row)
  89.  
  90. # Calculates the difference between firstLogdt and lastLogdt
  91. timeDiff = lastLogdt - firstLogdt # Produces a timedelta object, not datetime
  92.  
  93. # Calculates the total number of seconds from timeDiff
  94. timeSecs = timeDiff.total_seconds()
  95.  
  96. # Creates a function that will convert timeSecs to a readable format (yy:dd:hh:mm:ss)
  97. def readTime(seconds):
  98. minutes, seconds = divmod(seconds, 60)
  99. hours, minutes = divmod(minutes, 60)
  100. days, hours = divmod(hours, 24)
  101. years, days = divmod(days, 365)
  102. return '%02d:%02d:%02d:%02d:%02d' % (years, days, hours, minutes, seconds)
  103.  
  104. # UpdateCursor that will write the time difference calculation to the Total_Time field
  105. cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Time")
  106. for row in cursor:
  107. row[0] = readTime(timeSecs)
  108. cursor.updateRow(row)
  109.  
  110. # Calculates the total number of pings that would occur with optimal reception
  111. possiblePings = timeSecs / 5 # 1 ping every 5 seconds
  112.  
  113. # UpdateCursor that will write the total number of pings possible to the Possible_Pings field
  114. cursor = arcpy.da.UpdateCursor(queryLayer, "Possible_Pings")
  115. for row in cursor:
  116. row[0] = possiblePings
  117. cursor.updateRow(row)
  118.  
  119. # Using ArcPy Mapping Module to add queryLayer to current .mxd map display and zoom to it
  120. mxd = arcpy.mapping.MapDocument("CURRENT") # Set variable to currently active .mxd
  121. dataFrame = arcpy.mapping.ListDataFrames(mxd)[0] # Set variable equal to the first data frame within mxd
  122. addLayer = arcpy.mapping.Layer(queryLayer) # Set variable for queryLayer
  123. arcpy.mapping.AddLayer(dataFrame, addLayer) # Adds queryLayer to the map
  124. dataFrame.zoomToSelectedFeatures() # Zooms to queryLayer
  125. arcpy.RefreshActiveView() # Refreshes the active data frame's view
  126.  
  127. # Set overwrite output for Excel File to user's choice
  128. arcpy.env.overwriteOutput = overwriteE
  129.  
  130. # Change workspace environment to where the user wants the Excel file to be saved
  131. try: # Tries to change the environment workspace to the user's optional excel output folder
  132. arcpy.env.workspace = excelFolder
  133. except:
  134. pass # If user did not specify excel folder input, then let pass and continue on
  135.  
  136. # Process: 'Table To Excel' - Converts the final queried data to an excel spreadsheet file (optional)
  137. try: # Tries to run the tool (will only work if user specified optional excel inputs)
  138. arcpy.TableToExcel_conversion(queryLayer, excelName+'.xls')
  139. except:
  140. pass # If user did not specify optional excel inputs, then let pass and continue on
  141.  
  142. # delete cursor, row variables
  143. del cursor, row
Add Comment
Please, Sign In to add comment