Example #1
0
def confirm_date(config, datestring) : 
    """copy points (active fire and confirmed burn points to fire_events table"""
    db_date = db_date_string(image_date_time(datestring))
    print db_date

    vt.execute_active_fire_2_events(config, db_date)
    vt.execute_threshold_2_events(config, db_date)
Example #2
0
def confirm_date(config, datestring):
    """copy points (active fire and confirmed burn points to fire_events table"""
    db_date = db_date_string(image_date_time(datestring))
    print db_date

    vt.execute_active_fire_2_events(config, db_date)
    vt.execute_threshold_2_events(config, db_date)
Example #3
0
def create_fire_events_raster(config,
                              tbl,
                              gt_schema,
                              rast_table,
                              geom_table,
                              filt_dist=-1,
                              filter_table=None,
                              result_tbl=None):
    """dumps the ground truth fire mask to disk, overwrites by rasterizing fire_events, 
    reloads the table to postgis
    This ensures that the result is aligned to the specified ground truth 
    table."""

    query = "SELECT viirs_rasterize_375_mindoy('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
        config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)

    query = "SELECT viirs_rasterize_750_mindoy('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
        config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)

    if result_tbl is None:
        result_tbl = 'rast_doy'
    query = "SELECT viirs_rasterize_merge_doy('{0}', '{1}')".format(
        config.DBschema, result_tbl)
    vt.execute_query(config, query)

    query = 'DROP TABLE IF EXISTS "{0}".{1}'.format(config.DBschema,
                                                    result_tbl)
    vt.execute_query(config, query)

    query = 'SELECT rid, {1} as rast INTO "{0}".{1} FROM "{0}".fire_events_raster'.format(
        config.DBschema, result_tbl)
    vt.execute_query(config, query)
Example #4
0
def zonetbl_run(zone_schema,
                zonedef_tbl,
                zone_tbl,
                zone_col,
                config,
                nearest=False):
    """collects fire events raster points onto the zone results table for
    a single run, optionally recalculating the fire events raster
    zone_schema.zonedef_tbl  : names the zone definition table
    zone_col                 : names the zone column in the def table
    zone_schema.zone_tbl     : names the zone results table
    gt_schema.gt_table       : names the ground truth raster (for alignment)
    config                   : connection/run specific information
    
    This function will call one of two stored procedures in the database: 
        * viirs_zonetbl_run; or 
        * viirs_nearest_zonetbl_run
    depending on the value of 'nearest'.
    """
    run_schema = config.DBschema

    if nearest:
        function_name = 'viirs_nearest_zonetbl_run'
    else:
        function_name = 'viirs_zonetbl_run'

    query = "SELECT {5}('{0}','{1}','{2}','{3}','{4}')".format(
        zone_schema, zone_tbl, zonedef_tbl, run_schema, zone_col,
        function_name)
    vt.execute_query(config, query)
Example #5
0
def project_fire_events_nlcd(config):
    """Creates a new geometry column in the fire_events table and project to
    NLCD coordinates."""

    query = "SELECT viirs_nlcd_geom('{0}', 'fire_events', {1})".format(
        config.DBschema, vt.srids["NLCD"])
    vt.execute_query(config, query)
Example #6
0
def create_fire_events_raster(config,
                              tbl,
                              gt_schema,
                              rast_table,
                              geom_table,
                              filt_dist=-1,
                              filter_table=None):
    """dumps the ground truth fire mask to disk, overwrites by rasterizing fire_events, 
    reloads the table to postgis
    This ensures that the result is aligned to the specified ground truth 
    table."""

    query = "SELECT viirs_rasterize_375('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
        config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)

    query = "SELECT viirs_rasterize_750('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
        config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)

    if filter_table is None:
        query = "SELECT viirs_rasterize_merge('{0}', 'rast')".format(
            config.DBschema)
        vt.execute_query(config, query)
    else:
        query = "SELECT viirs_rasterize_merge('{0}', 'merged_rast')".format(
            config.DBschema)
        vt.execute_query(config, query)

        query = "SELECT viirs_rasterize_filter('{0}','merged_rast','{1}','{2}')".format(
            config.DBschema, gt_schema, filter_table)
        vt.execute_query(config, query)
