complex_model/OasisToRF.py
def create_rf_input(item_source, coverage_source, sqlite_fp, risk_platform_data):
    """This function populates Risk Frontiers exposure and coverage database from oasis generated input files.
    Precondition: The number of rows in item_source and coverage_source must be exactly the same.

    :param item_source: the items.csv as a dataframe.
    :param coverage_source: the coverages.csv as a dataframe
    :param sqlite_fp: path to store the sqlite database containing the exposure and coverage tables
    :param risk_platform_data: path containing the template databases for Risk Frontiers models
    :return: a number of rows in the items and coverages.
    """
    num_items = len(item_source)
    num_coverages = len(coverage_source)
    if not num_items == num_coverages:
        raise Exception("the items.csv and coverage.csv must have the exact same number of rows")

    if os.path.isfile(sqlite_fp):
        os.remove(sqlite_fp)
    copyfile(os.path.join(risk_platform_data, DEFAULT_DB), sqlite_fp)

    con = sqlite3.connect(sqlite_fp)
    cur = con.cursor()

    cur.execute("CREATE TABLE u_exposure_tmp (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF[col]["datatype"] for col in RF_DEFAULT_ITEM_SQLITE_DEF]) + ");")
    cur.execute("CREATE TABLE u_exposure (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF[col]["datatype"] for col in RF_DEFAULT_ITEM_SQLITE_DEF]) + ");")
    cur.execute("CREATE TABLE u_coverage (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_COVERAGE_SQLITE_DEF[col]["datatype"] for col in
         RF_DEFAULT_COVERAGE_SQLITE_DEF]) + ");")

    origin_file_line = 0
    items = []
    coverages = []
    for line_id in range(0, num_items):
        item_row = item_source.iloc[line_id]
        coverage_row = coverage_source.iloc[line_id]
        origin_file_line = line_id + 1
        # building item row
        rf_item = RF_DEFAULT_ITEM.copy()

        model_data = json.loads(item_row['model_data'])
        for key in rf_item.keys():
            if key in model_data and model_data[key] is not None:
                if key.lower() == "props":
                    rf_item[key] = json.dumps(model_data[key])
                else:
                    rf_item[key] = model_data[key]

        # if not rf_item['loc_id']:
        #     rf_item['loc_id'] = "rf_loc_" + str(model_data['loc_id'])
        rf_item['loc_id'] = str(item_row['item_id'])

        # building coverage row
        rf_coverage = RF_DEFAULT_COVERAGE.copy()

        rf_coverage['cover_id'] = int(model_data['cover_id'])
        rf_coverage['value'] = float(coverage_row['tiv'])

        rf_coverage['loc_id'] = rf_item['loc_id']

        # for oasis origin_file_line will be item_id/coverage_id
        rf_item['origin_file_line'] = origin_file_line
        rf_coverage['origin_file_line'] = origin_file_line

        items.append(tuple(rf_item.values()))
        coverages.append(tuple(rf_coverage.values()))

    item_sql = "INSERT INTO u_exposure_tmp VALUES (" + ",".join(["?" for c in RF_DEFAULT_ITEM]) + ");"
    coverage_sql = "INSERT INTO u_coverage VALUES (" + ",".join(["?" for c in RF_DEFAULT_COVERAGE]) + ");"
    cur.executemany(item_sql, items)
    cur.executemany(coverage_sql, coverages)
    con.commit()

    # spatial analysis ...
    fill_resolution_from_address_id(con, cur)
    fill_resolution_from_lat_long(con, cur)

    # post processing ...
    ofl_exposure_index = "CREATE INDEX ofl_exposure_index ON u_exposure (origin_file_line);"
    ofl_coverage_index = "CREATE INDEX ofl_coverage_index ON u_coverage (origin_file_line);"
    cur.execute(ofl_exposure_index)
    cur.execute(ofl_coverage_index)
    con.commit()

    con.close()
    return origin_file_line
This function populates Risk Frontiers exposure and coverage database from oasis generated input files.
    Precondition: The number of rows in item_source and coverage_source must be exactly the same.

    :param item_source: the items.csv as a dataframe.
    :param coverage_source: the coverages.csv as a dataframe
    :param sqlite_fp: path to store the sqlite database containing the exposure and coverage tables
    :param risk_platform_data: path containing the template databases for Risk Frontiers models
    :return: a number of rows in the items and coverages.
    
