def ChangeDistMode(UPConfig): UPConfig['DistMode'] = 'RasterEuc' upc.WriteUPConfigToGDB(UPConfig) return UPConfig
def ImportSALayer(InSALayer, UPlanGDB, LongName, SAIDField, SANameField, SearchLength): ''' *Imports an SubArea layer to the GDB *Adds a record to the upc_layers table for this SubArea layer *Updates the upc_subareas table with the new SubAreas *Updates the 3 SubArea KeyNames in the upc_key table *Updates UPConfig.p Calls: AddToUPC_Layers Called by: Import General Plan Toolbox Arguments: InSALayer: The layer to be added to the GDB as SubAreas UPlanGDB: The UPlan GDB (where the layer will be imported) LongName: The descriptive name of the layer SAIDField: Field within the layer that contains the SubArea codes (or IDs) SANameField: Field within the layer that contains the SubArea descriptions (or Names) SearchLength: When assigning Base Geometry centroids to SubAreas, this is the maximum distance a centroid can be outside of a SubArea polygon and still be assigned to it Returns: None ''' #Create a function to import a Subarea Layer, record the subarea_id and replace any prior subarea records arcpy.env.workspace = UPlanGDB if os.path.basename(InSALayer)[-4:] == ".shp": SHPName = os.path.basename(InSALayer)[:-4] else: SHPName = os.path.basename(InSALayer) #Add layer to geodatabase arcpy.FeatureClassToGeodatabase_conversion(InSALayer, UPlanGDB) #add general plan layer to layer tracker table AddToUPC_Layers(UPlanGDB, SHPName, LongName, 'Subareas') #clear sub areas table arcpy.DeleteRows_management('upc_subareas') #populate sub areas table with attributes from the new layer Subareas = [] rows = arcpy.SearchCursor(SHPName) for row in rows: SA = {} SA['sa'] = row.getValue(SAIDField) SA['SAName'] = row.getValue(SANameField) if SA not in Subareas: # don't add a SA that is already in the list Subareas.append(SA) print(Subareas) fields = ['Code', 'Name'] cur = arcpy.da.InsertCursor('upc_subareas', fields) for x in range(0, len(Subareas)): sa = Subareas[x]['sa'] SAName = Subareas[x]['SAName'] cur.insertRow((sa, SAName)) del cur #update the upc_key table with the new subarea - UpdateUPCKeyTable(UPlanGDB, 'Subarea_bnd', SHPName) UpdateUPCKeyTable(UPlanGDB, 'Subarea_id', SAIDField) UpdateUPCKeyTable(UPlanGDB, 'Subarea_search', str(SearchLength)) #update pickle file DBSplit = uiut.SplitPath(UPlanGDB) UPConfig = {} UPConfig = upc.ReadUPConfigFromGDB(DBSplit[0], DBSplit[1]) uiut.MakePickle(UPConfig, UPlanGDB)
def ImportBaseGeom(UPlanGDB, InSHP, DescName, CentInside): ''' *Imports a Base Geometry layer to the GDB *Adds a field called up_polyid and calculates it equal to the ObjectID *Creates a Centroid layer from the Base Geometry layer and names it BaseGeom_cent *Adds a record to the upc_layers table for this Base Geometry layer and the centroid layer *updates the upc_keys table with the new base geometry info *updates pickle file Calls: AddToUPC_Layers Called by: The Import Base Geometry Toolbox Arguments: UPlanGDB: The UPlan GDB (where the layer will be imported) InSHP: The shapefile to import DescName: The descriptive name of the layer CentInsdie = True if you want to force the centroid inside of the polygons, false for true centroid Returns: None ''' arcpy.env.workspace = UPlanGDB arcpy.env.overwriteOutput = True #Register it with the layer tracker if os.path.basename(InSHP)[-4:] == ".shp": SHPName = os.path.basename(InSHP)[:-4] #if the shp has a field named "OBJECTID", it needs to be deleted #(SHPs use FID, so if OBJECT ID is there, it's from copying form a GDB #when copied back into the UPlan GDB it creates OBJECTID_1 instead of OBJECTID if OBJECTID isn't deleted) if FieldExist(InSHP, 'OBJECTID'): arcpy.DeleteField_management(InSHP, 'OBJECTID') else: SHPName = os.path.basename(InSHP) AddToUPC_Layers(UPlanGDB, SHPName, DescName, 'BaseGeom_bnd') #delete up_polyid field if it exists if FieldExist(InSHP, 'up_polyid'): arcpy.DeleteField_management(InSHP, 'up_polyid') #Import the shp to the GDB arcpy.FeatureClassToFeatureClass_conversion(InSHP, UPlanGDB, SHPName) #Add int field called "up_polyid" and calc = to object ID arcpy.AddField_management(SHPName, "up_polyid", "LONG") arcpy.CalculateField_management(SHPName, "up_polyid", "!OBJECTID!", "PYTHON_9.3") #Create centroid layer and drop all fields except for "up_polyid" (and shape and objectid). if arcpy.ProductInfo() == 'ArcInfo': if CentInside: CentType = 'INSIDE' else: CentType = 'CENTROID' arcpy.FeatureToPoint_management(SHPName, "poly_cent", CentType) else: CreateCentroids(UPlanGDB, SHPName) Fields = arcpy.ListFields("poly_cent") FieldList = [] for Field in Fields: if not Field.required: if not Field.name == "up_polyid": FieldList.append(Field.name) arcpy.DeleteField_management("poly_cent", FieldList) #add centroid layer to layer tracker table AddToUPC_Layers(UPlanGDB, 'poly_cent', 'Base Geometry Centroids (Created from: ' + SHPName + ')', 'BaseGeom_cent') #update upc_key table with base geometry info UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_bnd', SHPName) UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_cent', 'poly_cent') UpdateUPCKeyTable(UPlanGDB, 'BaseGeom_id', 'up_polyid') #update pickle file DBSplit = uiut.SplitPath(UPlanGDB) UPConfig = {} UPConfig = upc.ReadUPConfigFromGDB(DBSplit[0], DBSplit[1]) uiut.MakePickle(UPConfig, UPlanGDB)
''' Created on Feb 5, 2015 @author: roth ''' import os import UPConfig from Utilities import UPCleanup if __name__ == "__main__": print("Starting Cleanup") UPConfig = UPConfig.LoadUPConfig_python(True, True) UPCleanup(UPConfig) print("Cleanup Finished")
''' Created on Feb 5, 2015 @author: roth ''' import os import arcpy import UPConfig import CalcGeneralPlans import CalcConstraints import CalcWeights from Utilities import Logger if __name__ == "__main__": Logger("Running UPlan") global UPConfig UPConfig = UPConfig.LoadUPConfig_python() CalcGeneralPlans.CalcGP(UPConfig) CalcConstraints.CalcConstraints(UPConfig) CalcWeights.CalcWeights(UPConfig) Logger("UPlan Finished")
Logger("Writing Redevelopment Pop and Emp") au.WriteRedev(UPConfig, TSResults[3], "upo_redev_{ts}".format(ts=TimeStep[0])) if __name__ == "__main__": """ Call the primary function for this module for testing purposes. """ # dbpath = r"G:\Public\UPLAN\Uplan4\testing\TestAllocation" # dbname = "AllocTest_ReDev.gdb" dbpath = r"G:\Public\UPLAN\Calaveras\Debug" dbname = "RyanTest_Calaveras.gdb" #global UPConfig # UPConfig = UPConfig.LoadUPConfig_python(True,True) UPConfig = UPConfig.ReadUPConfigFromGDB(dbpath, dbname) UPConfig['Redev'] = None ##########uncomment this line if redev isn't working # UPConfig['Redev'] = None # picklepath = "\\".join([dbpath,dbname,"UPConfig.p"]) # UPConfig2 = uiut.LoadPickle(picklepath) Logger("Cleanup and Prep" ) # So it doesn't error out when trying to overwrite tables. Utilities.UPCleanup_Alloc(UPConfig) Logger("Run Full Allocation") Allocate(UPConfig) Logger("Allocation Complete")
ts=ts[0])) return (results_array) if __name__ == "__main__": Logger("Calculating Weights") dbpath = r"..\testing\TestAllocation" dbname = 'AllocTest.gdb' UPGDB = os.path.join(dbpath, dbname) saveDisaggWts = True # UPGDB = r"..\\testing\calaveras.gdb" # saveDisaggWts = False # picklepath = "\\".join([UPGDB,"UPConfig.p"]) # UPConfig = uiut.LoadPickle(picklepath) splitpath = uiut.SplitPath(UPGDB) UPConfig = upc.ReadUPConfigFromGDB(splitpath[0], splitpath[1]) Logger("Cleaning up Weights") Utilities.UPCleanup_Weights(UPConfig) Logger("Calculating Distances") # Get unique set of attractors attractors = [] for TimeStep in UPConfig[ 'TimeSteps']: # Build a list of all attractors used in the run. newattractors = UPConfig[TimeStep[0]]['attractors'] attractors = attractors + newattractors attractors = set(attractors) CalcDistanceLayerMultiple(UPConfig, attractors, False) # False disables multi processing. for TimeStep in UPConfig['TimeSteps']: Logger("Working on time step: {ts}".format(ts=TimeStep[0]))
dbname = 'calaveras.gdb' TSCode = 'ts1' TestDemand = True TestRedev = False # Testing primary Demand Calculations if TestDemand == True: InWorkspace = os.path.join(dbpath, dbname) UPDemand = CalculateDemand(InWorkspace, TSCode) UPDemand = CalculateDemand(InWorkspace, 'ts2') print(UPDemand) # Testing Redevelopment if TestRedev == True: Logger("Reading UPConfig") UPConfig = upc.ReadUPConfigFromGDB(dbpath, dbname) pop = 250 emp = 200 resac = CalcRedevRes(UPConfig, UPConfig['TimeSteps'][0], pop) print(resac[0]) empac = CalcRedevEmp(UPConfig, UPConfig['TimeSteps'][0], emp) print(empac[0]) redevResults = ReDevCalcs(UPConfig, UPConfig['TimeSteps'][0], pop, emp) Logger("pause") print("script finished")
gplans.columns = [[UPConfig['BaseGeom_id'],UPConfig[TimeStep[0]]['gp'][1]]] # gplans = gplans.set_index([UPConfig['BaseGeom_id']]) # gplans[UPConfig['BaseGeom_id']] = gplans.index gparray = None return(gplans) if __name__ == "__main__": """ Testing functions within this utility module. """ Logger("Beginning Tests") global UPConfig UPConfig = UPConfig.ReadUPConfigFromGDB("..//testing","calaveras_complete.gdb") TimeStep = UPConfig['TimeSteps'][0] # Logger("Testing Making Allocation Table") # allocTable = MakeAllocTables(UPConfig) # Logger("Testing Saving Allocation Table") # try: # Utilities.SavePdDataFrameToTable(allocTable, "..//testing/Calaveras.gdb", 'z_testTable') # except Exception, e: # Logger("Saving Allocation Table Failed: {err}".format(err=str(e))) Logger("Testing DevSpace Calculations") devSpace = MakeDevSpace(UPConfig, TimeStep) Utilities.SavePdDataFrameToTable(devSpace, "..//testing/Calaveras.gdb", "z_devspace") Logger("DevSpace Calculation Complete")
picklepath = os.path.join(dbpath,dbname) # get UPConfig print("getting path") UPConfig = uiut.LoadPickle(picklepath) startlist = ['A','B','C','D','E'] outlist = uiut.InsertToList(startlist, "N", 2) TSShortName = "TS3" TSLongName = "Time Step 3" TSPosition = 2 TimeSteps = UPConfig['TimeSteps'] print(TimeSteps) UPConfig['TimeSteps'] = uiut.InsertToList(TimeSteps, [TSShortName,TSLongName], TSPosition) print(UPConfig['TimeSteps']) # Empty Timestep UPConfig[TSShortName] = uiut.MakeEmptyTimeStep() # Write out the pickle uiut.MakePickle(UPConfig, picklepath) # Write out to tables upc.WriteUPConfigToGDB(UPConfig) # excluding upc_layers print("Done")