Example #7
0
def create_fire_events_raster(config, tbl, 
                              gt_schema, rast_table, geom_table, 
                              filt_dist=-1,
                              filter_table=None) : 
    """dumps the ground truth fire mask to disk, overwrites by rasterizing fire_events, 
    reloads the table to postgis
    This ensures that the result is aligned to the specified ground truth 
    table."""

    query = "SELECT viirs_rasterize_375('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
          config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)

    query = "SELECT viirs_rasterize_750('{0}', '{1}', '{2}', '{3}', '{4}', {5})".format(
          config.DBschema, tbl, gt_schema, rast_table, geom_table, filt_dist)
    vt.execute_query(config, query)
    
    if filter_table is None  : 
        query = "SELECT viirs_rasterize_merge('{0}', 'rast')".format(config.DBschema)
        vt.execute_query(config, query)
    else : 
        query = "SELECT viirs_rasterize_merge('{0}', 'merged_rast')".format(config.DBschema)
        vt.execute_query(config, query)
        
        query = "SELECT viirs_rasterize_filter('{0}','merged_rast','{1}','{2}')".format(
              config.DBschema, gt_schema, filter_table)
        vt.execute_query(config, query)
Example #8
0
def extract_fire_mask(config,
                      gt_schema,
                      gt_table,
                      rast_col='rast',
                      geom_col='geom'):
    """Creates a geometry column and populates with pixel centers where pixel value=1"""
    query = "SELECT viirs_get_mask_pts('{0}', '{1}', '{2}', '{3}', {4})".format(
        gt_schema, gt_table, rast_col, geom_col, vt.srids['NLCD'])
    vt.execute_query(config, query)
Example #9
0
def create_events_view(config,year) : 
    """creates a view of the fire_events table, only showing 2013 data."""
    view_name = 'fire_events_{0}'.format(year)
    query="""CREATE OR REPLACE VIEW "{0}".{1} AS
          SELECT * FROM "{0}".fire_events
          WHERE collection_date BETWEEN '{2}-01-01' AND '{3}-01-01'
          """.format(config.DBschema, view_name,year,year+1)

    vt.execute_query(config, query)
    return view_name
Example #10
0
def mask_points(config):
    """apply landcover mask to active_fire and threshold burned"""
    vt.execute_query(
        config,
        "SELECT viirs_mask_points('{0}','active_fire','landmask','noburn','geom')"
        .format(config.DBschema))
    vt.execute_query(
        config,
        "SELECT viirs_mask_points('{0}','threshold_burned','landmask','noburn','geom')"
        .format(config.DBschema))
Example #11
0
def create_events_view(config, year):
    """creates a view of the fire_events table, only showing 2013 data."""
    view_name = 'fire_events_{0}'.format(year)
    query = """CREATE OR REPLACE VIEW "{0}".{1} AS
          SELECT * FROM "{0}".fire_events
          WHERE collection_date BETWEEN '{2}-01-01' AND '{3}-01-01'
          """.format(config.DBschema, view_name, year, year + 1)

    vt.execute_query(config, query)
    return view_name
Example #12
0
def mask_sum(config, gt_schema, gt_table):
    """adds the mask values from the fire_events_raster to the values in the ground truth raster.
    
    The fire events raster is in config.DBschema. The ground truth raster to use
    is provided in gt_schema and gt_table. If the supplied masks have only 0 and 1
    in them, as they should, then the sum raster should have only 0, 1, and 2.
    The logical "or" function between the two masks is the set of pixels having a
    nonzero value. The logical "and" function is the set of pixels having the value
    two."""
    query = "SELECT viirs_mask_sum('{0}', '{1}', '{2}')".format(
        config.DBschema, gt_schema, gt_table)
    vt.execute_query(config, query)
Example #13
0
def mask_sum(config, gt_schema, gt_table) : 
    """adds the mask values from the fire_events_raster to the values in the ground truth raster.
    
    The fire events raster is in config.DBschema. The ground truth raster to use
    is provided in gt_schema and gt_table. If the supplied masks have only 0 and 1
    in them, as they should, then the sum raster should have only 0, 1, and 2.
    The logical "or" function between the two masks is the set of pixels having a
    nonzero value. The logical "and" function is the set of pixels having the value
    two."""
    query = "SELECT viirs_mask_sum('{0}', '{1}', '{2}')".format(
         config.DBschema, gt_schema, gt_table)
    vt.execute_query(config,query)
