diff --git a/bindings/python/test/test_viewer.py b/bindings/python/test/test_viewer.py index a2bba4b84..79002734a 100644 --- a/bindings/python/test/test_viewer.py +++ b/bindings/python/test/test_viewer.py @@ -127,3 +127,72 @@ def test_add_target_success( in rendered_html ) assert rendered_html.endswith("") + + 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('') + 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("") + + 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('') + 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("") + + 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('') + 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("") diff --git a/bindings/python/tools/python/ostk/astrodynamics/viewer.py b/bindings/python/tools/python/ostk/astrodynamics/viewer.py index 3d983de56..2f8dd75c7 100644 --- a/bindings/python/tools/python/ostk/astrodynamics/viewer.py +++ b/bindings/python/tools/python/ostk/astrodynamics/viewer.py @@ -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. @@ -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( @@ -206,6 +208,9 @@ def add_target( ) ) + if label: + self.add_label(position, label, size, color) + def add_line( self, positions: list[Position], @@ -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.