How to display groundtrackplotter's plot like widget with QT library in Python

405 views Asked by At

We develop app which should display earth map with satellite and satellite track. That's how plot looks like

enter image description here

We are using python and PySide6.QT for UI manipulation, for earth map plot with satellite we are using GroundTrackPotter. That library makes plot in browser.

I need put plot in central widget**(check image)

enter image description here

Class for app

class Window(QWidget, QQmlApplicationEngine):
    def __init__(self):
        super().__init__()
        border_layout = BorderLayout()

        plot_3d = self.get_3d_lpot()
        border_layout.addWidget(plot_3d, Position.Center)

        satellite_dropdown = self.create_dropdown_satellite()
        border_layout.addWidget(satellite_dropdown, Position.West)

        satellite_info = self.get_satellite_info()
        border_layout.addWidget(satellite_info, Position.South)

        self.setLayout(border_layout)

        self.setWindowTitle("Satellite tracker")

    @staticmethod
    def create_label(text: str):
        label = QLabel(text)
        label.setFrameStyle(QFrame.Box | QFrame.Raised)
        return label

    @staticmethod
    def create_dropdown_satellite():
        widget = QComboBox()

        widget.addItems(["KITSUNE", "BEESAT", "ITUPSAT"])
        return widget

    @staticmethod
    def get_satellite_info():
        satellite_info = satellite_state.get_satellite_param_string()
        widget = QLabel(satellite_info)
        return widget

    @staticmethod
    def get_3d_lpot():
        *Here I need create widget for earth plot
        return widget

Earth plotting:

gp = GroundtrackPlotter()


def get_orbit_plot(orbit: Orbit):
    # Build spacecraft instance
    satellite_spacecraft = EarthSatellite(orbit, None)
    t_span = time_range(start=orbit.epoch - 1.5 * u.h, periods=150, end=orbit.epoch + 1.5 * u.h)
    # Generate an instance of the plotter, add title and show latlon grid
    gp.update_layout(title="International Space Station groundtrack")

    # Plot previously defined EarthSatellite object
    gp.plot(
        satellite_spacecraft,
        t_span,
        label="Satellite",
        color="red",
        marker={"size": 10, "symbol": "triangle-right", "line": {"width": 1, "color": "black"}},
    )
    # For building geo traces
    import plotly.graph_objects as go

    # Faculty of Radiophysics and Computer Technologies coordinates
    STATION = [53.83821551524637, 27.476136409973797] * u.deg

    # Let us add a new trace in original figure
    gp.add_trace(
        go.Scattergeo(
            lat=STATION[0],
            lon=STATION[-1],
            name="Faculty of Radiophysics and Computer Technologies",
            marker={"color": "blue"},
        )
    )
    gp.fig.show()
    # Switch to three dimensional representation
    gp.update_geos(projection_type="orthographic")

    gp.fig.show()


def get_gp_value():
    return gp
1

There are 1 answers

0
eyllanesc On BEST ANSWER

You can generate html using Figure from plotly and embed it in a QWebEngineView:

from astropy import units as u

from poliastro.earth import EarthSatellite
from poliastro.earth.plotting import GroundtrackPlotter
from poliastro.examples import iss
from poliastro.util import time_range

import plotly

from PySide6.QtWidgets import QApplication
from PySide6.QtWebEngineWidgets import QWebEngineView


def build_plot(fig):
    html = "".join(
        [
            "<html><body>",
            plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn"),
            "</body></html>",
        ]
    )
    return html


def create_plot():

    satellite_spacecraft = EarthSatellite(iss, None)
    t_span = time_range(iss.epoch - 1.5 * u.h, periods=150, end=iss.epoch + 1.5 * u.h)

    gp = GroundtrackPlotter()
    gp.update_layout(title="International Space Station groundtrack")

    # Plot previously defined EarthSatellite object
    gp.plot(
        satellite_spacecraft,
        t_span,
        label="Satellite",
        color="red",
        marker={"size": 10, "symbol": "triangle-right", "line": {"width": 1, "color": "black"}},
    )
    # For building geo traces
    import plotly.graph_objects as go

    # Faculty of Radiophysics and Computer Technologies coordinates
    STATION = [53.83821551524637, 27.476136409973797] * u.deg

    # Let us add a new trace in original figure
    gp.add_trace(
        go.Scattergeo(
            lat=STATION[0],
            lon=STATION[-1],
            name="Faculty of Radiophysics and Computer Technologies",
            marker={"color": "blue"},
        )
    )
    # Switch to three dimensional representation
    gp.update_geos(projection_type="orthographic")
    return gp.fig


def main():
    app = QApplication([])

    fig = create_plot()
    html = build_plot(fig)

    view = QWebEngineView()
    view.setHtml(html)
    view.resize(640, 480)
    view.show()

    app.exec()


if __name__ == "__main__":
    main()