Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #intro
- import arcpy, time, sys, string, os, traceback
- from arcpy import env
- #Start time and log.
- StartTime = time.time()
- d = datetime.datetime.now()
- log = open(r"X:\Files\FloodParcelLogFile.txt","a")
- log.write("----------------------------" + "\n")
- log.write("----------------------------" + "\n")
- log.write("Log: " + str(d) + "\n")
- print ("Log: " + str(d))
- log.write("\n")
- log.write("Begin process:\n")
- log.write("Process started at " + str(StartTime) + "\n")
- log.write("\n")
- try:
- #set workspace
- env.workspace = r"Database Connections\SQL_Database.sde"
- arcpy.env.overwriteOutput = True
- #check if # of records in parcels matches FloodParcel
- #if true, end script
- result = arcpy.GetCount_management("Dataset\Parcel_Feature_Class")
- ParCount = int(result.getOutput(0))
- result = arcpy.GetCount_management("Output_Feature_Class")
- FloodCount = int(result.getOutput(0))
- log.write("{0} records in Parcels and {1} records in Flood Parcels.\n".format(ParCount, FloodCount))
- if int(ParCount) == int(FloodCount):
- log.write("No new parcels. Terminating script.\n")
- log.write("\n")
- sys.exit()
- else:
- log.write("New parcels detected. Script proceeding.\n")
- #delete previous FloodParcels
- #arcpy.DeleteFeatures_management("Output_Feature_Class")
- arcpy.CopyFeatures_management("LocalGovernment.DBO.Parcels\LocalGovernment.DBO.Abilene_Parcels", "LocalGovernment.DBO.Flood\FloodParcels")
- #set new workspace
- env.workspace = r"Database Connections\SQL_Database.sde\Dataset"
- arcpy.env.overwriteOutput = True
- arcpy.MakeFeatureLayer_management("FloodParcels","FloodParcels_lyr")
- #add percent fields to FloodParcels
- FieldList = ('PERCFW', 'PERC100', 'PERC500', 'PERCNOT')
- for entry in FieldList:
- arcpy.AddField_management("FloodParcels_lyr", entry, "FLOAT")
- arcpy.CalculateField_management("FloodParcels_lyr", entry, "0", "PYTHON")
- arcpy.AssignDefaultToField_management("FloodParcels_lyr", entry, 0)
- arcpy.AlterField_management("FloodParcels_lyr", entry, "","","","","NON_NULLABLE")
- log.write("Field {0} has been conjured.\n".format(entry))
- RunTime = time.time() - StartTime
- log.write("Flood parcel layer conjured and defined. {0} seconds elapsed in script.\n".format(RunTime))
- FWList = ("FW", "FW_lyr", "INT_FW", "INT_FW_lyr", "DISS_FW", "DISS_FW_lyr", "AREAFW", "PERCFW")
- Y100List = ("Y100", "Y100_lyr", "INT_Y100", "INT_Y100_lyr", "DISS_Y100", "DISS_Y100_lyr", "AREAY100", "PERC100")
- Y500List = ("Y500", "Y500_lyr", "INT_Y500", "INT_Y500_lyr", "DISS_Y500", "DISS_Y500_lyr", "AREAY500", "PERC500")
- #Key:
- # 0: Filename
- # 1: Vector layer
- # 2: Intersect file
- # 3: Intersect layer
- # 4: Dissolve file
- # 5: Dissolve layer
- # 6: Area field
- # 7: Percent field
- #Define FloodList as list of lists.
- FloodList = (FWList, Y100List, Y500List)
- #Begin cycling through FloodList
- for FloodBit in FloodList:
- print "{0} is being processed...".format(FloodBit[0])
- log.write("{0} is being processed...\n".format(FloodBit[0]))
- LoopSTime = time.time()
- arcpy.CalculateField_management("FloodParcels_lyr", FloodBit[7], "0", "PYTHON")
- #arcpy.AddMessage("{0} is being processed...".format(FloodBit[0]))
- #Create arguments and layers
- arcpy.MakeFeatureLayer_management(FloodBit[0], FloodBit[1])
- ElTime = (time.time() - LoopSTime)
- print "{0} has been conjured. {1} seconds elapsed in this loop.".format(FloodBit[1], ElTime)
- log.write("{0} has been conjured. {1} seconds elapsed in this loop.\n".format(FloodBit[1], ElTime))
- IntList = ("FloodParcels_lyr", FloodBit[1])
- #Intersect to FloodBit[2]
- #Intersect_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {output_type})
- arcpy.Intersect_analysis(IntList, FloodBit[2], "ALL")
- ElTime = (time.time() - LoopSTime)
- print "{0} has been intersected. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
- log.write("{0} has been intersected. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
- #Dissolve to FloodBit[4]
- arcpy.Dissolve_management(FloodBit[2], FloodBit[4], "FID_FloodParcels")
- ElTime = (time.time() - LoopSTime)
- print "{0} has been dissolved. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
- log.write("{0} has been dissolved. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
- #Conjure layer FloodBit[5]
- arcpy.MakeFeatureLayer_management(FloodBit[4], FloodBit[5])
- ElTime = (time.time() - LoopSTime)
- print "{0} has been conjured. {1} seconds elapsed in this loop.".format(FloodBit[5], ElTime)
- log.write("{0} has been conjured. {1} seconds elapsed in this loop.\n".format(FloodBit[5], ElTime))
- #Add field FloodBit[6] to FloodBit[4]
- arcpy.AddField_management(FloodBit[5], FloodBit[6], "FLOAT")
- arcpy.AssignDefaultToField_management(FloodBit[5], FloodBit[6], 0)
- ElTime = (time.time() - LoopSTime)
- print "{0} has been added to {1}. {2} seconds elapsed in this loop.".format(FloodBit[6], FloodBit[5], ElTime)
- log.write("{0} has been added to {1}. {2} seconds elapsed in this loop.\n".format(FloodBit[6], FloodBit[5], ElTime))
- arcpy.Delete_management(FloodBit[1])
- print "{0} has been destroyed.".format(FloodBit[1])
- arcpy.Delete_management(FloodBit[3])
- print "{0} has been destroyed.".format(FloodBit[3])
- log.write("{0} and {1} have been destroyed.\n".format(FloodBit[1], FloodBit[3]))
- #Calculate field FloodBit[6]
- print "Calculating field {0}...".format(FloodBit[6])
- log.write("Calculating field {0}...\n")
- arcpy.CalculateField_management(FloodBit[5], FloodBit[6], "!shape.area@feet!", "PYTHON")
- ElTime = (time.time() - LoopSTime)
- print "{0} has been calculated. {1} seconds elapsed in this loop.".format(FloodBit[6], ElTime)
- log.write("{0} has been calculated. {1} seconds elapsed in this loop.\n".format(FloodBit[6], ElTime))
- #add field attribute index to FloodBit5
- IndexFields = str("FID_ParcelSet;" + FloodBit[6])
- arcpy.AddIndex_management(FloodBit[5], IndexFields, "DissIndex")
- print "{0} has been indexed.".format(FloodBit[5])
- log.write("{0} has been indexed.\n".format(FloodBit[5]))
- #Join field FloodBit[6] to FloodParcel
- arcpy.JoinField_management("FloodParcels_lyr", "OBJECTID", FloodBit[5], "FID_FloodParcels", FloodBit[6])
- ElTime = (time.time() - LoopSTime)
- print "{0} has been joined to the parcels. {1} seconds elapsed in this loop.".format(FloodBit[6], ElTime)
- log.write("{0} has been joined to the parcels. {1} seconds elapsed in this loop.\n".format(FloodBit[6], ElTime))
- #Build field calculator expression
- CalcExp = "(!" + FloodBit[6] + r"!/!Shape.area!)*100"
- #Calculate field FloodBit[7] = FloodBit[6] / Shape_Area
- print "Parcel area calculating on {0}...".format(CalcExp)
- log.write("Parcel area calculating on {0}...\n".format(CalcExp))
- arcpy.CalculateField_management("FloodParcels_lyr", FloodBit[7], CalcExp, "PYTHON")
- ElTime = (time.time() - LoopSTime)
- print "Parcel area calculated for {0}. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
- log.write("Parcel area calculated for {0}. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
- #Delete field FloodBit[6] from FloodParcels
- arcpy.DeleteField_management("FloodParcels_lyr", FloodBit[6])
- print "{0} has been destroyed.".format(FloodBit[6])
- log.write("{0} has been destroyed.\n".format(FloodBit[6]))
- #Delete 2-3-4-5?
- PurgeList = (FloodBit[5], FloodBit[2], FloodBit[4])
- for item in PurgeList:
- arcpy.Delete_management(item)
- log.write("{0} has been destroyed.\n".format(item))
- #Replace NULL values with 0.
- with arcpy.da.UpdateCursor("FloodParcels", [FloodBit[7]]) as cursor:
- for row in cursor:
- if row[0] == None:
- row[0] = 0
- cursor.updateRow(row)
- elif row[0] < 0:
- row[0] = 0
- elif row[0] > 100:
- row[0] = 100
- #End loop
- ElTime = (time.time() - LoopSTime)
- print "{0} has been processed. {1} seconds elapsed in this loop.".format(FloodBit[0], ElTime)
- log.write("{0} has been processed. {1} seconds elapsed in this loop.\n".format(FloodBit[0], ElTime))
- log.write("\n")
- #cleanup
- #Calculate PERCNOT
- CalcTime = time.time()
- print "Calculating area not in flood zone..."
- log.write("Calculating area outside flood zones...\n")
- CalcExp = "100 - (!PERCFW! + !PERC100! + !PERC500!)"
- arcpy.CalculateField_management("FloodParcels_lyr", "PERCNOT", CalcExp, "PYTHON")
- ElTime = (time.time() - CalcTime)
- print "Calculation completed. {0} seconds elapsed.".format(ElTime)
- log.write("Calculation completed. {0} seconds elapsed.\n".format(ElTime))
- #Overwrite service
- RunTime = time.time() - StartTime
- print "Script completed. {0} minutes elapsed.".format((RunTime/60))
- log.write("Script completed. {0} minutes elapsed.\n".format((RunTime/60)))
- log.close()
- except:
- #Get traceback object
- tb = sys.exc_info()[2]
- tbinfo = traceback.format_tb(tb)[0]
- pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
- msgs = "ArcPy Errors:\n" + arcpy.GetMessages(2) + "\n"
- arcpy.AddError(pymsg)
- arcpy.AddError(msgs)
- #print error messages and close log
- log.write("" + pymsg + "\n")
- log.write("" + msgs + "" + "\n")
- log.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement