Skip to content

Commit

Permalink
✨ Implement add_marker to add a marker to the Aladin Lite view
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Aug 8, 2024
1 parent 8612e25 commit 54639f4
Show file tree
Hide file tree
Showing 4 changed files with 83 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 @@ -259,6 +259,7 @@ export default class EventHandler {
});

this.eventHandlers = {
add_marker: this.messageHandler.handleAddMarker,
change_fov: this.messageHandler.handleChangeFoV,
goto_ra_dec: this.messageHandler.handleGotoRaDec,
save_view_as_image: this.messageHandler.handleSaveViewAsImage,
Expand Down
19 changes: 19 additions & 0 deletions js/models/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ export default class MessageHandler {
this.model = model;
}

handleAddMarker(msg) {
const options = convertOptionNamesToCamelCase(msg["options"] || {});
if (!options.name) options.name = "markers";
const rawMarkers = msg["markers"];
const catalog = A.catalog(options);
this.aladin.addCatalog(catalog);
const markers = [];
for (const marker of rawMarkers) {
const position = marker["position"].split(" ");
markers.push(
A.marker(parseFloat(position[0]), parseFloat(position[1]), {
popupTitle: marker["title"],
popupDesc: marker["description"],
}),
);
}
catalog.addSources(markers);
}

handleChangeFoV(msg) {
this.aladin.setFoV(msg["fov"]);
}
Expand Down
38 changes: 38 additions & 0 deletions src/ipyaladin/utils/marker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Tuple, Union

from astropy.coordinates import SkyCoord
from ipyaladin.utils._coordinate_parser import parse_coordinate_string


class Marker:
"""A class representing a marker in Aladin Lite."""

def __init__(
self,
position: Union[str, SkyCoord, Tuple[float, float]],
title: str,
description: str,
) -> None:
self.title = title
self.description = description
if isinstance(position, SkyCoord):
self.position = f"{position.ra.deg} {position.dec.deg}"
elif isinstance(position, str):
sc = parse_coordinate_string(position)
self.position = f"{sc.ra.deg} {sc.dec.deg}"
else:
self.position = f"{position[0]} {position[1]}"

def to_dict(self) -> dict:
"""Convert the marker to a dictionary.
Returns
-------
dict
The marker as a dictionary
"""
return {
"position": self.position,
"title": self.title,
"description": self.description,
}
25 changes: 25 additions & 0 deletions src/ipyaladin/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from .utils.exceptions import WidgetCommunicationError
from .utils._coordinate_parser import parse_coordinate_string
from .utils.marker import Marker

try:
from regions import (
Expand Down Expand Up @@ -389,6 +390,30 @@ def target(self, target: Union[str, SkyCoord]) -> None:
}
)

def add_marker(
self, markers: Union[Marker, List[Marker]], **catalog_options: any
) -> None:
"""Add a marker to the Aladin Lite widget.
Parameters
----------
markers : Marker or list[Marker]
The marker(s) to add to the widget. It can be given as a single `Marker`
object or as a list of `Marker` objects.
catalog_options : any
The options for the catalog. See the `Aladin Lite catalog options
<https://cds-astro.github.io/aladin-lite/global.html#CatalogOptions>`_
"""
if not isinstance(markers, list):
markers = [markers]
self.send(
{
"event_name": "add_marker",
"markers": [marker.to_dict() for marker in markers],
"options": catalog_options,
}
)

def _save_file(self, path: str, buffer: bytes) -> None:
"""Save a file from a buffer.
Expand Down

0 comments on commit 54639f4

Please sign in to comment.