将上面程序进行可视化操作,并有结果输出界面
时间: 2024-03-17 15:47:46 浏览: 118
C程序的可视化
好的,可以使用Python的Matplotlib库来进行可视化操作,使用PyQt5库来实现GUI界面。以下是具体实现:
```python
import sys
import numpy as np
from geopy.distance import geodesic
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('NMEA Accuracy Calculator')
self.setGeometry(100, 100, 800, 600)
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.main_layout = QtWidgets.QGridLayout()
self.central_widget.setLayout(self.main_layout)
self.create_widgets()
self.show()
def create_widgets(self):
self.file1_label = QtWidgets.QLabel('File 1:')
self.file1_lineedit = QtWidgets.QLineEdit()
self.file1_button = QtWidgets.QPushButton('Browse...')
self.file1_button.clicked.connect(lambda: self.browse_file(self.file1_lineedit))
self.file2_label = QtWidgets.QLabel('File 2:')
self.file2_lineedit = QtWidgets.QLineEdit()
self.file2_button = QtWidgets.QPushButton('Browse...')
self.file2_button.clicked.connect(lambda: self.browse_file(self.file2_lineedit))
self.calculate_button = QtWidgets.QPushButton('Calculate')
self.calculate_button.clicked.connect(self.calculate_accuracy)
self.rms_label = QtWidgets.QLabel('RMS:')
self.rms_result = QtWidgets.QLabel()
self.cep95_label = QtWidgets.QLabel('CEP95:')
self.cep95_result = QtWidgets.QLabel()
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.main_layout.addWidget(self.file1_label, 0, 0)
self.main_layout.addWidget(self.file1_lineedit, 0, 1)
self.main_layout.addWidget(self.file1_button, 0, 2)
self.main_layout.addWidget(self.file2_label, 1, 0)
self.main_layout.addWidget(self.file2_lineedit, 1, 1)
self.main_layout.addWidget(self.file2_button, 1, 2)
self.main_layout.addWidget(self.calculate_button, 2, 0, 1, 3)
self.main_layout.addWidget(self.rms_label, 3, 0)
self.main_layout.addWidget(self.rms_result, 3, 1)
self.main_layout.addWidget(self.cep95_label, 4, 0)
self.main_layout.addWidget(self.cep95_result, 4, 1)
self.main_layout.addWidget(self.canvas, 5, 0, 1, 3)
def browse_file(self, lineedit):
file_path, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', '', 'NMEA Files (*.nmea)')
if file_path:
lineedit.setText(file_path)
def calculate_accuracy(self):
file_path1 = self.file1_lineedit.text()
file_path2 = self.file2_lineedit.text()
if not file_path1 or not file_path2:
QtWidgets.QMessageBox.warning(self, 'Warning', 'Please select two NMEA files.')
return
nmea_data1 = self.read_nmea(file_path1)
nmea_data2 = self.read_nmea(file_path2)
positions1 = [(float(d[2]), float(d[4])) for d in nmea_data1]
positions2 = [(float(d[2]), float(d[4])) for d in nmea_data2]
distances = []
for i in range(min(len(positions1), len(positions2))):
distances.append(geodesic(positions1[i], positions2[i]).meters)
rms = np.sqrt(np.mean(np.square(distances)))
cep95 = np.percentile(distances, 95)
self.rms_result.setText('{:.2f} meters'.format(rms))
self.cep95_result.setText('{:.2f} meters'.format(cep95))
self.plot_distances(distances)
def read_nmea(self, file_path):
with open(file_path, 'r') as f:
data = f.readlines()
nmea_data = []
for line in data:
if line.startswith('$GPGGA'):
nmea_data.append(line.split(','))
return nmea_data
def plot_distances(self, distances):
self.figure.clear()
ax = self.figure.add_subplot(111)
ax.hist(distances, bins=50)
ax.set_xlabel('Distance (meters)')
ax.set_ylabel('Frequency')
self.canvas.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
```
上面的代码中,`MainWindow`类继承自`QtWidgets.QMainWindow`,用于创建GUI界面。`create_widgets`方法用于创建界面中的各个控件,`browse_file`方法用于弹出文件选择框,`calculate_accuracy`方法用于计算RMS和CEP95,并绘制距离直方图,`read_nmea`方法用于读取NMEA文件中的数据,`plot_distances`方法用于绘制距离直方图。
最后,调用`MainWindow`类即可运行程序,并进行NMEA文件的水平精度误差计算和可视化操作。
阅读全文