Example #14
0
def copy_data(configs, master_schema='master') : 
    """Copies the active_fire and threshold_burned tables from the master schema to this one."""
    af_columns = 'fid, latitude, longitude, collection_date, geom, event_fid, pixel_size, band_i_m, masked, geom_nlcd'
    tb_columns = 'fid, latitude, longitude, collection_date, geom, confirmed_burn, pixel_size, band_i_m, masked, geom_nlcd'

    copy_query = 'INSERT INTO "{tgt_schema}".{table}({columns}) SELECT {columns} FROM "{master_schema}".{table}'

    for cfg in configs : 
        # copy active fire
        vt.execute_query(cfg, copy_query.format(tgt_schema=cfg.DBschema,
              table='active_fire', columns=af_columns, master_schema=master_schema))
        # copy threshold_burned
        vt.execute_query(cfg, copy_query.format(tgt_schema=cfg.DBschema,
              table='threshold_burned', columns=tb_columns, master_schema=master_schema))
Example #15
0
def zonetbl_run(zone_schema, zonedef_tbl, zone_tbl, zone_col, config,nearest=False) : 
    """collects fire events raster points onto the zone results table for
    a single run, optionally recalculating the fire events raster
    zone_schema.zonedef_tbl  : names the zone definition table
    zone_col                 : names the zone column in the def table
    zone_schema.zone_tbl     : names the zone results table
    gt_schema.gt_table       : names the ground truth raster (for alignment)
    config                   : connection/run specific information
    
    This function will call one of two stored procedures in the database: 
        * viirs_zonetbl_run; or 
        * viirs_nearest_zonetbl_run
    depending on the value of 'nearest'.
    """
    run_schema = config.DBschema
    
    if nearest : 
        function_name = 'viirs_nearest_zonetbl_run'
    else : 
        function_name = 'viirs_zonetbl_run'

    query="SELECT {5}('{0}','{1}','{2}','{3}','{4}')".format(
        zone_schema, zone_tbl, zonedef_tbl, run_schema, zone_col, function_name)
    vt.execute_query(config, query)
Example #16
0
def calc_ioveru_fom(config) :  
    """calculates the intersection over union figure of merit.
    This function assumes that the mask_sum raster already exists in the 
    database. Returns a floating point number from 0..1"""
    query = "SELECT viirs_calc_fom('{0}')".format(config.DBschema)
    
    # now, need to execute a query that returns a single result.
    ConnParam = vt.postgis_conn_params(config)
    conn = psycopg2.connect(ConnParam)
    # Open a cursor to perform database operations
    cur = conn.cursor()
    cur.execute(query)
    rows = cur.fetchall()

    conn.commit()
    # Close communication with the database
    cur.close()
    conn.close()
    
    return rows[0][0]
Example #17
0
def calc_ioveru_fom(config):
    """calculates the intersection over union figure of merit.
    This function assumes that the mask_sum raster already exists in the 
    database. Returns a floating point number from 0..1"""
    query = "SELECT viirs_calc_fom('{0}')".format(config.DBschema)

    # now, need to execute a query that returns a single result.
    ConnParam = vt.postgis_conn_params(config)
    conn = psycopg2.connect(ConnParam)
    # Open a cursor to perform database operations
    cur = conn.cursor()
    cur.execute(query)
    rows = cur.fetchall()

    conn.commit()
    # Close communication with the database
    cur.close()
    conn.close()

    return rows[0][0]
Example #18
0
def find_missing_zonetbl_runs(gt_schema, zone_tbl, config) : 
    """locates missing run_ids in the zone_tbl by comparing with the list of schemas"""
    query = """select b.runs from
      (select nspname runs from pg_namespace where nspname LIKE 'Run_%') b
      where b.runs not in (select distinct run_id from "{0}"."{1}")""".format(
                gt_schema, zone_tbl)

    # now, need to execute a query that returns multiple results.
    ConnParam = vt.postgis_conn_params(config)
    conn = psycopg2.connect(ConnParam)
    # Open a cursor to perform database operations
    cur = conn.cursor()
    cur.execute(query)
    rows = cur.fetchall()

    conn.commit()
    # Close communication with the database
    cur.close()
    conn.close()

    runs = [ i[0] for i in rows ] 

    return runs
