Beispiel #1
0
def vegetation_fis_validation(top_level_folder, database):
    """
    Validate PyBRAT 4 vegetation FIS with that of pyBRAT 3
    :param top_level_folder: Top level folder containing pyBRAT 3 HUC 8 projects
    :param database: Path to the SQLite database containing pyBRAT configuration
    :return: None
    """

    hucs = get_hucs_present(top_level_folder, database)

    for _label, veg_type in {'Existing': 'EX', 'Historic': 'hpe'}.items():
        plot_values = []
        for _huc, paths in hucs.items():
            out_field = 'oVC_{}'.format(veg_type)
            streamside_field = 'iVeg_30{}'.format(veg_type)
            riparian_field = 'iVeg100{}'.format(veg_type)

            # Load the input fields required as well as the pyBRAT3 output fields
            feature_values = load_attributes(
                paths['Network'], 'ReachID',
                [streamside_field, riparian_field])
            expected_output = load_attributes(paths['Network'], 'ReachID',
                                              [out_field])

            calculate_vegegtation_fis(feature_values, streamside_field,
                                      riparian_field, out_field)

            # Merge the results into a master list
            for reach, feature in feature_values.items():
                plot_values.append(
                    (expected_output[reach][out_field], feature[out_field]))

        validation_chart(plot_values, '{} Vegetation FIS'.format(veg_type))

    print('Validation complete')
def combined_fis_validation(top_level_folder, database):
    """
    Validate PyBRAT 4 combined FIS with that of pyBRAT 3
    :param top_level_folder: Top level folder containing pyBRAT 3 HUC 8 projects
    :param database: Path to the SQLite database containing pyBRAT configuration
    :return: None
    """

    hucs = get_hucs_present(top_level_folder, database)
    dathresh = get_drainage_area_thresh(database)

    for label, veg_type in {'Existing': 'EX', 'Historic': 'HPE'}.items():
        capacity_values = []
        density_values = []

        veg_fis_field = 'oVC_{}'.format(veg_type)
        com_capacity_field = 'oCC_{}'.format(veg_type)
        com_density_field = 'oMC_{}'.format(veg_type)

        for huc, paths in hucs.items():

            max_drainage_area = dathresh[huc]

            # Load the input fields required as well as the pyBRAT3 output fields
            feature_values = load_attributes(paths['Network'], 'ReachID', [
                veg_fis_field, 'iGeo_Slope', 'iGeo_DA', 'iHyd_SP2',
                'iHyd_SPLow', 'iGeo_Len'
            ])
            expected_output = load_attributes(
                paths['Network'], 'ReachID',
                [com_capacity_field, com_density_field])

            # Do the combined FIS calculation
            calculate_combined_fis(feature_values, veg_fis_field,
                                   com_capacity_field, com_density_field,
                                   max_drainage_area)

            # Merge the results into a master list
            for reach, feature in feature_values.items():
                capacity_values.append(
                    (expected_output[reach][com_capacity_field],
                     feature[com_capacity_field]))
                density_values.append(
                    (expected_output[reach][com_density_field],
                     feature[com_density_field]))

        # Plot the master list
        validation_chart(capacity_values,
                         '{} Combined FIS Capacity'.format(label))
        validation_chart(density_values,
                         '{} Combined FIS Density'.format(label))

    print('Validation complete')
def reach_geometry_validation(top_level_folder, database, buffer_distance):
    """
    Validate PyBRAT 4 vegetation FIS with that of pyBRAT 3
    :param top_level_folder: Top level folder containing pyBRAT 3 HUC 8 projects
    :param database: Path to the SQLite database containing pyBRAT configuration
    :param buffer_distance: PyBRAT 4 buffer distance for sampling DEM raster elevations
    :return: None
    """

    hucs = get_hucs_present(top_level_folder, database)
    fields = ['iGeo_Slope', 'iGeo_ElMin', 'iGeo_ElMax', 'iGeo_Len']

    results = {}
    for _huc, paths in hucs.items():
        polylines = load_geometries(paths['Network'], 'ReachID')
        db_srs = get_db_srs(database)
        expected = load_attributes(paths['Network'], 'ReachID', fields)
        results = calculate_reach_geometry(polylines, paths['DEM'], db_srs, buffer_distance)

        for field in fields:
            if field not in results:
                results[field] = []

            for reachid, values in results.items():
                if reachid in expected:
                    results[field].append((expected[reachid][field], values[field]))

    [validation_chart(results[field], '{} Reach Geometry'.format(field)) for field in fields]

    print('Validation complete')
