def test_compare_label_stats(filleddata, bspotdata):
    speedups.disable()
    assert not speedups.enabled
    stats = label.label_stats(filleddata, bspotdata)
    speedups.enable()
    assert speedups.enabled
    statsopt = label.label_stats(filleddata, bspotdata)

    for s, so in zip(stats, statsopt):
        assert s == so
Example #2
0
def test_nofilter(bspotdata, depthsdata):
    raw_bluespot_stats = label.label_stats(depthsdata, bspotdata)
    filter_function = lambda r: True
    keepers = bluespots.filterbluespots(filter_function, 1.0,
                                        raw_bluespot_stats)
    assert len(keepers) == len(raw_bluespot_stats)
    assert sum(keepers) == len(raw_bluespot_stats)
Example #3
0
def process_bspots(depths, out, filter):
    """Label bluespots.

    Assign unique bluespot ID to all cells belonging to a bluespot. Optionally disregarding some bluespots based on a
    filter expression. ID 0 (zero) is used for cells not belonging to a bluespot.
    """

    depths_reader = io.RasterReader(depths)
    labeled_writer = io.RasterWriter(out, depths_reader.transform,
                                     depths_reader.crs, 0)
    filter_function = parse_filter(filter)

    transform = depths_reader.transform
    cell_width = abs(transform[1])
    cell_height = abs(transform[5])
    cell_area = cell_width * cell_height

    depths_data = depths_reader.read()
    raw_labeled, raw_nlabels = label.connected_components(depths_data)
    if not filter:
        # This is the end  my friend
        labeled_writer.write(raw_labeled)
        return

    del depths

    raw_bluespot_stats = label.label_stats(depths_data, raw_labeled)
    keepers = filterbluespots(filter_function, cell_area, raw_bluespot_stats)
    new_components = label.keep_labels(raw_labeled, keepers)
    labeled, nlabels = label.connected_components(new_components)
    labeled_writer.write(labeled)
Example #4
0
def bluespot_hypsometry_stats(bluespotlabels,
                              dem,
                              resolution,
                              labels_max=None,
                              background=0):
    if not speedups.enabled:
        logger.warning(
            'Warning: Speedups are not available. If you have more than toy data you want them to be!'
        )

    if labels_max is None:
        labels_max = np.max(bluespotlabels)

    logger.debug("Calculate z stats for each bluespot")
    label_z_stats = label_stats(dem, bluespotlabels, labels_max)

    logger.debug("Collecting label data values")
    label_data_values = label_data(dem,
                                   bluespotlabels,
                                   labels_max,
                                   background=background)

    logger.debug("Calculating histograms")
    for label, data in enumerate(label_data_values):
        if label == background:
            bins = HistogramBinsInfo(0, 0, 0, -1)
            counts = []
        else:
            bins = histogram_bins(label_z_stats[label]["min"],
                                  label_z_stats[label]["max"], resolution)
            counts, _ = np.histogram(data, bins.num_bins,
                                     (bins.lower_bound, bins.upper_bound))
        yield label, HypsometryStats(Histogram(counts, bins),
                                     label_z_stats[label]["min"],
                                     label_z_stats[label]["max"])
Example #5
0
def test_filter(bspotdata, depthsdata):
    raw_bluespot_stats = label.label_stats(depthsdata, bspotdata)
    filter_function = lambda r: r['max'] > 2 and r['count'] > 5 and r['sum'
                                                                      ] > 1
    keepers = bluespots.filterbluespots(filter_function, 1.0,
                                        raw_bluespot_stats)
    assert len(keepers) == len(raw_bluespot_stats)
    assert sum(keepers) == 19
Example #6
0
def process_pourpoints(bluespots, depths, watersheds, dem, accum, out, format,
                       layername, dsco, lco):
    """Determine pour points.

    \b
    Determines a pour point for each bluespot using one of two methods:
        * Random candidate. Requires DEM only
        * Maximum accumulated flow candidate. Requires accumulated flow
    The output of the two methods only differ when there are more than one pour point candidate (ie multiple threshold
    cells with identical Z) for a given bluespot.

    For documentation of OGR features (format, dsco and lco) see http://www.gdal.org/ogr_formats.html
    """
    bspot_reader = io.RasterReader(bluespots)
    depths_reader = io.RasterReader(depths)
    wsheds_reader = io.RasterReader(watersheds)

    data = accum if accum else dem
    if not data:
        raise Exception('Either accum or dem must be specified')
    data_reader = io.RasterReader(data)

    format = str(format)
    layername = str(layername)

    pourpnt_writer = io.VectorWriter(format, out, layername, [], ogr.wkbPoint,
                                     depths_reader.crs, dsco, lco)

    # Recalculate stats on filtered bluespots
    labeled_data = bspot_reader.read()
    depths_data = depths_reader.read()
    bluespot_stats = label.label_stats(depths_data, labeled_data)
    del depths_data

    if accum:
        pp_pix = label.label_max_index(data_reader.read(), labeled_data)
    elif dem:
        dem_data = data_reader.read()
        short, diag = fill.minimum_safe_short_and_diag(dem_data)
        filled_no_flats = fill.fill_terrain_no_flats(dem_data, short, diag)
        pp_pix = label.label_min_index(filled_no_flats, labeled_data)
        del dem_data

    watershed_stats = label.label_count(wsheds_reader.read())
    pour_points = assemble_pourpoints(depths_reader.transform, pp_pix,
                                      bluespot_stats, watershed_stats)

    feature_collection = dict(type="FeatureCollection", features=pour_points)
    pourpnt_writer.write_geojson_features(feature_collection)
def test_label_stats_optimized(filleddata, bspotdata):
    speedups.enable()
    assert speedups.enabled
    stats = label.label_stats(filleddata, bspotdata)
    assert len(stats) == np.max(bspotdata) + 1
    assert np.all(stats[:]['min'] <= stats[:]['max'])
    assert np.sum(stats[:]['count']) == bspotdata.shape[0] * bspotdata.shape[1]
    assert np.min(stats[:]['min']) == np.min(filleddata)
    assert np.max(stats[:]['max']) == np.max(filleddata)

    # Check label with most cells
    lbl = np.argmax(stats[:]['count'])
    lblix = bspotdata == lbl
    assert stats[lbl]['min'] == np.min(filleddata[lblix])
    assert stats[lbl]['max'] == np.max(filleddata[lblix])
    np.testing.assert_almost_equal(
        stats[lbl]['sum'], np.sum(filleddata.astype(np.float64)[lblix]))
    assert stats[lbl]['count'] == np.sum(lblix)