def test_RBGAMapped_Masking(product_layer_mask_map, style_cfg_map_mask): def fake_make_mask(data, **kwargs): val = kwargs["bar"] return data == val band = np.array([0, 0, 1, 1, 2, 2]) timarray = [np.datetime64(datetime.date.today())] times = DataArray(timarray, coords=[timarray], dims=["time"], name="time") da = DataArray(band, name='foo') dst = Dataset(data_vars={'foo': da}) ds = concat([dst], times) npmap = np.array([True, True, True, True, True, True]) damap = DataArray(npmap) with patch('datacube_ows.styles.colormap.make_mask', new_callable=lambda: fake_make_mask) as fmm: style_def = StyleDef(product_layer_mask_map, style_cfg_map_mask) data = style_def.transform_data(ds, damap) r = data["red"] g = data["green"] b = data["blue"] a = data["alpha"] assert (r[2:3:1] == 0) assert (g[2:3:1] == 0) assert (b[2:3:1] == 0) assert (a[2:3:1] == 0) assert (r[4:5:1] == 255) assert (g[4:5:1] == 255) assert (b[4:5:1] == 255) assert (a[4:5:1] == 255)
def test_alpha_style_map(product_layer_alpha_map, style_cfg_map_alpha_1, style_cfg_map_alpha_2, style_cfg_map_alpha_3): def fake_make_mask(data, **kwargs): return data band = np.array([True, True, True]) timarray = [np.datetime64(datetime.date.today())] times = DataArray(timarray, coords=[timarray], dims=["time"], name="time") da = DataArray(band, name='foo') dst = Dataset(data_vars={'foo': da}) ds = concat([dst], times) npmap = np.array([True, True, True]) damap = DataArray(npmap) with patch('datacube_ows.styles.colormap.make_mask', new_callable=lambda: fake_make_mask) as fmm: style_def = StyleDef(product_layer_alpha_map, style_cfg_map_alpha_1) result = style_def.transform_data(ds, damap) alpha_channel = result["alpha"].values assert (alpha_channel == 0).all() style_def = StyleDef(product_layer_alpha_map, style_cfg_map_alpha_2) result = style_def.transform_data(ds, None) alpha_channel = result["alpha"].values assert (alpha_channel == 127).all() style_def = StyleDef(product_layer_alpha_map, style_cfg_map_alpha_3) result = style_def.transform_data(ds, damap) alpha_channel = result["alpha"].values assert (alpha_channel == 255).all()
def test_style_exceptions(product_layer, style_cfg_map: dict): style_no_name = dict(style_cfg_map) style_no_name.pop('name', None) with pytest.raises(ConfigException) as excinfo: style_def = StyleDef(product_layer, style_no_name) assert "Required field missing in" in str(excinfo.value)
def test_dynamic_range_compression_scale_range(product_layer, style_cfg_lin): style_cfg_lin["scale_range"] = [-3000, 3000] style_def = StyleDef(product_layer, style_cfg_lin) assert style_def.scale_min == -3000 assert style_def.scale_max == 3000 band = np.zeros(3) band[0] = -3000 band[1] = 0 band[2] = 3000 compressed = style_def.compress_band("red", band) assert compressed[0] == 0 assert compressed[1] == 255 / 2 assert compressed[2] == 255
def parse_styling(self, cfg): self.styles = list([StyleDef(self, s) for s in cfg["styles"]]) self.style_index = {s.name: s for s in self.styles} if "default_style" in cfg: if cfg["default_style"] not in self.style_index: raise ConfigException( "Default style %s is not in the 'styles' for layer %s" % (cfg["default_style"], self.name)) self.default_style = self.style_index[cfg["default_style"]] else: self.default_style = self.styles[0]
def test_dynamic_range_compression_scale_factor(product_layer, style_cfg_lin): del style_cfg_lin["scale_range"] style_cfg_lin["scale_factor"] = 2.5 style_def = StyleDef(product_layer, style_cfg_lin) assert style_def.scale_min == 0.0 assert style_def.scale_max == 637.5 band = np.zeros(3) band[0] = -3000 band[1] = 0 band[2] = 3000
def test_correct_style_hybrid(product_layer, style_cfg_lin): style_cfg_lin["component_ratio"] = 1.0 style_cfg_lin["range"] = [1, 2] style_cfg_lin["index_function"] = { "function": "datacube_ows.band_utils.constant", "pass_product_cfg": True, "kwargs": { "const": "0.1" } } style_def = StyleDef(product_layer, style_cfg_lin) assert isinstance(style_def, datacube_ows.styles.hybrid.HybridStyleDef)
def parse_styling(self, cfg): self.styles = [] self.style_index = {} for scfg in cfg["styles"]: style = StyleDef(self, scfg) self.styles.append(style) self.style_index[style.name] = style if "default_style" in cfg: if cfg["default_style"] not in self.style_index: raise ConfigException( f"Default style {cfg['default_style']} is not in the 'styles' for layer {self.name}" ) self.default_style = self.style_index[cfg["default_style"]] else: self.default_style = self.styles[0]
def test_correct_style_ramp(product_layer, style_cfg_ramp): style_def = StyleDef(product_layer, style_cfg_ramp) assert isinstance(style_def, datacube_ows.styles.ramp.ColorRampDef)
def test_correct_style_map(product_layer, style_cfg_map): style_def = StyleDef(product_layer, style_cfg_map) assert isinstance(style_def, datacube_ows.styles.colormap.ColorMapStyleDef)
def test_correct_style_linear(product_layer, style_cfg_lin): style_def = StyleDef(product_layer, style_cfg_lin) assert isinstance(style_def, datacube_ows.styles.component.ComponentStyleDef)