This function populates Risk Frontiers exposure and coverage database from oasis generated input files.
Precondition: The number of rows in item_source and coverage_source must be exactly the same.
This function populates Risk Frontiers exposure and coverage database from oasis generated input files . Precondition : The number of rows in item_source and coverage_source must be exactly the same .
def create_rf_input(item_source, coverage_source, sqlite_fp, risk_platform_data):
    num_items = len(item_source)
    num_coverages = len(coverage_source)
    if not num_items == num_coverages:
        raise Exception("the items.csv and coverage.csv must have the exact same number of rows")
    if os.path.isfile(sqlite_fp):
        os.remove(sqlite_fp)
    copyfile(os.path.join(risk_platform_data, DEFAULT_DB), sqlite_fp)
    con = sqlite3.connect(sqlite_fp)
    cur = con.cursor()
    cur.execute("CREATE TABLE u_exposure_tmp (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF[col]["datatype"] for col in RF_DEFAULT_ITEM_SQLITE_DEF]) + ");")
    cur.execute("CREATE TABLE u_exposure (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF[col]["datatype"] for col in RF_DEFAULT_ITEM_SQLITE_DEF]) + ");")
    cur.execute("CREATE TABLE u_coverage (" + ",".join(
        ["[" + col + "] " + RF_DEFAULT_COVERAGE_SQLITE_DEF[col]["datatype"] for col in
         RF_DEFAULT_COVERAGE_SQLITE_DEF]) + ");")
    origin_file_line = 0
    items = []
    coverages = []
    for line_id in range(0, num_items):
        item_row = item_source.iloc[line_id]
        coverage_row = coverage_source.iloc[line_id]
        origin_file_line = line_id + 1
        rf_item = RF_DEFAULT_ITEM.copy()
        model_data = json.loads(item_row['model_data'])
        for key in rf_item.keys():
            if key in model_data and model_data[key] is not None:
                if key.lower() == "props":
                    rf_item[key] = json.dumps(model_data[key])
                else:
                    rf_item[key] = model_data[key]
        rf_item['loc_id'] = str(item_row['item_id'])
        rf_coverage = RF_DEFAULT_COVERAGE.copy()
        rf_coverage['cover_id'] = int(model_data['cover_id'])
        rf_coverage['value'] = float(coverage_row['tiv'])
        rf_coverage['loc_id'] = rf_item['loc_id']
        rf_item['origin_file_line'] = origin_file_line
        rf_coverage['origin_file_line'] = origin_file_line
        items.append(tuple(rf_item.values()))
        coverages.append(tuple(rf_coverage.values()))
    item_sql = "INSERT INTO u_exposure_tmp VALUES (" + ",".join(["?" for c in RF_DEFAULT_ITEM]) + ");"
    coverage_sql = "INSERT INTO u_coverage VALUES (" + ",".join(["?" for c in RF_DEFAULT_COVERAGE]) + ");"
    cur.executemany(item_sql, items)
    cur.executemany(coverage_sql, coverages)
    con.commit()
    fill_resolution_from_address_id(con, cur)
    fill_resolution_from_lat_long(con, cur)
    ofl_exposure_index = "CREATE INDEX ofl_exposure_index ON u_exposure (origin_file_line);"
    ofl_coverage_index = "CREATE INDEX ofl_coverage_index ON u_coverage (origin_file_line);"
    cur.execute(ofl_exposure_index)
    cur.execute(ofl_coverage_index)
    con.commit()
    con.close()
    return origin_file_line
