Skip to content

Commit

Permalink
feat: add label to viewer (#483)
Browse files Browse the repository at this point in the history
* feat: add label to viewer

* chore: update test

* chore: remove ipdb

* chore: fix tests
  • Loading branch information
vishwa2710 authored Jan 4, 2025
1 parent 911b0ba commit d1c4ed4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
69 changes: 69 additions & 0 deletions bindings/python/test/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,72 @@ 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()

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(0.0, 0.0, 292863.0000000001), label: {text: "Hello, World!", fillColor: Cesium.Color.RED, scale: 1.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.
"""

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

0 comments on commit d1c4ed4

Please sign in to comment.