diff --git a/jdaviz/configs/imviz/plugins/tools.py b/jdaviz/configs/imviz/plugins/tools.py index 4dfa4d08d5..44121d477a 100644 --- a/jdaviz/configs/imviz/plugins/tools.py +++ b/jdaviz/configs/imviz/plugins/tools.py @@ -8,6 +8,7 @@ from glue_jupyter.utils import debounced from jdaviz.core.tools import BoxZoom, PanZoom, _MatchedZoomMixin +from jdaviz.configs.imviz.helper import get_top_layer_index __all__ = [] @@ -37,15 +38,18 @@ def deactivate(self): def on_click(self, data): # Find visible layers - visible_layers = [layer for layer in self.viewer.state.layers if layer.visible] - if len(visible_layers) == 0: + try: + i = get_top_layer_index(self.viewer) + except IndexError: + return + if i is None: return # Same data as mousemove event in Imviz viewer. # Any other config that wants this functionality has to have the following: # viewer._get_real_xy() # viewer.center_on() --> inherited from AstrowidgetsImageViewerMixin - image = visible_layers[0].layer + image = self.viewer.state.layers[i].layer x = data['domain']['x'] y = data['domain']['y'] if x is None or y is None: # Out of bounds diff --git a/jdaviz/configs/imviz/plugins/viewers.py b/jdaviz/configs/imviz/plugins/viewers.py index a36e317caa..285d801b77 100644 --- a/jdaviz/configs/imviz/plugins/viewers.py +++ b/jdaviz/configs/imviz/plugins/viewers.py @@ -135,12 +135,12 @@ def blink_once(self, reversed=False): def on_limits_change(self, *args): try: i = get_top_layer_index(self) - if i is None: - return except IndexError: if self.compass is not None: self.compass.clear_compass() return + if i is None: + return self.set_compass(self.state.layers[i].layer) @property diff --git a/jdaviz/configs/imviz/tests/test_tools.py b/jdaviz/configs/imviz/tests/test_tools.py index dd260f800e..4d6abab8af 100644 --- a/jdaviz/configs/imviz/tests/test_tools.py +++ b/jdaviz/configs/imviz/tests/test_tools.py @@ -1,7 +1,10 @@ import numpy as np +import pytest +from astropy.coordinates import SkyCoord +from astropy.nddata import NDData from numpy.testing import assert_allclose -from jdaviz.configs.imviz.tests.utils import BaseImviz_WCS_WCS +from jdaviz.configs.imviz.tests.utils import BaseImviz_WCS_WCS, create_example_gwcs class TestPanZoomTools(BaseImviz_WCS_WCS): @@ -64,6 +67,46 @@ def test_panzoom_tools(self): t_linkedpan.deactivate() +@pytest.mark.parametrize("link_type", ["Pixels", "WCS"]) +def test_panzoom_click_center_linking(imviz_helper, link_type): + """https://github.com/spacetelescope/jdaviz/issues/2749""" + v = imviz_helper.default_viewer._obj + + # Since we are not really displaying, need this to test pan/zoom. + v.shape = (100, 100) + v.state._set_axes_aspect_ratio(1) + + arr_big = np.ones((40, 30), dtype=int) + w_big = create_example_gwcs(arr_big.shape) + arr_small = np.ones((20, 15), dtype=int) + w_small = create_example_gwcs(arr_small.shape) + + imviz_helper.load_data(NDData(arr_big, wcs=w_big), data_label="big") + imviz_helper.load_data(NDData(arr_small, wcs=w_small), data_label="small") + + lc_plugin = imviz_helper.plugins['Orientation'] + lc_plugin.link_type = link_type + + coo = SkyCoord(ra=197.89262754541807, dec=-1.3644568140486624, unit="deg") + + if link_type == "WCS": + mouseover_loc = v.state.reference_data.coords.world_to_pixel(coo) + else: # Pixels + mouseover_loc = w_small.world_to_pixel(coo) + + t = v.toolbar.tools['jdaviz:imagepanzoom'] + t.activate() + t.on_click({'event': 'click', 'domain': {'x': mouseover_loc[0], 'y': mouseover_loc[1]}}) + t.deactivate() + + # We want to make sure click centers viewer to where it is supposed to be. + cur_cen = v._get_center_skycoord() + v.center_on(coo) + real_cen = v._get_center_skycoord() + assert_allclose(cur_cen.ra.deg, real_cen.ra.deg) + assert_allclose(cur_cen.dec.deg, real_cen.dec.deg) + + def test_blink(imviz_helper): viewer = imviz_helper.default_viewer._obj