Exemplo n.º 1
0
def test_multiple_excl_fractions():
    """
    Test that multiple fraction exclusions are handled properly
    """
    excl_h5 = os.path.join(TESTDATADIR, 'ri_exclusions', 'ri_exclusions.h5')

    excl_dict = {'ri_smod': {'include_values': [1, ], 'weight': 0.5,
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth = f.mask

    excl_dict = {'ri_padus': {'exclude_values': [1, ], 'weight': 0.25,
                              'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth = np.minimum(truth, f.mask)

    excl_dict = {'ri_smod': {'include_values': [1, ], 'weight': 0.5,
                             'exclude_nodata': True},
                 'ri_padus': {'exclude_values': [1, ], 'weight': 0.25,
                              'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        test = f.mask

    assert np.allclose(test, truth)
    assert np.all(test[test > 0] >= 0.25)
Exemplo n.º 2
0
def test_inclusion_weights():
    """
    Test inclusion weights
    """
    excl_h5 = os.path.join(TESTDATADIR, 'ri_exclusions', 'ri_exclusions.h5')

    excl_dict = {'ri_smod': {'include_values': [1, ], 'weight': 1,
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth = f.mask

    excl_dict = {'ri_smod': {'include_values': [2, 3], 'weight': 0.5,
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth += f.mask

    excl_dict = {'ri_smod': {'inclusion_weights': {1: 1, 2: 0.5, 3: 0.5},
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        test = f.mask

    assert np.allclose(test, truth)
    assert np.all(test > 0)

    excl_dict = {'ri_smod': {'inclusion_weights': {1.0: 1, 2.0: 0.5, 3.0: 0.5},
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        test = f.mask

    assert np.allclose(test, truth)
    assert np.all(test > 0)
Exemplo n.º 3
0
def test_bad_layer():
    """
    Test creation of inclusion mask
    """
    excl_h5 = os.path.join(TESTDATADIR, 'ri_exclusions', 'ri_exclusions.h5')
    excl_dict = CONFIGS['bad']
    with pytest.raises(ExclusionLayerError):
        with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict,
                                   check_layers=True) as f:
            # pylint: disable=pointless-statement
            f.mask

    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict,
                               check_layers=False) as f:
        assert not f.mask.any()
Exemplo n.º 4
0
def test_no_excl(ds_slice):
    """
    Test ExclusionMask with no exclusions provided
    """
    excl_h5 = os.path.join(TESTDATADIR, 'ri_exclusions', 'ri_exclusions.h5')
    with ExclusionLayers(excl_h5) as f:
        shape = f.shape

    truth = np.ones(shape)
    with ExclusionMask(excl_h5) as f:
        if ds_slice is None:
            test = f.mask
        else:
            test = f[ds_slice]
            truth = truth[ds_slice]

        assert np.allclose(truth, test)

    truth = np.ones(shape)
    with ExclusionMaskFromDict(excl_h5) as f:
        if ds_slice is None:
            test = f.mask
        else:
            test = f[ds_slice]
            truth = truth[ds_slice]

        assert np.allclose(truth, test)
Exemplo n.º 5
0
    def __init__(self,
                 excl_fpath,
                 excl_dict=None,
                 area_filter_kernel='queen',
                 min_area=None,
                 check_excl_layers=False):
        """
        Parameters
        ----------
        excl_fpath : str
            Filepath to exclusions h5 with techmap dataset.
        excl_dict : dict | None
            Dictionary of exclusion LayerMask arugments {layer: {kwarg: value}}
        area_filter_kernel : str
            Contiguous area filter method to use on final exclusions mask
        min_area : float | None
            Minimum required contiguous area filter in sq-km
        check_excl_layers : bool
            Run a pre-flight check on each exclusion layer to ensure they
            contain un-excluded values
        """

        self._excl_fpath = excl_fpath
        self._excl = ExclusionMaskFromDict(excl_fpath,
                                           layers_dict=excl_dict,
                                           min_area=min_area,
                                           kernel=area_filter_kernel,
                                           check_layers=check_excl_layers)
Exemplo n.º 6
0
def test_force_inclusion():
    """
    Test force inclusion
    """
    excl_h5 = os.path.join(TESTDATADIR, 'ri_exclusions', 'ri_exclusions.h5')

    excl_dict = {'ri_padus': {'exclude_values': [1, ], 'weight': 0.25,
                              'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth = f.mask

    excl_dict = {'ri_smod': {'force_include_values': [1, ], 'weight': 0.5,
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        truth = np.maximum(truth, f.mask)

    excl_dict = {'ri_padus': {'exclude_values': [1, ], 'weight': 0.25,
                              'exclude_nodata': True},
                 'ri_smod': {'force_include_values': [1, ], 'weight': 0.5,
                             'exclude_nodata': True}}
    with ExclusionMaskFromDict(excl_h5, layers_dict=excl_dict) as f:
        test = f.mask

    assert np.allclose(test, truth)
Exemplo n.º 7
0
def mask(ag_dir, dst):
    """Use a supply-curve config to aggregate exclusion layers into one."""
    if os.path.exists(dst):
        return

    # Get the aggreation configuration file
    path = os.path.join(ag_dir, "config_aggregation.json")
    with open(path, "r") as file:
        config = json.load(file)

    # Extract the needed elements from the confgi
    excl_h5 = config["excl_fpath"]
    layers_dict = config["excl_dict"]
    if "min_area" in config:
        min_area = config["min_area"]
    else:
        min_area = None
    if "area_filter_kernel" in config:
        kernel = config["area_filter_kernel"]
    else:
        kernel = "queen"

    # Create a mask converter
    masker = ExclusionMaskFromDict(excl_h5,
                                   layers_dict=layers_dict,
                                   min_area=min_area,
                                   kernel=kernel)

    # Get the mask and the georeferencing
    mask = masker.mask
    mask = mask.astype("float32")
    try:
        profile = masker.excl_h5.profile
    except KeyError:
        with h5py.File(excl_h5, "r") as ds:
            for key in ds.keys():
                if "profile" in ds[key].attrs.keys():
                    profile = ds[key].attrs["profile"]
                    profile = json.loads(profile)
    profile["dtype"] = str(mask.dtype)

    # Save
    with rio.Env():
        with rio.open(dst, "w", **profile) as file:
            file.write(mask, 1)
}

EXCL_DICT = {
    'ri_srtm_slope': {
        'inclusion_range': (None, 5),
        'exclude_nodata': True
    },
    'ri_padus': {
        'exclude_values': [1],
        'exclude_nodata': True
    }
}

RESOLUTION = 64
EXTENT = SupplyCurveExtent(EXCL_FPATH, resolution=RESOLUTION)
EXCL = ExclusionMaskFromDict(EXCL_FPATH, EXCL_DICT)
FRICTION = FrictionMask(FRICTION_FPATH, FRICTION_DSET)


def test_friction_mask():
    """Test the friction mask on known quantities."""

    x = FRICTION[slice(700, 800), slice(300, 400)].mean()
    assert x == 20.0, 'Friction for region should be 20.0, but is {}'.format(x)

    x = FRICTION[slice(300, 400), slice(100, 300)].mean()
    assert x == 1.0, 'Friction for nodata should be 1.0, but is {}'.format(x)

    x = FRICTION[slice(300, 400), slice(800, 900)].mean()
    assert x == 1.0, 'Friction for nodata should be 1.0, but is {}'.format(x)