Beispiel #4
0
def stream_power_validation(top_level_folder, database, id_field):

    # Detect which Idaho BRAT HUCs are present on local disk
    hucs = load_hucs.get_hucs_present(top_level_folder, database)

    results = {}
    for huc, paths in hucs.items():

        if 'Network' not in paths:
            print('Skipping {} because no network shapefile'.format(huc))
            continue

        print('Validating stream power for HUC', huc)

        # Load the pyBRAT3 values
        inputs = shapefile.load_attributes(paths['Network'], id_field,
                                           input_fields)
        expected = shapefile.load_attributes(paths['Network'], id_field,
                                             output_fields)

        # Calculate the land use attributes
        calculated = calculate_stream_power.calculate_stream_power(inputs)

        # Synthesize the results for all the HUCs
        for field in output_fields:
            if field not in results:
                results[field] = []

            for reachid, values in calculated.items():
                if reachid in expected:
                    results[field].append(
                        (expected[reachid][field], values[field]))

    # Generate the validation plots
    [
        plotting.validation_chart(results[field],
                                  '{} Stream Power'.format(field))
        for field in output_fields
    ]

    print('Validation complete')
Beispiel #5
0
def vegetation_summary_validation(database, veg_raster, top_level_folder):
    """
    Validate PyBRAT 4 vegetation FIS with that of pyBRAT 3
    :param top_level_folder: Top level folder containing pyBRAT 3 HUC 8 projects
    :param database: Path to the SQLite database containing pyBRAT configuration
    :return: None
    """

    hucs = get_hucs_present(top_level_folder, database)

    for table, prefix in {'Existing': 'EX', 'Historic': 'Hpe'}.items():
        for buffer in [30, 100]:
            plot_values = []

            for huc, paths in hucs.items():
                print('Validating', huc)

                # _rough_convert_metres_to_shapefile_units(paths['Network'], 300)

                veg_field = 'iVeg_{}{}'.format(buffer, prefix)
                if buffer == 100:
                    veg_field = veg_field.replace('_', '')

                # Load the input fields required as well as the pyBRAT3 output fields
                geometries = load_geometries(paths['Network'], 'ReachID', 5070)
                expected_output = load_attributes(paths['Network'], 'ReachID',
                                                  [veg_field])

                results = calculate_vegetation_summary(database, geometries,
                                                       veg_raster, buffer,
                                                       table, prefix, huc)

                # Merge the results into a master list
                for reach, feature in results.items():
                    plot_values.append((expected_output[reach][veg_field],
                                        feature[veg_field]))

            validation_chart(
                plot_values,
                '{0}m {1} Vegetation Summary'.format(buffer, table))

    print('Validation complete')
Beispiel #6
0
outLayer.CreateField(ogr.FieldDefn("oPBRC_CR", ogr.OFTInteger))

hucDefn = ogr.FieldDefn("HUC", ogr.OFTString)
hucDefn.SetWidth(8)
outLayer.CreateField(hucDefn)
outLayerDefn = outLayer.GetLayerDefn()

for region in regions:
    print('Processing', region)

    # Load conservation attributes first. That's all we need from this ShapeFile
    conserve = os.path.join(
        top_level_folder, region,
        '01_Perennial_Network/03_Conservation_Restoration_Model/Conservation_Restoration_Model_Perennial_{}.shp'
        .format(region.replace('_', '')))
    consatts = load_attributes(conserve, 'ReachID', ['oPBRC_UI', 'oPBRC_CR'])

    # Use the geometries from the capacity ShapeFile for the output
    capacity = os.path.join(
        top_level_folder, region,
        '01_Perennial_Network/02_Combined_Capacity_Model/Combined_Capacity_Model_Perennial_{}.shp'
        .format(region.replace('_', '')))
    inDataSource = driver.Open(capacity, 0)
    inLayer = inDataSource.GetLayer()
    transform = osr.CoordinateTransformation(inLayer.GetSpatialRef(),
                                             outSpatialRef)

    for feature in inLayer:
        reachid = feature.GetField('ReachID')
        geom = feature.GetGeometryRef()
        geom.Transform(transform)