Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add label to viewer #483

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions bindings/python/test/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,76 @@ def test_add_target_success(
in rendered_html
)
assert rendered_html.endswith("</script>")

def test_add_target_with_label_success(
self,
viewer: Viewer,
):
viewer.add_target(
position=Position.meters([1.0, 2.0, 3.0], Frame.ITRF()),
size=123,
color="red",
label="TEST",
)

rendered_html: str = viewer.render()

assert rendered_html.startswith('<meta charset="utf-8">')
assert "var widget = new Cesium.Viewer" in rendered_html
assert (
"widget.entities.add({position: Cesium.Cartesian3.fromDegrees(63.43494882292201, 18.22447811510915, -6376045.535225509), point: {pixelSize: 123.0, color: Cesium.Color.RED}});"
in rendered_html
)
assert (
'widget.entities.add({position: Cesium.Cartesian3.fromDegrees(63.43494882292201, 18.22447811510915, -6376045.535225509), label: {text: "TEST", fillColor: Cesium.Color.RED, scale: 123.0}});'
in rendered_html
)
assert rendered_html.endswith("</script>")

def test_add_line_success(
self,
viewer: Viewer,
):
viewer.add_line(
positions=[
Position.meters([1.0, 2.0, 3.0], Frame.ITRF()),
Position.meters([4.0, 5.0, 6.0], Frame.ITRF()),
],
size=10.0,
color="red",
)

rendered_html: str = viewer.render()

assert rendered_html.startswith('<meta charset="utf-8">')
assert "var widget = new Cesium.Viewer" in rendered_html
assert (
"widget.entities.add({polyline: {positions: Cesium.Cartesian3.fromDegreesArrayHeights([63.43494882292201, 18.22447811510915, 10.0, 51.34019174590991, 10.165199393640696, 10.0]), width: 10.0, material: Cesium.Color.RED}});"
in rendered_html
)
assert rendered_html.endswith("</script>")

def test_add_label_success(
self,
viewer: Viewer,
):
viewer.add_label(
position=Position.meters([6671000.0, 0.0, 0.0], Frame.ITRF()),
text="Hello, World!",
size=1.0,
color="red",
)

rendered_html: str = viewer.render()

import IPython

IPython.embed()

assert rendered_html.startswith('<meta charset="utf-8">')
assert "var widget = new Cesium.Viewer" in rendered_html
assert (
'widget.entities.add({position: Cesium.Cartesian3.fromDegrees(63.43494882292201, 18.22447811510915, -6376045.535225509), label: {text: "Hello, World!", fillColor: Cesium.Color.RED, scale: 10.0}});'
in rendered_html
)
assert rendered_html.endswith("</script>")
32 changes: 32 additions & 0 deletions bindings/python/tools/python/ostk/astrodynamics/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def add_target(
position: Position,
size: int | None = None,
color: str | None = None,
label: str | None = None,
) -> None:
"""
Add target to Viewer.
Expand All @@ -196,6 +197,7 @@ def add_target(
position (Position): Target position.
size (int, optional): Target size. Defaults to None.
color (str, optional): Target color. Defaults to None.
label (str, optional): Target label. Defaults to None.
"""

self._viewer.entities.add(
Expand All @@ -206,6 +208,9 @@ def add_target(
)
)

if label:
self.add_label(position, label, size, color)

def add_line(
self,
positions: list[Position],
Expand Down Expand Up @@ -242,6 +247,33 @@ def add_line(
)
)

def add_label(
self,
position: Position,
text: str,
size: int | None = None,
color: str | None = None,
) -> None:
"""
Add label to Viewer.

Args:
position (Position): Label position.
text (str): Label text.
size (int, optional): Label size. Defaults to None.
color (str, optional): Label color. Defaults to None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

"""

self._viewer.entities.add(
cesiumpy.Label(
position=_cesium_from_ostk_position(position),
text=text,
scale=size or 10,
fill_color=color or cesiumpy.color.WHITE,
)
)

def render(self) -> str:
"""
Render Viewer as HTML string.
Expand Down
Loading