Advertisement
mapping-glory

Parcel Percent in Categories

Feb 15th, 2016
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.91 KB | None | 0 0
  1. #intro
  2. import arcpy, time, sys, string, os, traceback
  3. from arcpy import env
  4.  
  5. #Start time and log.
  6. StartTime = time.time()
  7. d = datetime.datetime.now()
  8. log = open(r"X:\Files\FloodParcelLogFile.txt","a")
  9. log.write("----------------------------" + "\n")
  10. log.write("----------------------------" + "\n")
  11. log.write("Log: " + str(d) + "\n")
  12. print ("Log: " + str(d))
  13. log.write("\n")
  14. log.write("Begin process:\n")
  15. log.write("Process started at " + str(StartTime) + "\n")
  16. log.write("\n")
  17.  
  18. try:
  19.  
  20.     #set workspace
  21.     env.workspace = r"Database Connections\SQL_Database.sde"
  22.     arcpy.env.overwriteOutput = True
  23.  
  24.     #check if # of records in parcels matches FloodParcel
  25.     #if true, end script
  26.     result = arcpy.GetCount_management("Dataset\Parcel_Feature_Class")
  27.     ParCount = int(result.getOutput(0))
  28.     result = arcpy.GetCount_management("Output_Feature_Class")
  29.     FloodCount = int(result.getOutput(0))
  30.     log.write("{0} records in Parcels and {1} records in Flood Parcels.\n".format(ParCount, FloodCount))
  31.     if int(ParCount) == int(FloodCount):
  32.         log.write("No new parcels. Terminating script.\n")
  33.         log.write("\n")
  34.         sys.exit()
  35.     else:
  36.         log.write("New parcels detected. Script proceeding.\n")
  37.  
  38.  
  39.  
  40.     #delete previous FloodParcels
  41.     #arcpy.DeleteFeatures_management("Output_Feature_Class")
  42.     arcpy.CopyFeatures_management("LocalGovernment.DBO.Parcels\LocalGovernment.DBO.Abilene_Parcels", "LocalGovernment.DBO.Flood\FloodParcels")
  43.  
  44.     #set new workspace
  45.     env.workspace = r"Database Connections\SQL_Database.sde\Dataset"
  46.     arcpy.env.overwriteOutput = True
  47.  
  48.     arcpy.MakeFeatureLayer_management("FloodParcels","FloodParcels_lyr")
  49.  
  50.     #add percent fields to FloodParcels
  51.     FieldList = ('PERCFW', 'PERC100', 'PERC500', 'PERCNOT')
  52.     for entry in FieldList:
  53.         arcpy.AddField_management("FloodParcels_lyr", entry, "FLOAT")
  54.         arcpy.CalculateField_management("FloodParcels_lyr", entry, "0", "PYTHON")
  55.         arcpy.AssignDefaultToField_management("FloodParcels_lyr", entry, 0)
  56.         arcpy.AlterField_management("FloodParcels_lyr", entry, "","","","","NON_NULLABLE")
  57.         log.write("Field {0} has been conjured.\n".format(entry))
  58.  
  59.  
  60.  
  61.     RunTime = time.time() - StartTime
  62.     log.write("Flood parcel layer conjured and defined. {0} seconds elapsed in script.\n".format(RunTime))
  63.     FWList = ("FW", "FW_lyr", "INT_FW", "INT_FW_lyr", "DISS_FW", "DISS_FW_lyr", "AREAFW", "PERCFW")
  64.     Y100List = ("Y100", "Y100_lyr", "INT_Y100", "INT_Y100_lyr", "DISS_Y100", "DISS_Y100_lyr", "AREAY100", "PERC100")
  65.     Y500List = ("Y500", "Y500_lyr", "INT_Y500", "INT_Y500_lyr", "DISS_Y500", "DISS_Y500_lyr", "AREAY500", "PERC500")
  66.     #Key:
  67.     # 0: Filename
  68.     # 1: Vector layer
  69.     # 2: Intersect file
  70.     # 3: Intersect layer
  71.     # 4: Dissolve file
  72.     # 5: Dissolve layer
  73.     # 6: Area field
  74.     # 7: Percent field
  75.  
  76.  
  77.  
  78.     #Define FloodList as list of lists.
  79.     FloodList = (FWList, Y100List, Y500List)
  80.  
  81.     #Begin cycling through FloodList
  82.     for FloodBit in FloodList:
  83.         print "{0} is being processed...".format(FloodBit[0])
  84.         log.write("{0} is being processed...\n".format(FloodBit[0]))
  85.         LoopSTime = time.time()
  86.         arcpy.CalculateField_management("FloodParcels_lyr", FloodBit[7], "0", "PYTHON")
  87.         #arcpy.AddMessage("{0} is being processed...".format(FloodBit[0]))
  88.         #Create arguments and layers
  89.         arcpy.MakeFeatureLayer_management(FloodBit[0], FloodBit[1])
  90.         ElTime = (time.time() - LoopSTime)
  91.         print "{0} has been conjured. {1} seconds elapsed in this loop.".format(FloodBit[1], ElTime)
  92.         log.write("{0} has been conjured. {1} seconds elapsed in this loop.\n".format(FloodBit[1], ElTime))
  93.         IntList = ("FloodParcels_lyr", FloodBit[1])
  94.         #Intersect to FloodBit[2]
  95.         #Intersect_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {output_type})
  96.         arcpy.Intersect_analysis(IntList, FloodBit[2], "ALL")
  97.         ElTime = (time.time() - LoopSTime)
  98.         print "{0} has been intersected. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
  99.         log.write("{0} has been intersected. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
  100.         #Dissolve to FloodBit[4]
  101.         arcpy.Dissolve_management(FloodBit[2], FloodBit[4], "FID_FloodParcels")
  102.         ElTime = (time.time() - LoopSTime)
  103.         print "{0} has been dissolved. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
  104.         log.write("{0} has been dissolved. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
  105.         #Conjure layer FloodBit[5]
  106.         arcpy.MakeFeatureLayer_management(FloodBit[4], FloodBit[5])
  107.         ElTime = (time.time() - LoopSTime)
  108.         print "{0} has been conjured. {1} seconds elapsed in this loop.".format(FloodBit[5], ElTime)
  109.         log.write("{0} has been conjured. {1} seconds elapsed in this loop.\n".format(FloodBit[5], ElTime))
  110.         #Add field FloodBit[6] to FloodBit[4]
  111.         arcpy.AddField_management(FloodBit[5], FloodBit[6], "FLOAT")
  112.         arcpy.AssignDefaultToField_management(FloodBit[5], FloodBit[6], 0)
  113.         ElTime = (time.time() - LoopSTime)
  114.         print "{0} has been added to {1}. {2} seconds elapsed in this loop.".format(FloodBit[6], FloodBit[5], ElTime)
  115.         log.write("{0} has been added to {1}. {2} seconds elapsed in this loop.\n".format(FloodBit[6], FloodBit[5], ElTime))
  116.         arcpy.Delete_management(FloodBit[1])
  117.         print "{0} has been destroyed.".format(FloodBit[1])
  118.         arcpy.Delete_management(FloodBit[3])
  119.         print "{0} has been destroyed.".format(FloodBit[3])
  120.         log.write("{0} and {1} have been destroyed.\n".format(FloodBit[1], FloodBit[3]))
  121.  
  122.         #Calculate field FloodBit[6]
  123.         print "Calculating field {0}...".format(FloodBit[6])
  124.         log.write("Calculating field {0}...\n")
  125.         arcpy.CalculateField_management(FloodBit[5], FloodBit[6], "!shape.area@feet!", "PYTHON")
  126.         ElTime = (time.time() - LoopSTime)
  127.         print "{0} has been calculated. {1} seconds elapsed in this loop.".format(FloodBit[6], ElTime)
  128.         log.write("{0} has been calculated. {1} seconds elapsed in this loop.\n".format(FloodBit[6], ElTime))
  129.         #add field attribute index to FloodBit5
  130.         IndexFields = str("FID_ParcelSet;" + FloodBit[6])
  131.         arcpy.AddIndex_management(FloodBit[5], IndexFields, "DissIndex")
  132.         print "{0} has been indexed.".format(FloodBit[5])
  133.         log.write("{0} has been indexed.\n".format(FloodBit[5]))
  134.         #Join field FloodBit[6] to FloodParcel
  135.         arcpy.JoinField_management("FloodParcels_lyr", "OBJECTID", FloodBit[5], "FID_FloodParcels", FloodBit[6])
  136.         ElTime = (time.time() - LoopSTime)
  137.         print "{0} has been joined to the parcels. {1} seconds elapsed in this loop.".format(FloodBit[6], ElTime)
  138.         log.write("{0} has been joined to the parcels. {1} seconds elapsed in this loop.\n".format(FloodBit[6], ElTime))
  139.         #Build field calculator expression
  140.         CalcExp = "(!" + FloodBit[6] + r"!/!Shape.area!)*100"
  141.         #Calculate field FloodBit[7] = FloodBit[6] / Shape_Area
  142.         print "Parcel area calculating on {0}...".format(CalcExp)
  143.         log.write("Parcel area calculating on {0}...\n".format(CalcExp))
  144.         arcpy.CalculateField_management("FloodParcels_lyr", FloodBit[7], CalcExp, "PYTHON")
  145.         ElTime = (time.time() - LoopSTime)
  146.         print "Parcel area calculated for {0}. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
  147.         log.write("Parcel area calculated for {0}. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
  148.         #Delete field FloodBit[6] from FloodParcels
  149.         arcpy.DeleteField_management("FloodParcels_lyr", FloodBit[6])
  150.         print "{0} has been destroyed.".format(FloodBit[6])
  151.         log.write("{0} has been destroyed.\n".format(FloodBit[6]))
  152.         #Delete 2-3-4-5?
  153.         PurgeList = (FloodBit[5], FloodBit[2], FloodBit[4])
  154.         for item in PurgeList:
  155.             arcpy.Delete_management(item)
  156.             log.write("{0} has been destroyed.\n".format(item))
  157.  
  158.         #Replace NULL values with 0.
  159.         with arcpy.da.UpdateCursor("FloodParcels", [FloodBit[7]]) as cursor:
  160.             for row in cursor:
  161.                 if row[0] == None:
  162.                     row[0] = 0
  163.                     cursor.updateRow(row)
  164.                 elif row[0] < 0:
  165.                     row[0] = 0
  166.                 elif row[0] > 100:
  167.                     row[0] = 100
  168.  
  169.         #End loop
  170.         ElTime = (time.time() - LoopSTime)
  171.         print "{0} has been processed. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
  172.         log.write("{0} has been processed. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
  173.         log.write("\n")
  174.  
  175.     #cleanup
  176.     #Calculate PERCNOT
  177.     CalcTime = time.time()
  178.     print "Calculating area not in flood zone..."
  179.     log.write("Calculating area outside flood zones...\n")
  180.     CalcExp = "100 - (!PERCFW! + !PERC100! + !PERC500!)"
  181.     arcpy.CalculateField_management("FloodParcels_lyr", "PERCNOT", CalcExp, "PYTHON")
  182.     ElTime = (time.time() - CalcTime)
  183.     print "Calculation completed. {0} seconds elapsed.".format(ElTime)
  184.     log.write("Calculation completed. {0} seconds elapsed.\n".format(ElTime))
  185.  
  186.  
  187.     #Overwrite service
  188.  
  189.     RunTime = time.time() - StartTime
  190.     print "Script completed. {0} minutes elapsed.".format((RunTime/60))
  191.     log.write("Script completed. {0} minutes elapsed.\n".format((RunTime/60)))
  192.     log.close()
  193.  
  194. except:
  195.     #Get traceback object
  196.     tb = sys.exc_info()[2]
  197.     tbinfo = traceback.format_tb(tb)[0]
  198.     pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
  199.     msgs = "ArcPy Errors:\n" + arcpy.GetMessages(2) + "\n"
  200.     arcpy.AddError(pymsg)
  201.     arcpy.AddError(msgs)
  202.     #print error messages and close log
  203.     log.write("" + pymsg + "\n")
  204.     log.write("" + msgs + "" + "\n")
  205.     log.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement