Пример #1
0
def WriteDistanceToTable(UPConfig, Distarr):
    '''
    Add records to up_distance. If up_distances does not exist, it is created.
    
    Called By:
    CalcDistancesLayer
    
    Calls:
    
    Arguments:
    UPConfig
    Distarr a numpy structured array with (attracter, BaseGeom_id, distance)

    
    '''
    if arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'],
                         'up_distances')) == False:
        Logger("Making up_distances")
        MakeUPDistTable(UPConfig)

    Logger("Inserting to Distance Table")
    # insert for this one
    cur = arcpy.da.InsertCursor(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     'up_distances'),
        ['attracter', UPConfig['BaseGeom_id'], 'distance'])
    for ln in Distarr:
        row = (ln['attracter'], ln[UPConfig['BaseGeom_id']], ln['distance'])
        cur.insertRow(row)
Пример #2
0
def WriteNetWeightsByLu(UPConfig, ts, lu, weights):
    '''
    Writes net weights to table. Creates the table if needed and removes potentially conflicting data.
    
    Called By:
    GetWeightsByTs
    
    Calls:
    MakeUPNetWeightsTable
    RemoveNetWeightsFromTableByLu
    '''

    if not arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights')):
        MakeUPNetWeightsTable(UPConfig, ts, lu)
    else:
        RemoveNetWeightsFromTableByLu(UPConfig, ts, lu)

    Logger("Writing Net Weights")
    cur = arcpy.da.InsertCursor(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     'up_net_weights'),
        ['timestep', 'lu', UPConfig['BaseGeom_id'], 'weight'])
    for ln in weights:
        row = (ts[0], lu, ln[UPConfig['BaseGeom_id']], ln['weight'])
        cur.insertRow(row)
    Logger("Weights Written")
Пример #3
0
def CalcAvailSpace(UPConfig,TimeStep,lu,row,cumAlloc):
    """
    Calculate the amount of available space for a land use in a polygon based on unconstrained acres, cumulative allocation, and general plan settings.
    
    Called By:
    Allocation.PriAllocLU
    
    Calls:
    OrderLU
    MakeLUListForGP
    MakeMUList
    
    Arguments:
    UPConfig
    TimeStep
    lu: the land use being tested for allocation.
    row: the row from devSpaceTable
    cumAlloc: cumulative allocation table
    """
    pclid = row[UPConfig['BaseGeom_id']]
    
#     if pclid == 30:
#         print("Stop here")
    
    # get cumAlloc row for polygon
    try:
        caRow = cumAlloc.loc[cumAlloc[UPConfig['BaseGeom_id']]== pclid] #cumAlloc.loc[pclid]
    except Exception, e:
        availableSpace = 0
        remList = [] 
        Logger("Error, AllocUtilities.CalcAvailSpace")
        Logger(str(e))
        #raise(e)
        return([availableSpace,remList])
Пример #4
0
def RemoveDistanceFromTable(UPConfig, layername):
    '''
    Remove records for "layername" from up_distance
    
    Called By:
    CalcDistancesLayer
    
    Calls:
    
    Arguments:
    UPConfig
    layername
    
    '''

    if arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances')) == True:

        Logger("Cleaning up_distances: {ln}".format(ln=layername))
        # Delete any records from the table for the layer
        whereclause = """attracter = '{ln}'""".format(ln=layername)
        cur = arcpy.da.UpdateCursor(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            ['OID@'], whereclause)
        for row in cur:
            cur.deleteRow()
    else:
        Logger("up_distances does not exist: skipping cleaning")
Пример #5
0
def CreateOrEmptyTable(db, TableName, Fields, FieldTypes):
    '''
    If the given table doesn't exist, this function will create the table.
    If the given table does exist, all rows will be removed
    
    Called by: CalculateDemand
    
    Arguments:
    db: full path to the uplan file geodatabase
    TableName: The name of the table
    Fields: A list containing the field names
    FieldTypes: A Dictionary that defines the field types by the field names
    
    '''
    arcpy.env.workspace = db
    if arcpy.Exists(TableName):
        Logger("Emptying Table: " + TableName)
        #arcpy.DeleteRows_management(TableName)
    else:
        #create the table
        Logger("Creating Table: " + TableName)
        arcpy.CreateTable_management(InWorkspace, TableName)
        #add the field(s)
        for Field in Fields:
            arcpy.AddField_management(TableName, Field, FieldTypes[Field])
Пример #6
0
def CalcDistancesLayer(UPConfig, layername):
    '''
    Calculate Distances for one layer and enter them to up_distances table.
    
    Called By:
    main
    
    Calls:
    CalcDistArray
    RemoveDistanceFromTable
    WriteDistancetoTable
    
    Arguments:
    UPConfig
    Layername
    '''

    Logger("Calculating Distances: {ln}".format(ln=layername))
    if UPConfig['DistMode'] == 'GenerateNear':
        Logger("Distance Mode=GenerateNear")
        Distarr = CalcDistArray([UPConfig, layername])
        RemoveDistanceFromTable(UPConfig, layername)
        WriteDistanceToTable(UPConfig, Distarr)

    elif UPConfig['DistMode'] == 'RasterEuc':
        Logger("Distance Mode=RasterEuc")
        Distarr = CalcDistArrayRaster([UPConfig, layername, 50])
        RemoveDistanceFromTable(UPConfig, layername)
        WriteDistanceToTable(UPConfig, Distarr)
        pass

    Logger("Distance Calculated")
Пример #7
0
def WriteDisaggWeightsByLu(UPConfig, ts, lu, weights):
    '''
    Write disaggregate weight to table by lu. 
    Creates the table if it doesn't exist, and empties potentially confilicting data
    
    Called By:
    GetWeightsByTs
    main
    
    Calls:
    makeUPDisaggWeightsTable
    RemoveDisaggWeightsFromTableByLu
    
    
    '''
    if not arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_disagg_weights')):
        MakeUPDisaggWeightsTable(UPConfig, ts, lu)
    else:
        RemoveDisaggWeightsFromTableByLu(UPConfig, ts, lu)

    Logger("Writing Disaggregate Weights")
    cur = arcpy.da.InsertCursor(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     'up_disagg_weights'),
        ['timestep', 'lu', 'attracter', UPConfig['BaseGeom_id'], 'weight'])
    for ln in weights:
        row = (ts[0], lu, ln['attracter'], ln[UPConfig['BaseGeom_id']],
               ln['weight'])
        cur.insertRow(row)
    Logger("Weights Written")
Пример #8
0
def CalcRedevRes(UPConfig, TimeStep, pop):
    """
    Return the number of acres of each res type for redevelopment
    
    
    """

    Logger("Calculating Redev Acres: Residential")
    gdb = os.path.join(UPConfig['paths']['dbpath'],
                       UPConfig['paths']['dbname'])

    # get pphh
    whereClause = """TSCode = '{ts}'""".format(ts=TimeStep[0])
    fields = ['PPHH']
    cur = arcpy.da.SearchCursor(os.path.join(gdb, 'upd_demographics'), fields,
                                whereClause)
    pphh = cur.next()[0]

    # get subarea proportions
    sap = {}
    for sa in UPConfig['Subareas']:
        sap[sa['sa']] = {}
        whereClause = """TSCode = '{ts}' AND SACode = '{sacd}' """.format(
            ts=TimeStep[0], sacd=sa['sa'])
        cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_subareares'),
                                 whereClause, ['LUCode', 'PctRes'])
        lus = []
        for row in cur:
            lus.append(row.getValue('LUCode'))
            sap[sa['sa']][row.getValue('LUCode')] = row.getValue('PctRes')

        # Get the subarea's proportion of total pop
        cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_subareademand'),
                                 whereClause, ['PctRes'])
        sap[sa['sa']]['PctRes'] = cur.next().getValue('PctRes')

    # Get densities

    whereClause = """TSCode = '{ts}'""".format(ts=TimeStep[0])
    cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_rescalcs'), whereClause,
                             ['LUCode', 'GrossAcPerOccUnit'])
    acPerUnit = {}
    for row in cur:
        acPerUnit[row.getValue('LUCode')] = row.getValue('GrossAcPerOccUnit')

    resAc = {}
    reDevHH = {}
    for sa in UPConfig['Subareas']:
        resAc[sa['sa']] = {}
        reDevHH[sa['sa']] = {}
        sapop = pop * sap[sa['sa']]['PctRes']
        sahh = sapop / pphh
        for lu in lus:
            reDevHH[sa['sa']][lu] = sahh * sap[sa['sa']][lu]
            resAc[sa['sa']][lu] = reDevHH[sa['sa']][lu] * acPerUnit[lu]

    Logger("Calculated Redev Acres")

    return ([resAc, reDevHH])
Пример #9
0
def Allocate(UPConfig):
    """
    Manages the full allocation including all timesteps, subareas, landuses, and redevelopment
    
    How it works:
    1. Creates the cumulative allocation table
    2. Loops through the time steps
    3. For each time step:
        1. Call AllocateTimeStep()
        2. Collect the results
        3. Write the cumulative allocation, time step allocation, and any underallocation to disk. 
    4. Move to next time step.
    
    
    Called By: 
    Python Toolbox
    
    Calls:
    au.MakeAllocTables
    AllocateTimeStep
    au.WriteAllocTables
    au.AllLUField
    
    Arguments:
    UPConfig
    
    
    """

    # make cumAlloc
    cumAlloc = au.MakeAllocTables(UPConfig)

    # for TimeStep in UPConfig:
    for TimeStep in UPConfig['TimeSteps']:
        Logger("Working on Timestep: {ts}".format(ts=TimeStep[1]))

        TSResults = AllocateTimeStep(UPConfig, TimeStep, cumAlloc)

        # Save Cumulative Allocation at this point
        Logger("Writing Cumulative Allocation")
        au.WriteAllocTables(UPConfig, TSResults[0],
                            "upo_cum_alloc_{ts}".format(ts=TimeStep[0]))
        au.AddLUField(UPConfig, "upo_cum_alloc_{ts}".format(ts=TimeStep[0]))

        #Save the TimeStep's Allocation
        Logger("Writing TimeStep Allocation")
        au.WriteAllocTables(UPConfig, TSResults[1],
                            "upo_ts_alloc_{ts}".format(ts=TimeStep[0]))

        #Save any underallocation for the TimeStep
        Logger("Writing UnderAllocation")
        au.WriteUndAlloc(
            UPConfig, TSResults[2], "upo_und_alloc_{ts}".format(ts=TimeStep[0])
        )  # AllocTables(UPConfig, TSResults[2], "upo_und_alloc_{ts}".format(ts=TimeStep[0]))

        Logger("Writing Redevelopment Pop and Emp")
        au.WriteRedev(UPConfig, TSResults[3],
                      "upo_redev_{ts}".format(ts=TimeStep[0]))
Пример #10
0
def MakeUPNetWeightsTable(UPConfig, ts, lu):
    '''
    Create an empty table to hold weights data in the database. 
    Only call this if you want to create a new table. This
    function is not intended to overwite exising versions. 
    
    Called By:
    WriteNetWeightsByLu
    
    Calls:
  
    Arguments:
    UPConfig
    
    '''

    if not arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights')):
        Logger("Creating New up_net_weights table")
        arcpy.env.overwriteOutput = False
        arcpy.CreateTable_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname']), 'up_net_weights')
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'),
            'timestep', 'TEXT', "", "", 8)
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'), 'lu',
            'TEXT', "", "", 8)
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'),
            UPConfig['BaseGeom_id'], 'LONG')
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'),
            'weight', 'DOUBLE')
        arcpy.AddIndex_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'),
            'timestep', 'timestep_nwt_idx')
        arcpy.AddIndex_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'), 'lu',
            'lu_nwt_idx')
        arcpy.AddIndex_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_net_weights'),
            UPConfig['BaseGeom_id'],
            "_".join([UPConfig['BaseGeom_id'], 'nwt', 'idx']))
        Logger("Created New up_net_weights table")
    else:
        Logger("up_net_weights table already exists, skipping")
Пример #11
0
def CalcRedevEmp(UPConfig, TimeStep, emp):
    """
    Return the number of acres of each emp type for redevelopment
    
    
    """

    Logger("Calculating Redev Acres: Employment")
    gdb = os.path.join(UPConfig['paths']['dbpath'],
                       UPConfig['paths']['dbname'])

    # get subarea proportions
    sap = {}
    for sa in UPConfig['Subareas']:
        sap[sa['sa']] = {}
        whereClause = """TSCode = '{ts}' AND SACode = '{sacd}' """.format(
            ts=TimeStep[0], sacd=sa['sa'])
        cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_subareaemp'),
                                 whereClause, ['LUCode', 'PctEmp'])
        lus = []
        for row in cur:
            lus.append(row.getValue('LUCode'))
            sap[sa['sa']][row.getValue('LUCode')] = row.getValue('PctEmp')

        # Get the subarea's proportion of total pop
        cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_subareademand'),
                                 whereClause, ['PctEmp'])
        sap[sa['sa']]['PctEmp'] = cur.next().getValue('PctEmp')

    # Get densities

    whereClause = """TSCode = '{ts}'""".format(ts=TimeStep[0])
    cur = arcpy.SearchCursor(os.path.join(gdb, 'upd_empcalcs'), whereClause,
                             ['LUCode', 'GrossAcPerEmp'])
    acPerEmp = {}
    for row in cur:
        acPerEmp[row.getValue('LUCode')] = row.getValue('GrossAcPerEmp')

    empAc = {}
    reDevEmp = {}
    for sa in UPConfig['Subareas']:
        empAc[sa['sa']] = {}
        reDevEmp[sa['sa']] = {}
        saemp = emp * sap[sa['sa']]['PctEmp']
        for lu in lus:
            reDevEmp[sa['sa']][lu] = saemp * sap[sa['sa']][lu]
            empAc[sa['sa']][lu] = reDevEmp[sa['sa']][lu] * acPerEmp[lu]

    Logger("Calculated Redev Acres")

    return ([empAc, reDevEmp])
Пример #12
0
def PopulateEmpCalcTable(db, TSCode):
    #calculate employment space values and populate the upd_empcalcs table
    EmpCalcFields = ['TSCode', 'LUCode', 'GrossAcPerEmp']
    EmpCalcFieldTypes = {
        'TSCode': 'TEXT',
        'LUCode': 'TEXT',
        'GrossAcPerEmp': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_empcalcs', EmpCalcFields, EmpCalcFieldTypes,
                       TSCode)

    Logger("Populating upd_empcalcs")
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_empcalcs'),
                                EmpCalcFields)
    EmpRows = arcpy.SearchCursor('upd_emplanduses',
                                 where_clause=r"TSCode = '" + TSCode + r"'")
    for EmpRow in EmpRows:
        LUCode = EmpRow.getValue('LUCode')
        SFPerEmp = EmpRow.getValue('SFPerEmp')
        FAR = EmpRow.getValue('FAR')
        PerOtherSpace = EmpRow.getValue('PctOther')
        AcrePerEmp = ((SFPerEmp / FAR) / (1 - PerOtherSpace)) / 43560

        cur.insertRow((TSCode, LUCode, AcrePerEmp))
    del cur
