Skip to content

Commit

Permalink
✨ Split add_overlay in 2 functions from_stcs and from_region
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed May 17, 2024
1 parent cfa5513 commit c06027b
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/ipyaladin/aladin.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def add_table(self, table: Union[QTable, Table], **table_options: any) -> None:
buffers=[table_bytes.getvalue()],
)

def add_overlay(
def add_overlay_from_region(
self,
region: SupportedRegion,
**graphic_options: any,
Expand All @@ -394,6 +394,15 @@ def add_overlay(
See graphicOverlay options here https://cds-astro.github.io/aladin-lite/A.html
"""
if Region is None:
raise ModuleNotFoundError(
"A region can be given as a regions object. To read "
"regions objects, you need to install the regions library with "
"'pip install regions'."
)

# Check if the region is a list of regions or a single
# Region and convert it to a list of Regions
if isinstance(region, Regions):
region_list = region.regions
elif not isinstance(region, list):
Expand All @@ -404,18 +413,7 @@ def add_overlay(
regions_infos = []
for region_element in region_list:
# Check if the regions library is installed and raise an error if not
if (
not isinstance(region_element, str) and Region is None
): # Only need to check one of the imports
raise ModuleNotFoundError(
"A region can be given as an STC-S string or a regions "
"object. To read regions objects, you need to install the regions "
"library with 'pip install regions'."
)

if not isinstance(region_element, str) and not isinstance(
region_element, Region
):
if not isinstance(region_element, Region):
raise ValueError(
"region must be a string or a `~regions` object. See the "
"documentation for the supported region types."
Expand All @@ -434,8 +432,10 @@ def add_overlay(
}
)

def add_overlay_from_stcs(self, stc_string: str, **overlay_options: any) -> None:
"""Add an overlay layer defined by a STC-S string.
def add_overlay_from_stcs(
self, stc_string: Union[typing.List[str], str], **overlay_options: any
) -> None:
"""Add an overlay layer defined by an STC-S string.
Parameters
----------
Expand All @@ -444,11 +444,23 @@ def add_overlay_from_stcs(self, stc_string: str, **overlay_options: any) -> None
overlay_options: keyword arguments
"""
# Add deprecation warning
warnings.warn(
"add_overlay_from_stcs is deprecated, use add_overlay instead", stacklevel=2
region_list = [stc_string] if not isinstance(stc_string, list) else stc_string

regions_infos = [
{
"region_type": "stcs",
"infos": {"stcs": region_element},
"options": overlay_options,
}
for region_element in region_list
]
self.send(
{
"event_name": "add_overlay",
"regions_infos": regions_infos,
"graphic_options": {},
}
)
self.add_overlay(stc_string, **overlay_options)

def get_JPEG_thumbnail(self) -> None:
"""Create a popup window with the current Aladin view."""
Expand Down

0 comments on commit c06027b

Please sign in to comment.