From 6a1d21e4d001ff6d9deef065a1f3c132e60f8178 Mon Sep 17 00:00:00 2001 From: vishwa shah Date: Sat, 4 Jan 2025 02:36:02 +0000 Subject: [PATCH 1/4] feat: add label to viewer --- bindings/python/test/test_viewer.py | 69 +++++++++++++++++++ .../tools/python/ostk/astrodynamics/viewer.py | 32 +++++++++ 2 files changed, 101 insertions(+) diff --git a/bindings/python/test/test_viewer.py b/bindings/python/test/test_viewer.py index a2bba4b84..10de4d233 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([1.0, 2.0, 3.0], Frame.ITRF()), + text="Hello, World!", + 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({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("") 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. From fe156a0eef1d4effe75fe4de4ab5b8e8949f8a85 Mon Sep 17 00:00:00 2001 From: vishwa shah Date: Sat, 4 Jan 2025 02:46:28 +0000 Subject: [PATCH 2/4] chore: update test --- bindings/python/test/test_viewer.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bindings/python/test/test_viewer.py b/bindings/python/test/test_viewer.py index 10de4d233..334c92ff8 100644 --- a/bindings/python/test/test_viewer.py +++ b/bindings/python/test/test_viewer.py @@ -181,14 +181,18 @@ def test_add_label_success( viewer: Viewer, ): viewer.add_label( - position=Position.meters([1.0, 2.0, 3.0], Frame.ITRF()), + position=Position.meters([6671000.0, 0.0, 0.0], Frame.ITRF()), text="Hello, World!", - size=10.0, + size=1.0, color="red", ) rendered_html: str = viewer.render() + import IPython + + IPython.embed() + assert rendered_html.startswith('') assert "var widget = new Cesium.Viewer" in rendered_html assert ( From 40a72989d6ab8f4a0d165e98213083fbad825c0d Mon Sep 17 00:00:00 2001 From: vishwa shah Date: Sat, 4 Jan 2025 14:23:06 +0000 Subject: [PATCH 3/4] chore: remove ipdb --- bindings/python/test/test_viewer.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bindings/python/test/test_viewer.py b/bindings/python/test/test_viewer.py index 334c92ff8..b2c029bac 100644 --- a/bindings/python/test/test_viewer.py +++ b/bindings/python/test/test_viewer.py @@ -189,14 +189,10 @@ def test_add_label_success( rendered_html: str = viewer.render() - import IPython - - IPython.embed() - 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), label: {text: "Hello, World!", fillColor: Cesium.Color.RED, scale: 10.0}});' + 'widget.entities.add({position: Cesium.Cartesian3.fromDegrees(63.43494882292201, 18.22447811510915, -6376045.535225509), label: {text: "Hello, World!", fillColor: Cesium.Color.RED, scale: 1.0}});' in rendered_html ) assert rendered_html.endswith("") From 22c051b639f1b55204cadf218d2cdaa3892e38a1 Mon Sep 17 00:00:00 2001 From: vishwa shah Date: Sat, 4 Jan 2025 14:41:02 +0000 Subject: [PATCH 4/4] chore: fix tests --- bindings/python/test/test_viewer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/python/test/test_viewer.py b/bindings/python/test/test_viewer.py index b2c029bac..79002734a 100644 --- a/bindings/python/test/test_viewer.py +++ b/bindings/python/test/test_viewer.py @@ -192,7 +192,7 @@ def test_add_label_success( 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), label: {text: "Hello, World!", fillColor: Cesium.Color.RED, scale: 1.0}});' + '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("")