Пример #13
0
def PopulateEmpLUTable(db, TSCode, EmpInputs):
    '''
    EmpInputs = A dictionary that contains square feet per employee, FAR, and percent other space by Employment Land Use
        * The Key Name for square feet per employee = 'SFPerEmp'
        * The Key Name for FAR = 'FAR'
        * The Key Name for percent other space = 'PctOther'
        * Example Dictionary = {'ind': {'FAR': 0.2, 'PctOther': 0.5, 'SFPerEmp': 650}, 'ser': {'FAR': 0.5, 'PctOther': 0.2, 'SFPerEmp': 250}}
    '''
    #populate the upd_emplanduses table
    EmpLUFields = ['TSCode', 'LUCode', 'LUName', 'SFPerEmp', 'FAR', 'PctOther']
    EmpLUFieldTypes = {
        'TSCode': 'TEXT',
        'LUCode': 'TEXT',
        'LUName': 'TEXT',
        'SFPerEmp': 'DOUBLE',
        'FAR': 'DOUBLE',
        'PctOther': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_emplanduses', EmpLUFields, EmpLUFieldTypes,
                       TSCode)

    Logger("Populating upd_emplanduses")
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_emplanduses'),
                                EmpLUFields)
    EmpRows = arcpy.SearchCursor('upc_lu', where_clause=r"LUType = 'emp'")
    for EmpRow in EmpRows:
        LUCode = EmpRow.getValue('Code')
        LUName = EmpRow.getValue('Name')
        SFPerEmp = EmpInputs[LUCode]['SFPerEmp']
        FAR = EmpInputs[LUCode]['FAR']
        PctOther = EmpInputs[LUCode]['PctOther']

        cur.insertRow((TSCode, LUCode, LUName, SFPerEmp, FAR, PctOther))
    del cur
Пример #14
0
def PopulateResCalcsTable(db, TSCode):
    #calculate residential space values and populate the upd_rescalcs table
    ResCalcFields = ['TSCode', 'LUCode', 'GrossAcPerUnit', 'GrossAcPerOccUnit']
    ResCalcFieldTypes = {
        'TSCode': 'TEXT',
        'LUCode': 'TEXT',
        'GrossAcPerUnit': 'DOUBLE',
        'GrossAcPerOccUnit': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_rescalcs', ResCalcFields, ResCalcFieldTypes,
                       TSCode)

    Logger("Populating upd_rescalcs")
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_rescalcs'),
                                ResCalcFields)
    ResRows = arcpy.SearchCursor('upd_reslanduses',
                                 where_clause=r"TSCode = '" + TSCode + r"'")
    for ResRow in ResRows:
        LUCode = ResRow.getValue('LUCode')
        AcrePerUnit = ResRow.getValue('AcPerUnit')
        PerVacant = ResRow.getValue('PctVacantUnits')
        PerOtherSpace = ResRow.getValue('PctOther')
        AcrePerUnit = AcrePerUnit / (1 - PerOtherSpace)
        AcrePerOccUnit = AcrePerUnit / (1 - PerVacant)

        cur.insertRow((TSCode, LUCode, AcrePerUnit, AcrePerOccUnit))
    del cur
Пример #15
0
 def __init__(self, secret_key, public_key, parent_dir, time_window):
     Huobi_Connection_Utils.SECRET_KEY = secret_key
     Huobi_Connection_Utils.ACCESS_KEY = public_key
     self.client = Client
     self.logger = log.Logger("[HUOBI]")
     self.parent_dir = parent_dir
     self.time_window = self.HUOBI_TIME_WINDOW_DICT[time_window]
Пример #16
0
def CalcGPAvail(mpval):
    """
    Calculate the availablity of each polygon to each land use based on the time series and general plan. Return as a numpy array.
    
    Called by:
    CalcGPAvailablity
    
    Arguments:
    mpval: a list with [UPConfig,TimeStep,lu]
    
    """

    UPConfig = mpval[0]
    TimeStep = mpval[1]
    lu = mpval[2]

    Logger("Calculating GP Availablity for: {lu}".format(lu=lu))

    array = arcpy.da.TableToNumPyArray(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     'up_bg_gp_{ts}'.format(ts=TimeStep[0])), ["*"])
    array = AddNumpyField(array, [('gp_{lu}'.format(lu=lu), 'i4')])
    for ln in array:
        if ln[UPConfig[TimeStep[0]]['gp'][1]] in UPConfig[
                TimeStep[0]]['gplu'][lu]:
            ln['gp_{lu}'.format(lu=lu)] = 1
        else:
            ln['gp_{lu}'.format(lu=lu)] = 0

    keepFields = [str(UPConfig['BaseGeom_id']), 'gp_{lu}'.format(lu=lu)]
    array = array[keepFields].copy()
    return (array, TimeStep, lu)
Пример #17
0
def MakeUPDistTable(UPConfig):
    '''
    Create an empty table to hold distance data in the database. 
    Only call this if you want to create a new table. This
    function is not intended to overwite exising versions. 
    
    Called By:
    
    Calls:
    
    Arguments:
    UPConfig
    
    '''

    if not arcpy.Exists(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances')):
        Logger("Creating New up_distances table")
        arcpy.env.overwriteOutput = False
        arcpy.CreateTable_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname']), 'up_distances')
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            'attracter', 'TEXT', "", "", 50)
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            UPConfig['BaseGeom_id'], 'LONG')
        arcpy.AddField_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            'distance', 'DOUBLE')
        arcpy.AddIndex_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            'attracter', 'attracter_idx')
        arcpy.AddIndex_management(
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_distances'),
            UPConfig['BaseGeom_id'], "_".join([UPConfig['BaseGeom_id'],
                                               'idx']))
        Logger("Created New up_distances table")
    else:
        Logger("up_distances table already exists, skipping")