Example #19
0
def find_missing_zonetbl_runs(gt_schema, zone_tbl, config):
    """locates missing run_ids in the zone_tbl by comparing with the list of schemas"""
    query = """select b.runs from
      (select nspname runs from pg_namespace where nspname LIKE 'Run_%') b
      where b.runs not in (select distinct run_id from "{0}"."{1}")""".format(
        gt_schema, zone_tbl)

    # now, need to execute a query that returns multiple results.
    ConnParam = vt.postgis_conn_params(config)
    conn = psycopg2.connect(ConnParam)
    # Open a cursor to perform database operations
    cur = conn.cursor()
    cur.execute(query)
    rows = cur.fetchall()

    conn.commit()
    # Close communication with the database
    cur.close()
    conn.close()

    runs = [i[0] for i in rows]

    return runs
Example #20
0
def zonetbl_init(zone_schema, zone_tbl, zone_col, config):
    """drops and re-creates the table in which results are accumulated"""
    query = "SELECT viirs_zonetbl_init('{0}', '{1}', '{2}', {3})".format(
        zone_schema, zone_tbl, zone_col, vt.srids['NLCD'])
    vt.execute_query(config, query)
Example #21
0
def project_fire_events_nlcd(config) :
    """Creates a new geometry column in the fire_events table and project to
    NLCD coordinates."""
    
    query = "SELECT viirs_nlcd_geom('{0}', 'fire_events', {1})".format(config.DBschema, vt.srids["NLCD"])
    vt.execute_query(config, query)
Example #22
0
def delete_confirmed(config) : 
    """wipe out fire events and collections"""
    vt.execute_query(config, 'DELETE FROM "{0}".fire_events'.format(config.DBschema))
    vt.execute_query(config, 'DELETE FROM "{0}".fire_collections'.format(config.DBschema))
Example #23
0
def init_schemas(configs) : 
    """Drops (if necessary) and re-creates a schema for each of the
    runs in the configuration list."""
    for cfg in configs : 
        vt.initialize_schema_for_postgis(cfg)
Example #24
0
def zonetbl_init(zone_schema, zone_tbl, zone_col, config) : 
    """drops and re-creates the table in which results are accumulated"""
    query = "SELECT viirs_zonetbl_init('{0}', '{1}', '{2}', {3})".format(
           zone_schema, zone_tbl, zone_col, vt.srids['NLCD'])
    vt.execute_query(config, query)
Example #25
0
def refresh(base_dir):
    config_list = vc.VIIRSConfig.load_batch(base_dir)

    for c in config_list:
        vt.output_shape_files(c)
Example #26
0
def refresh(base_dir) : 
    config_list = vc.VIIRSConfig.load_batch(base_dir)

    for c in config_list : 
        vt.output_shape_files(c)
Example #27
0
def extract_fire_mask(config, gt_schema, gt_table,rast_col='rast',geom_col='geom') : 
    """Creates a geometry column and populates with pixel centers where pixel value=1"""
    query="SELECT viirs_get_mask_pts('{0}', '{1}', '{2}', '{3}', {4})".format(
         gt_schema, gt_table, rast_col, geom_col, vt.srids['NLCD'])
    vt.execute_query(config, query) 
Example #28
0
def delete_confirmed(config):
    """wipe out fire events and collections"""
    vt.execute_query(config,
                     'DELETE FROM "{0}".fire_events'.format(config.DBschema))
    vt.execute_query(
        config, 'DELETE FROM "{0}".fire_collections'.format(config.DBschema))
Example #29
0
def mask_points(config) : 
    """apply landcover mask to active_fire and threshold burned"""
    vt.execute_query(config, "SELECT viirs_mask_points('{0}','active_fire','landmask','noburn','geom')".format(config.DBschema))
    vt.execute_query(config, "SELECT viirs_mask_points('{0}','threshold_burned','landmask','noburn','geom')".format(config.DBschema))