def create_rf_input ( item_source , coverage_source , sqlite_fp , risk_platform_data ) : num_items = len ( item_source ) num_coverages = len ( coverage_source ) if not num_items == num_coverages : raise Exception ( "the items.csv and coverage.csv must have the exact same number of rows" ) if os . path . isfile ( sqlite_fp ) : os . remove ( sqlite_fp ) copyfile ( os . path . join ( risk_platform_data , DEFAULT_DB ) , sqlite_fp ) con = sqlite3 . connect ( sqlite_fp ) cur = con . cursor ( ) cur . execute ( "CREATE TABLE u_exposure_tmp (" + "," . join ( [ "[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF [ col ] [ "datatype" ] for col in RF_DEFAULT_ITEM_SQLITE_DEF ] ) + ");" ) cur . execute ( "CREATE TABLE u_exposure (" + "," . join ( [ "[" + col + "] " + RF_DEFAULT_ITEM_SQLITE_DEF [ col ] [ "datatype" ] for col in RF_DEFAULT_ITEM_SQLITE_DEF ] ) + ");" ) cur . execute ( "CREATE TABLE u_coverage (" + "," . join ( [ "[" + col + "] " + RF_DEFAULT_COVERAGE_SQLITE_DEF [ col ] [ "datatype" ] for col in RF_DEFAULT_COVERAGE_SQLITE_DEF ] ) + ");" ) origin_file_line = 0 items = [ ] coverages = [ ] for line_id in range ( 0 , num_items ) : item_row = item_source . iloc [ line_id ] coverage_row = coverage_source . iloc [ line_id ] origin_file_line = line_id + 1 rf_item = RF_DEFAULT_ITEM . copy ( ) model_data = json . loads ( item_row [ 'model_data' ] ) for key in rf_item . keys ( ) : if key in model_data and model_data [ key ] is not None : if key . lower ( ) == "props" : rf_item [ key ] = json . dumps ( model_data [ key ] ) else : rf_item [ key ] = model_data [ key ] rf_item [ 'loc_id' ] = str ( item_row [ 'item_id' ] ) rf_coverage = RF_DEFAULT_COVERAGE . copy ( ) rf_coverage [ 'cover_id' ] = int ( model_data [ 'cover_id' ] ) rf_coverage [ 'value' ] = float ( coverage_row [ 'tiv' ] ) rf_coverage [ 'loc_id' ] = rf_item [ 'loc_id' ] rf_item [ 'origin_file_line' ] = origin_file_line rf_coverage [ 'origin_file_line' ] = origin_file_line items . append ( tuple ( rf_item . values ( ) ) ) coverages . append ( tuple ( rf_coverage . values ( ) ) ) item_sql = "INSERT INTO u_exposure_tmp VALUES (" + "," . join ( [ "?" for c in RF_DEFAULT_ITEM ] ) + ");" coverage_sql = "INSERT INTO u_coverage VALUES (" + "," . join ( [ "?" for c in RF_DEFAULT_COVERAGE ] ) + ");" cur . executemany ( item_sql , items ) cur . executemany ( coverage_sql , coverages ) con . commit ( ) fill_resolution_from_address_id ( con , cur ) fill_resolution_from_lat_long ( con , cur ) ofl_exposure_index = "CREATE INDEX ofl_exposure_index ON u_exposure (origin_file_line);" ofl_coverage_index = "CREATE INDEX ofl_coverage_index ON u_coverage (origin_file_line);" cur . execute ( ofl_exposure_index ) cur . execute ( ofl_coverage_index ) con . commit ( ) con . close ( ) return origin_file_line
This function populates Risk Frontiers exposure and coverage database from oasis generated input files.
This function populates Risk Frontiers exposure and coverage database from oasis generated input files .
"""This function populates Risk Frontiers exposure and coverage database from oasis generated input files.
    Precondition: The number of rows in item_source and coverage_source must be exactly the same.

    :param item_source: the items.csv as a dataframe.
    :param coverage_source: the coverages.csv as a dataframe
    :param sqlite_fp: path to store the sqlite database containing the exposure and coverage tables
    :param risk_platform_data: path containing the template databases for Risk Frontiers models
    :return: a number of rows in the items and coverages.
    """ # building item row # if not rf_item['loc_id']: #     rf_item['loc_id'] = "rf_loc_" + str(model_data['loc_id']) # building coverage row # for oasis origin_file_line will be item_id/coverage_id # spatial analysis ... # post processing ...
a number of rows in the items and coverages.
a number of rows in the items and coverages .
item_source
the items.csv as a dataframe.
the items . csv as a dataframe .
coverage_source
the coverages.csv as a dataframe
the coverages . csv as a dataframe
sqlite_fp
path to store the sqlite database containing the exposure and coverage tables
path to store the sqlite database containing the exposure and coverage tables
risk_platform_data
path containing the template databases for Risk Frontiers models
path containing the template databases for Risk Frontiers models