Пример #18
0
 def testVector(self):
     vector = Vector(-1, -2, 3.5)
     other = Vector(1, 2, 3)
     logger = Logger.getLogger()
     logger.info("Test create vector : %s" % vector)
     logger.info("Test normalize : %s" % vector.normalize())
     logger.info("Test dot : %s" % vector.dot(other))
     logger.info("Test cross : %s" % vector.cross(other))
Пример #19
0
def MakeNewRunGDB(dbpath, dbname):
    '''
    Create new run file geodatabase and create the empty configuration tables.
    
    Arguments:
    dbpath: the path at which to create the geodatabase
    dbname: the name of the run database
    '''

    # create geodatabase
    Logger("Creating File Geodatabase")
    try:
        arcpy.CreateFileGDB_management(dbpath, dbname)
        arcpy.env.workspace = os.path.join(dbpath, dbname)
    except Exception, e:
        Logger(str(e))
        raise
Пример #20
0
def PopulateSAResTable(db, TSCode, SubAreas, SAResInputs):
    '''
    SubAreas = List of SubAreaCodes
        * Example List = ['sa1','sa2']
    SAResInputs = A dictionary that contains the percent of households going into each residential land use type by SubArea
        * Example Dictionary = {'sa1': {'rl': 0.05, 'rm': 0.85, 'rh': 0.1},'sa2': {'rl': 0.2, 'rm': 0.8, 'rh': 0}}
    '''

    #create the upd_subareares table
    SAResFields = [
        'TSCode', 'SACode', 'LUCode', 'PctRes', 'OccUnits', 'AcresDemand'
    ]
    SAResFieldTypes = {
        'TSCode': 'TEXT',
        'SACode': 'TEXT',
        'LUCode': 'TEXT',
        'PctRes': 'DOUBLE',
        'OccUnits': 'DOUBLE',
        'AcresDemand': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_subareares', SAResFields, SAResFieldTypes,
                       TSCode)

    Logger("Populating upd_subareares")
    #calculate occupied units and gross acres demanded by subarea
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_subareares'),
                                SAResFields)

    for SubArea in SubAreas:
        #get the number of Households in this SubArea
        SADemandRows = arcpy.SearchCursor('upd_subareademand',
                                          where_clause=r"SACode = '" +
                                          SubArea + r"'")
        SADemandRow = SADemandRows.next()
        NumSAHH = SADemandRow.getValue('NumHH')

        ResLURows = arcpy.SearchCursor('upc_lu',
                                       where_clause=r"LUType = 'res'")
        for ResLURow in ResLURows:
            ResLU = ResLURow.getValue('Code')

            #get GrossAcrePerOccUnit Value for this land use
            ConversionRows = arcpy.SearchCursor('upd_rescalcs',
                                                where_clause=r"LUCode = '" +
                                                ResLU + r"' AND TSCode = '" +
                                                TSCode + r"'")
            ConversionRow = ConversionRows.next()
            GrossAcrePerOccUnit = ConversionRow.getValue('GrossAcPerOccUnit')

            #get number of households by LU type
            OccHH = NumSAHH * SAResInputs[SubArea][ResLU]
            ResAcresByLU = OccHH * GrossAcrePerOccUnit

            cur.insertRow((TSCode, SubArea, ResLU, SAResInputs[SubArea][ResLU],
                           OccHH, ResAcresByLU))
    del cur
Пример #21
0
def CreateUPCTables(UPConfig, UPCTables, UPCTableFields):
    '''
    Creates the upc_* tables
    
    Called by:
    WriteUPConfigToGDB
    
    Arguments:
    UPConfig: default UPlan configuration database
    UPCTables: A list of the table names to be created
    UPCTableFields: A dictionary that holds the fields to be created for each table
    
    Returns: None
    '''

    #set the workspace
    InWorkspace = os.path.join(UPConfig['paths']['dbpath'],
                               UPConfig['paths']['dbname'])
    arcpy.env.workspace = InWorkspace

    #field types
    FieldTypes = {
        'KeyName': 'TEXT',
        'KeyValue': 'TEXT',
        'TSOrder': 'SHORT',
        'Code': 'TEXT',
        'Name': 'TEXT',
        'GPLayer': 'TEXT',
        'GPField': 'TEXT',
        'LUType': 'TEXT',
        'AllocMethod': 'TEXT',
        'Priority': 'SHORT',
        'TSCode': 'TEXT',
        'SACode': 'TEXT',
        'LUCode': 'TEXT',
        'Acres': 'DOUBLE',
        'GPCat': 'TEXT',
        'CLayer': 'TEXT',
        'Weight': 'DOUBLE',
        'AttLayer': 'TEXT',
        'Dist': 'DOUBLE',
        'FCName': 'TEXT',
        'LongName': 'TEXT',
        'DateAdded': 'DATE',
        'Role': 'TEXT'
    }

    #Create tables (if needed)
    for UPCTable in UPCTables:
        if not arcpy.Exists(UPCTable):
            #create the table
            Logger("Creating Table: " + UPCTable)
            arcpy.CreateTable_management(InWorkspace, UPCTable)
            #add the field(s)
            for Field in UPCTableFields[UPCTable]:
                arcpy.AddField_management(UPCTable, Field, FieldTypes[Field])
