# ---------------------------------------------------------------------------------------------- # GetGISCAMARuntimeData.py # Version 1.2 # Last Updated: 2/16/2017 # Script by: Ken Morrell # Description: Extracts GIS data into file geodatabase, merges CAMA data and creates runtime GDB # ---------------------------------------------------------------------------------------------- # Import modules import datetime prgstart = datetime.datetime.now() import arcpy, sys, os, glob from arcpy import env # Get local variables from arguments: conum = sys.argv[1] # 2-digit county number countyname = sys.argv[2] # county name GDB = sys.argv[3] # source geodatabase outpath = sys.argv[4] # output path reappr = sys.argv[5].upper() # include reappraisal CAMA data in_map = sys.argv[6] # MXD path and name for runtime content runtime_GDB = sys.argv[7] # Runtime geodatabase output path # Initialize other variables tempGDB = outpath+"\\"+countyname+"_CAMA.gdb" prgpath = os.getcwd() steptime = prgstart steptime2 = prgstart eltime = prgstart # Set reappraisal variable if reappr is None: reappr = 'No' elif reappr == 'R' or reappr == 'Y': reappr = 'Yes' else: reappr = 'No' ####Sample argument values (for development only) ##conum = "59" #sys.argv[1] 2-digit county number ##countyname = "Marshall" #sys.argv[2] # county name ##GDB = "Database Connections\DConn - GIS @ Default @ TNBMP2NEW.sde" #sys.argv[3] # source geodatabase ##outpath = "C:\GISData\Marshall" #sys.argv[4] # output path ##reappr = "Y" ##in_map = "C:\GISData\Marshall\MXD\ParcelLayers_Marshall.mxd" ##runtime_GDB = "C:\GISData\Marshall\Parcels" ## Set coordinate systems variables inputCS = arcpy.SpatialReference(prgpath+'\NAD 1983 StatePlane Tennessee FIPS 4100 (US Feet).prj') outputCS = arcpy.SpatialReference(prgpath+'\WGS 1984 Web Mercator (auxiliary sphere).prj') arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(prgpath+'\WGS 1984 Web Mercator (auxiliary sphere).prj') ##env.workspace = env.workspace = GDB print '' #Create output folder if it does not exist if not os.path.exists(outpath): os.mkdir(outpath) #Define function for elapsed time of each step def ElapsedTime(): global steptime, steptime2, eltime steptime2 = datetime.datetime.now() eltime = steptime2-steptime print ' Completed (Elapsed time: '+str(eltime.seconds//3600).zfill(2)+':'+str(eltime.seconds//60).zfill(2)+':'+str(eltime.seconds%60).zfill(2)+')' steptime = steptime2 #Define function for exception processing def TryException(msg): import traceback print ' **' + msg ##print(e.message) #Direct output to logfile and screen ##sys.stdout = open(f_outpath+"/Logfile_"+countyname+".txt","w") f_outpath=str.replace(outpath,"\\","/") f_tempGDB=str.replace(tempGDB,"\\","/") class MyWriter: def __init__(self, stdout, filename): self.stdout = stdout self.logfile = file(filename,"w") def write(self, text): self.stdout.write(text) self.logfile.write(text) def close(self): self.stdout.close() self.logfile.close() writer = MyWriter(sys.stdout, f_outpath+'/Logfile_'+countyname+'.txt') sys.stdout = writer ##List parameters print '' print '*** Make GIS CAMA Data from SDE Geodatabase ***' print '' print 'Program Name: ' + sys.argv[0] print 'Program Folder: ' + os.getcwd() print 'County Number: ' + conum print 'County Name: ' + countyname print 'Input GDB: ' + env.workspace print 'Output Path: ' + outpath print 'Temporary GDB: ' + tempGDB print 'Include Reappr: ' + reappr try: print 'Map document ' + in_map print 'Runtime GDB: ' + runtime_GDB except Exception: print 'Map document ' print 'Runtime GDB: ' print '' print 'Program Started: ' + prgstart.strftime("%m/%d/%Y %H:%M:%S %p") print '' ##sys.exit() ##For testing only ElapsedTime() print 'Initiating program - Completed (Elapsed time: '+str(eltime.seconds//3600).zfill(2)+':'+str(eltime.seconds//60).zfill(2)+':'+str(eltime.seconds%60).zfill(2)+')' ##Delete previous temporary file geodatabase print('Deleting previous temporary file geodatabase'), try: arcpy.Delete_management(tempGDB) ElapsedTime() except Exception: TryException('NOT SUCCESSFUL') ##Create file geodatabase print('Creating new temporary file geodatabase'), try: arcpy.CreateFileGDB_management(outpath, countyname+"_CAMA") ElapsedTime() except Exception: TryException('NOT CREATED') ##Add source feature classes to file geodatabase print ' ' print'Adding feature classes...' arcpy.env.workspace = GDB list = ['Index400','Index100','Index50','Parcels','Acreage_Anno','Block_Num_Anno','Control_Num_Anno','Easement_Anno','Exempt_Anno', 'Gas_Pipe_Anno','Group_Corner_Anno','Lot_Num_Anno','Lot_Tract_Acreage_Anno','Misc_Anno','Misc_Lines', 'Parcel_Anno','Parcel_Conflict_Anno','Parcel_Dim_Anno','Point_Symbols','ProposedROW_Anno','RailRoadROW_Anno', 'Special_Int_Anno','StreetROW_Anno','Subdivision_Anno', 'Creek_Anno','EOP_Lines','Hydro_Lines','Hydro_Poly','Lake_Pond_Anno','River_Anno','Streets','Trees', 'City','City_Anno','Civil_Anno','Civil_Districts','County','County_Anno','Special_Districts'] for item in list: if item in ('Index400','Index100','Index50'): fDataSet = '_MapIndex' elif item in ('Parcels','Acreage_Anno','Block_Num_Anno','Control_Num_Anno','Easement_Anno','Exempt_Anno', 'Gas_Pipe_Anno','Group_Corner_Anno','Lot_Num_Anno','Lot_Tract_Acreage_Anno','Misc_Anno','Misc_Lines', 'Parcel_Anno','Parcel_Conflict_Anno','Parcel_Dim_Anno','Point_Symbols','ProposedROW_Anno','RailRoadROW_Anno', 'Special_Int_Anno','StreetROW_Anno','Subdivision_Anno'): fDataSet = '_Cadastral' elif item in ('Creek_Anno','EOP_Lines','Hydro_Lines','Hydro_Poly','Lake_Pond_Anno','River_Anno','Streets','Trees'): fDataSet = '_Planimetric' elif item in ('City','City_Anno','Civil_Anno','Civil_Districts','County','County_Anno','Special_Districts'): fDataSet = '_Admin' print(" "+item), try: ## feature classes having selection criteria if item in ('Parcels'): arcpy.FeatureClassToFeatureClass_conversion(GDB+"\\GIS."+countyname+fDataSet+"\\GIS."+item+"_"+conum,f_tempGDB,item+'_CAMA_A',"GISLINK > ' '"), if reappr == 'Yes': arcpy.FeatureClassToFeatureClass_conversion(GDB+"\\GIS."+countyname+fDataSet+"\\GIS."+item+"_"+conum,f_tempGDB,item+'_CAMA_R',"GISLINK > ' '"), elif item in ('Index100'): arcpy.FeatureClassToFeatureClass_conversion(GDB+"\\GIS."+countyname+fDataSet+"\\GIS."+item+"_"+conum,f_tempGDB,item,"TILE_TYPE <= 2"), else: ## feature classes not having selection criteria arcpy.FeatureClassToFeatureClass_conversion(GDB+"\\GIS."+countyname+fDataSet+"\\GIS."+item+"_"+conum,f_tempGDB,item) ElapsedTime() except Exception as e: TryException('NOT COMPLETED') ## Merge selected layers env.workspace = tempGDB ##Change workspace to working file geodatabase print ' Merging Admin layers', try: arcpy.Merge_management(["County","City"],tempGDB+"/Admin") ElapsedTime() except Exception as e: TryException('NOT COMPLETED') ##Report total elapsed time for file GDB extract prgend = datetime.datetime.now() eltime = steptime2-prgstart print 'Geodatabase extraction: ' + prgend.strftime("%m/%d/%Y %H:%M:%S %p")+" (GDB elapsed time: "+str(eltime.seconds//3600).zfill(2)+':'+str(eltime.seconds//60).zfill(2)+':'+str(eltime.seconds%60).zfill(2)+')' ## Join with CAMA data env.workspace = tempGDB ##Change workspace to working file geodatabase inputtable = 'Parcels_CAMA_A' jointable = outpath+'\\camagisdata.dbf' ## Non-reappraisal CAMA data joinfield = 'GISLINK' print '' print("Joining IMPACT attribute data...") try: arcpy.JoinField_management(inputtable, joinfield, jointable, joinfield) ElapsedTime() except Exception as e: TryException('NOT COMPLETED') if reappr == 'Yes': inputtable = 'Parcels_CAMA_R' jointable = outpath+'\\camagisdata_r.dbf' ## Reappraisal CAMA data print '' print("Joining reappraisal data...") try: arcpy.JoinField_management(inputtable, joinfield, jointable, joinfield) ElapsedTime() except Exception as e: TryException('NOT COMPLETED') ## Create runtime geodatabase (runtime content) if in_map is not None and runtime_GDB is not None: # Delete runtime GDB output folder if arcpy.Exists(runtime_GDB): arcpy.management.Delete(runtime_GDB) ## Create runtime content print '' print'Creating runtime geodatabase...' try: arcpy.CreateRuntimeContent_management(in_map,runtime_GDB,"#","#","DISPLAY","NETWORK_DATA; FEATURE_AND_TABULAR_DATA","NON_OPTIMIZE_SIZE","ONLINE","PNG","1","#") ElapsedTime() except Exception as e: TryException('NOT COMPLETED') # Report total program time print '' prgend = datetime.datetime.now() eltime = prgend-prgstart print 'Program completed: ' + prgend.strftime("%m/%d/%Y %H:%M:%S %p")+" (Total elapsed time: "+str(eltime.seconds//3600).zfill(2)+':'+str(eltime.seconds//60).zfill(2)+':'+str(eltime.seconds%60).zfill(2)+')' sys.stdout=sys.__stdout__