Skip to content

Commit ee86228

Browse files
feat: add ground track to viewer (#550)
* feat: add ground track to viewer * fix: remove wrong argument * test: fix test
1 parent e093722 commit ee86228

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

bindings/python/test/test_viewer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ def test_add_celestial_body_direction_success(
201201
)
202202
assert rendered_html.endswith("</script>")
203203

204+
def test_add_ground_tracks_success(
205+
self,
206+
viewer: Viewer,
207+
orbit: Orbit,
208+
):
209+
viewer.add_ground_tracks(
210+
profile_or_trajectory=orbit,
211+
time_step=Duration.seconds(30.0),
212+
)
213+
214+
rendered_html: str = viewer.render()
215+
216+
assert rendered_html.startswith('<meta charset="utf-8">')
217+
assert "var widget = new Cesium.Viewer" in rendered_html
218+
assert "widget.entities.add({polyline:" in rendered_html
219+
assert rendered_html.endswith("</script>")
220+
204221
def test_add_target_success(
205222
self,
206223
viewer: Viewer,

bindings/python/tools/python/ostk/astrodynamics/viewer.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131

3232
from .converters import coerce_to_datetime
3333
from .utilities import lla_from_position
34+
from .utilities import lla_from_state
35+
from .utilities import position_from_lla
3436

3537
DEFAULT_SATELLITE_IMAGE: str = (
3638
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADJSURBVDhPnZHRDcMgEEMZjVEYpaNklIzSEfLfD4qNnXAJSFWfhO7w2Zc0Tf9QG2rXrEzSUeZLOGm47WoH95x3Hl3jEgilvDgsOQUTqsNl68ezEwn1vae6lceSEEYvvWNT/Rxc4CXQNGadho1NXoJ+9iaqc2xi2xbt23PJCDIB6TQjOC6Bho/sDy3fBQT8PrVhibU7yBFcEPaRxOoeTwbwByCOYf9VGp1BYI1BA+EeHhmfzKbBoJEQwn1yzUZtyspIQUha85MpkNIXB7GizqDEECsAAAAASUVORK5CYII="
3739
)
38-
DEFAULT_STEP_DURATION: Duration = Duration.seconds(1.0)
40+
DEFAULT_STEP_DURATION: Duration = Duration.seconds(10.0)
3941

4042

4143
@dataclass
@@ -376,6 +378,47 @@ def _create_celestial_body_direction_state(
376378

377379
return self
378380

381+
def add_ground_tracks(
382+
self,
383+
profile_or_trajectory: Profile | Trajectory,
384+
time_step: Duration | None = None,
385+
) -> Viewer:
386+
"""
387+
Add ground tracks to the viewer.
388+
389+
Args:
390+
profile_or_trajectory (Profile | Trajectory): The profile or trajectory to be added.
391+
time_step (Duration, optional): The duration of each step in the grid.
392+
Default to None. If None, the default step duration is used.
393+
394+
Returns:
395+
Viewer: The Viewer.
396+
"""
397+
time_step = time_step or DEFAULT_STEP_DURATION
398+
399+
ground_track_positions: list[Position] = []
400+
for state in profile_or_trajectory.get_states_at(
401+
self._interval.generate_grid(time_step)
402+
):
403+
lla: LLA = lla_from_state(state)
404+
ground_track_positions.append(
405+
position_from_lla(
406+
LLA(
407+
latitude=lla.get_latitude(),
408+
longitude=lla.get_longitude(),
409+
altitude=Length.meters(0.0),
410+
)
411+
)
412+
)
413+
414+
self.add_line(
415+
positions=ground_track_positions,
416+
size=1,
417+
color=cesiumpy.color.GRAY,
418+
)
419+
420+
return self
421+
379422
def add_target(
380423
self,
381424
position: Position,
@@ -496,9 +539,7 @@ def _repr_html_(self) -> str:
496539

497540

498541
def _generate_llas(states: list[State]) -> list[LLA]:
499-
return [
500-
lla_from_position(state.get_position(), state.get_instant()) for state in states
501-
]
542+
return list(map(lla_from_state, states))
502543

503544

504545
def _generate_sampled_position_from_llas(

0 commit comments

Comments
 (0)