Пример #22
0
def PopulateSAEmpTable(db, TSCode, SubAreas, SAEmpInputs):
    '''
    SubAreas = List of SubAreaCodes
        * Example List = ['sa1','sa2']
    SAEmpInputs = A dictionary that contains the percent of employees going into each employment land use type by SubArea
        * Example Dictionary = {'sa1': {'ind': 0.1, 'ch': 0.3, 'cl': 0.6},'sa2': {'ind': 0.5, 'ch': 0.2, 'cl': 0.3}}
    '''
    #create the upd_subareaemp table
    SAEmpFields = [
        'TSCode', 'SACode', 'LUCode', 'PctEmp', 'NumEmp', 'AcresDemand'
    ]
    SAEmpFieldTypes = {
        'TSCode': 'TEXT',
        'SACode': 'TEXT',
        'LUCode': 'TEXT',
        'PctEmp': 'DOUBLE',
        'NumEmp': 'DOUBLE',
        'AcresDemand': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_subareaemp', SAEmpFields, SAEmpFieldTypes,
                       TSCode)

    Logger("Populating upd_subareaemp")
    #calculate number of employees and acres demanded by subarea and save to upd_subareaemp
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_subareaemp'),
                                SAEmpFields)

    for SubArea in SubAreas:
        #get the number of employees in this SubArea
        SADemandRows = arcpy.SearchCursor('upd_subareademand',
                                          where_clause=r"SACode = '" +
                                          SubArea + r"'")
        SADemandRow = SADemandRows.next()
        NumSAEmp = SADemandRow.getValue('NumEmp')

        EmpLURows = arcpy.SearchCursor('upc_lu',
                                       where_clause=r"LUType = 'emp'")
        for EmpLURow in EmpLURows:
            EmpLU = EmpLURow.getValue('Code')

            #get number of employees in this LU type
            NumEmpByLU = NumSAEmp * SAEmpInputs[SubArea][EmpLU]

            #convert number of employees to space needed
            EmpAcresRows = arcpy.SearchCursor('upd_empcalcs',
                                              where_clause=r"LUCode = '" +
                                              EmpLU + r"' AND TSCode = '" +
                                              TSCode + r"'")
            EmpAcresRow = EmpAcresRows.next()
            GrossAcrePerEmp = EmpAcresRow.getValue('GrossAcPerEmp')

            EmpAcresByLU = NumEmpByLU * GrossAcrePerEmp

            cur.insertRow((TSCode, SubArea, EmpLU, SAEmpInputs[SubArea][EmpLU],
                           NumEmpByLU, EmpAcresByLU))
    del cur
Пример #23
0
def CalcGPAvailablity(TimeStep, UPConfig, runMulti=True):
    '''
    Create A table with the parcel id, gp category, and permissions for each land use for the parcel.
    
    Called by:
    CalcGP
    
    Calls:
    CalcGPAvail
    Utilities.MergeArrays
    
    Arguments:
    Timestep: which time step is being processed
    UPconfig: the primary settings configuration object
    '''

    mplist = []
    for lu in UPConfig['LUPriority']:
        # prep mpvals
        mplist.append([UPConfig, TimeStep, lu])

    if runMulti == True:
        Logger("Starting Multiprocess: Calculating GPAvailablity")
        pool = multiprocessing.Pool(multiprocessing.cpu_count() - 1)
        results_array = pool.map(CalcGPAvail, mplist)
        pool.close()
        pool.join()
        Logger("End Multiprocess")

    else:
        results_array = []
        for mpval in mplist:
            results_array.append(CalcGPAvail(mpval))

    arrlist = [el[0] for el in results_array]
    array = MergeArrays(arrlist, UPConfig['BaseGeom_id'])
    arcpy.da.NumPyArrayToTable(
        array,
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     'up_bg_gp_avail_{ts}'.format(ts=TimeStep[0])))
Пример #24
0
def CalcSAInt(UPConfig):
    '''
    Create a table with the BaseGeom_id and the Subarea_id
    
    Called By:
    main
    
    Calls:
    Utilities.MakeNumpyView
    Utilities.AddNumpyField
    
    Arguments:
    UPconfig: the primary settings configuration object
    '''
    arcpy.env.workspace = os.path.join(UPConfig['paths']['dbpath'],
                                       UPConfig['paths']['dbname'])

    if len(UPConfig['Subareas']) > 1:
        Logger("Intersecting SubAreas")
        arcpy.SpatialJoin_analysis(UPConfig['BaseGeom_cent'],
                                   UPConfig['Subarea_bnd'], 'up_sa_bg_t')
        array = arcpy.da.TableToNumPyArray('up_sa_bg_t', ["*"])
        keepFields = [UPConfig['BaseGeom_id'], UPConfig['Subarea_id']]
        array = MakeNumpyView(array, keepFields)
        arcpy.da.NumPyArrayToTable(array, 'up_sa_bg')
        Logger("SubAreas Intersected")
    else:
        Logger("Setting Single SubArea")
        array = arcpy.da.TableToNumPyArray(UPConfig['BaseGeom_cent'], ["*"])
        keepFields = [UPConfig['BaseGeom_id']]
        array = MakeNumpyView(array, keepFields)
        array = AddNumpyField(array, [('Subarea_id', 'a8')])
        array = DropNumpyField(array, 'f0')
        for ln in array:
            ln['Subarea_id'] = UPConfig['Subareas'][0]
        arcpy.da.NumPyArrayToTable(
            array,
            os.path.join(UPConfig['paths']['dbpath'],
                         UPConfig['paths']['dbname'], 'up_sa_bg'))
        Logger("SubArea set")
