Skip to content

Commit

Permalink
✨ Implement add_overlay function with support for stcs string, Circle…
Browse files Browse the repository at this point in the history
…SkyRegion, EllipseSkyRegion and LineSkyRegion
  • Loading branch information
Xen0Xys committed May 13, 2024
1 parent 39415a9 commit f9ee0e4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export default class EventHandler {
add_catalog_from_URL: this.messageHandler.handleAddCatalogFromURL,
add_MOC_from_URL: this.messageHandler.handleAddMOCFromURL,
add_MOC_from_dict: this.messageHandler.handleAddMOCFromDict,
add_overlay: this.messageHandler.handleAddOverlay,
add_overlay_from_stcs: this.messageHandler.handleAddOverlayFromSTCS,
change_colormap: this.messageHandler.handleChangeColormap,
get_JPG_thumbnail: this.messageHandler.handleGetJPGThumbnail,
Expand Down
22 changes: 22 additions & 0 deletions js/models/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ export default class MessageHandler {
this.aladin.addMOC(A.MOCFromJSON(msg["moc_dict"], options));
}

handleAddOverlay(msg) {
const infos = msg["infos"];
const options = convertOptionNamesToCamelCase(msg["options"] || {});
const overlay = A.graphicOverlay(options);
switch (msg["region_type"]) {
case "circle":
this.aladin.addOverlay(overlay);
overlay.add(A.circle(infos.ra, infos.dec, infos.radius));
break;
case "ellipse":
this.aladin.addOverlay(overlay);
overlay.add(
A.ellipse(infos.ra, infos.dec, infos.a, infos.b, infos.theta),
);
break;
case "line":
this.aladin.addOverlay(overlay);
overlay.add(A.line(infos.ra1, infos.dec1, infos.ra2, infos.dec2));
break;
}
}

handleAddOverlayFromSTCS(msg) {
const overlayOptions = convertOptionNamesToCamelCase(
msg["overlay_options"] || {},
Expand Down
56 changes: 56 additions & 0 deletions src/ipyaladin/aladin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from astropy.table import Table
from astropy.coordinates import SkyCoord, Angle
import traitlets
from regions import CircleSkyRegion, EllipseSkyRegion, LineSkyRegion
from traitlets import (
Float,
Int,
Expand Down Expand Up @@ -336,6 +337,61 @@ def add_table(self, table: Union[QTable, Table], **table_options: any) -> None:
buffers=[table_bytes.getvalue()],
)

def add_overlay(
self,
region: Union[str, CircleSkyRegion, EllipseSkyRegion, LineSkyRegion],
**overlay_options: any,
) -> None:
"""Add an overlay layer to the Aladin Lite widget.
Parameters
----------
region: str, CircleSkyRegion, EllipseSkyRegion, LineSkyRegion
The region to overlay. It can be a string, a CircleSkyRegion,
an EllipseSkyRegion or a LineSkyRegion.
overlay_options: keyword arguments
"""
region_type = ""
infos = {}

if isinstance(region, str):
self.add_overlay_from_stcs(region, **overlay_options)
return
if isinstance(region, CircleSkyRegion):
region_type = "circle"
infos = {
"ra": region.center.ra.deg,
"dec": region.center.dec.deg,
"radius": region.radius.deg,
}
elif isinstance(region, EllipseSkyRegion):
region_type = "ellipse"
infos = {
"ra": region.center.ra.deg,
"dec": region.center.dec.deg,
"a": region.width.deg,
"b": region.height.deg,
"theta": region.angle.deg,
}
elif isinstance(region, LineSkyRegion):
region_type = "line"
infos = {
"ra1": region.start.ra.deg,
"dec1": region.start.dec.deg,
"ra2": region.end.ra.deg,
"dec2": region.end.dec.deg,
}

self.send(
{
"event_name": "add_overlay",
"region_type": region_type,
"infos": infos,
"options": overlay_options,
}
)

def add_overlay_from_stcs(self, stc_string: str, **overlay_options: any) -> None:
"""Add an overlay layer defined by a STC-S string.
Expand Down

0 comments on commit f9ee0e4

Please sign in to comment.