Пример #25
0
def CalcGP(UPConfig):
    '''
    Make tables with the parcel id, gp category, and permissions for each land use for the parcel, buy TimeStep

    Called by:
    main
    
    Calls:
    CalcGPInts
    CalcGPAvailability
    
    Arguments:
    UPconfig: the primary settings configuration object

    
    '''
    Logger("Processing General Plans")
    arcpy.env.workspace = os.path.join(UPConfig['paths']['dbpath'],
                                       UPConfig['paths']['dbname'])
    for ts in UPConfig['TimeSteps']:
        Logger("Working on Time Step: {ts}".format(ts=ts[1]))
        CalcGPInt(ts, UPConfig)
        CalcGPAvailablity(ts, UPConfig, False)

    Logger("General Plan Processing Complete")


# if __name__ == "__main__":
#     Logger("Running GP Calcs")
#     dbpath = r"G:\Public\UPLAN\Amador"
#     dbname = "Run_4_Recreate.gdb"
#     UPConfig2 = UPConfig.ReadUPConfigFromGDB(dbpath,dbname)
#
#     UPCleanup_GeneralPlans(UPConfig2)
#     CalcGP(UPConfig2)
#
#     Logger("GP Calcs Finished")
Пример #26
0
def CalcGPInt(TimeStep, UPConfig):
    '''
    Create a feature class with the general plan category and the polygon id for the specified timestep
    
    Called By:
    CalcGP
    
    
    Arguments:
    Timestep: which time step is being processed
    UPconfig: the primary settings configuration object
    
    '''

    #TODO: Convert to multiprocess

    Logger("Intersecting General Plan")
    arcpy.SpatialJoin_analysis(UPConfig['BaseGeom_cent'],
                               UPConfig[TimeStep[0]]['gp'][0],
                               'up_bg_gp_{ts}'.format(ts=TimeStep[0]))

    #delete any datetime fields - creating an array later will fail if not
    DateFields = arcpy.ListFields('up_bg_gp_{ts}'.format(ts=TimeStep[0]), '*',
                                  'Date')
    if len(DateFields) != 0:
        DeleteFields = []
        for DateField in DateFields:
            DeleteFields.append(DateField.name)
        arcpy.DeleteField_management('up_bg_gp_{ts}'.format(ts=TimeStep[0]),
                                     DeleteFields)

    arcpy.AddIndex_management('up_bg_gp_{ts}'.format(ts=TimeStep[0]),
                              UPConfig['BaseGeom_id'],
                              'idx_bg_gp_pclid_{ts}'.format(ts=TimeStep[0]),
                              'UNIQUE', 'ASCENDING')

    Logger("General Plan Intersected")
Пример #27
0
def CreateOrEmptyTable(db, TableName, Fields, FieldTypes, TSCode):
    '''
    If the given table doesn't exist, this function will create the table.
    If the given table does exist, all rows will be removed for the given timestep
    
    Called by: CalculateDemand
    
    Arguments:
    db: full path to the uplan file geodatabase
    TableName: The name of the table
    Fields: A list containing the field names
    FieldTypes: A Dictionary that defines the field types by the field names
    TSCode = Timestep Code
    
    '''
    arcpy.env.workspace = db

    if not arcpy.Exists(TableName):
        #create the table
        Logger("Creating Table: " + TableName)
        arcpy.CreateTable_management(db, TableName)
        #add the field(s)
        for Field in Fields:
            arcpy.AddField_management(TableName, Field, FieldTypes[Field])
    else:
        #erase the rows for the given timestep
        Logger("Emptying Table: " + TableName + " for Timestep: " + TSCode)

        tempTableView = TableName + "TableView"
        if arcpy.Exists(tempTableView):
            arcpy.Delete_management(tempTableView)
        arcpy.MakeTableView_management(TableName, tempTableView)
        arcpy.SelectLayerByAttribute_management(tempTableView, 'NEW_SELECTION',
                                                r"TSCode = '" + TSCode + r"'")
        if int(arcpy.GetCount_management(tempTableView).getOutput(0)) > 0:
            arcpy.DeleteRows_management(tempTableView)
Пример #28
0
def PriAlloc(UPConfig, TimeStep, demand, cumAlloc, tsAlloc):
    """
    Manages the primary allocation for a TimeStep.
    
    How it works: 
    1. Create the devSpaceTable (using au.MakeDevSpace) which contains the amount of unconstrained space in each polygon for each land use.
    2. Pulls the land use demand values from UPConfig
    3. Loops through the land uses in the priority order handing them to PriAllocLU for allocation.
    4. Accept the results from PriAllocLU and get it ready for the next land use.
    
    
    Called By:
    AllocateTimeStep
    
    Calls:
    au.MakeDevSpace
    PriAllocLU
    
    Arguments:
    UPConfig
    TimeStep
    cumAlloc: cumulative allocation table
    tsAlloc: timestep allocation table
    """

    Logger("Allocation for TimeStep: {ts}".format(ts=TimeStep[1]))
    # Calculate Developable Space
    devSpaceTable = au.MakeDevSpace(UPConfig, TimeStep)
    #     devSpaceTable.to_csv(r"G:\Public\UPLAN\Uplan4\testing\TestAllocation\temp\devSpaceTable.csv")

    # reDevTracker
    reDevTracker = [0, 0]

    # Basic Demand
    #     demand = UPConfig[TimeStep[0]]['Demand']

    # Loop Land Uses
    for lu in UPConfig['LUPriority']:
        result = PriAllocLU(UPConfig, TimeStep, lu, demand, cumAlloc, tsAlloc,
                            devSpaceTable, reDevTracker)
        cumAlloc = result[0]
        tsAlloc = result[1]
        demand = result[2]
        reDevTracker = result[3]

    NoSpace = au.CalcNoSpace(UPConfig, demand)

    return ([cumAlloc, tsAlloc, demand, NoSpace, reDevTracker])
Пример #29
0
def PopulateSADemandTable(db, TSCode, SubAreas, SADemandInputs):
    '''
    SubAreas = List of SubAreaCodes
        * Example List = ['sa1','sa2']
    SADemandInputs = A dictionary that contains percent of population and percent of employment by SubArea
        * The Key Name for percent population = 'PercentPop'
        * The Key Name for percent employment = 'PercentEmp'
        * Example Dictionary = {'sa1':{'PercentPop':0.6, 'PercentEmp':0.9},'sa2':{'PercentPop':0.4, 'PercentEmp':0.1}}
    '''

    #create the upd_subareademand table
    SADemandFields = [
        'TSCode', 'SACode', 'PctRes', 'PctEmp', 'NumHH', 'NumEmp'
    ]
    SADemandFieldTypes = {
        'TSCode': 'TEXT',
        'SACode': 'TEXT',
        'PctRes': 'DOUBLE',
        'PctEmp': 'DOUBLE',
        'NumHH': 'DOUBLE',
        'NumEmp': 'DOUBLE'
    }
    CreateOrEmptyTable(db, 'upd_subareademand', SADemandFields,
                       SADemandFieldTypes, TSCode)

    Logger("Populating upd_subareademand")
    #calculate the number of households and employees by subarea and save to upd_subareademand table
    cur = arcpy.da.InsertCursor(os.path.join(db, 'upd_subareademand'),
                                SADemandFields)

    #get population change, PPHH, and EPHH from Demographics table
    DemoRows = arcpy.SearchCursor('upd_demographics',
                                  where_clause=r"TSCode = '" + TSCode + r"'")
    DemoRow = DemoRows.next()
    PopChange = DemoRow.getValue('PopChange')
    PPHH = DemoRow.getValue('PPHH')
    EPHH = DemoRow.getValue('EPHH')

    for SubArea in SubAreas:
        PercentPop = SADemandInputs[SubArea]['PercentPop']
        PercentEmp = SADemandInputs[SubArea]['PercentEmp']
        NumHH = PercentPop * (PopChange / PPHH)
        NumEmp = PercentEmp * (PopChange / PPHH) * EPHH

        cur.insertRow((TSCode, SubArea, PercentPop, PercentEmp, NumHH, NumEmp))
    del cur
Пример #30
0
 def get_population(self, code, year, given_population):
     code_without_year = code[0:5]
     try:
         pop_this_year = float(self.counties[code_without_year][year])
         pop_2010 = float(self.counties[code_without_year]['2010'])
         ratio = (pop_this_year / pop_2010)
         return float(given_population) * ratio
     except (KeyError):
         # set up personal logger
         logger = Logger()
         current_path = os.getcwd()
         logger.define_issue_log(os.path.join(current_path, 'files/issues.log'))
         logger.record_issue('No population data for county:', code)
         return given_population
Пример #31
0
import os
import re
import traceback
from multiprocessing import Queue, Pipe

# logger
from Utilities import Logger, AutoEnum

logger = Logger.getLogger('Command Logger', 'logs', False)


#
# UTIL : call stack function for log
#
reTraceStack = re.compile("File \"(.+?)\", line (\d+?), .+")  # [0] filename, [1] line number


def getTraceCallStack():
    for line in traceback.format_stack()[::-1]:
        m = re.match(reTraceStack, line.strip())
        if m:
            filename = m.groups()[0]
            # ignore case
            if filename == __file__:
                continue
            filename = os.path.split(filename)[1]
            lineNumber = m.groups()[1]
            return "[%s:%s]" % (filename, lineNumber)
    return ""

Пример #32
0
# logger
from Utilities import Logger
logger = Logger.getLogger('UI Logger', 'logs', False)


from .MainWindow import run_editor
Пример #33
0
# default logger
from Utilities import Logger
logger = Logger.getLogger('default', 'logs', False)

# config
from Configure import Config
config = Config("Config.ini")

from .Command import *
from .CoreManager import *