用python编写一个文件格式转化工具,要请求可以将nmea文件转为谷歌地图识别的kml文件;并以PYQT5实现可视化
时间: 2024-03-20 19:43:53 浏览: 139
谷歌地图KML文件制作工具
以下是一个简单的Python NMEA文件格式转换工具,它可以将NMEA文件转换为KML格式,以便在Google地图中识别。我还提供了一个基本的PyQt5界面,让用户可以选择要转换的NMEA文件并将结果保存为KML文件。这只是一个起点,你可以根据需要进行修改和扩展。
注意:在运行此代码之前,请确保已经安装了PyQt5和pyproj(用于坐标转换)。
```python
import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QLabel, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from pyproj import Proj
from datetime import datetime
class NmeaToKmlConverter(QWidget):
def __init__(self):
super().__init__()
self.title = 'NMEA to KML Converter'
self.left = 100
self.top = 100
self.width = 400
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# File selection button
select_file_button = QPushButton('Select NMEA File', self)
select_file_button.setToolTip('Select the NMEA file to convert')
select_file_button.move(50, 50)
select_file_button.clicked.connect(self.select_file)
# Convert button
convert_button = QPushButton('Convert to KML', self)
convert_button.setToolTip('Convert the selected NMEA file to KML')
convert_button.move(200, 50)
convert_button.clicked.connect(self.convert_to_kml)
# Selected file label
self.selected_file_label = QLabel(self)
self.selected_file_label.move(50, 100)
# Status label
self.status_label = QLabel(self)
self.status_label.move(50, 150)
self.show()
@pyqtSlot()
def select_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_name, _ = QFileDialog.getOpenFileName(self,"Select NMEA File", "","NMEA Files (*.nmea)", options=options)
if file_name:
self.selected_file_label.setText(f"Selected file: {file_name}")
@pyqtSlot()
def convert_to_kml(self):
nmea_file = self.selected_file_label.text().replace("Selected file: ", "")
if not nmea_file:
self.status_label.setText("Please select an NMEA file")
return
kml_file = os.path.splitext(nmea_file)[0] + ".kml"
with open(nmea_file, "r") as nmea:
with open(kml_file, "w") as kml:
kml.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
kml.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n")
kml.write("<Document>\n")
kml.write("<name>NMEA Track</name>\n")
kml.write("<description>Converted from NMEA file</description>\n")
kml.write("<Style id=\"lineStyle\">\n")
kml.write("<LineStyle>\n")
kml.write("<color>7f00ffff</color>\n")
kml.write("<width>4</width>\n")
kml.write("</LineStyle>\n")
kml.write("</Style>\n")
kml.write("<Placemark>\n")
kml.write("<styleUrl>#lineStyle</styleUrl>\n")
kml.write("<LineString>\n")
kml.write("<extrude>1</extrude>\n")
kml.write("<altitudeMode>absolute</altitudeMode>\n")
kml.write("<coordinates>\n")
proj = Proj(proj='latlong', datum='WGS84')
for line in nmea:
parts = line.split(",")
if parts[0] == "$GPRMC":
try:
lat = float(parts[3][:2]) + float(parts[3][2:])/60
if parts[4] == "S":
lat *= -1
lon = float(parts[5][:3]) + float(parts[5][3:])/60
if parts[6] == "W":
lon *= -1
coords = proj(lon, lat)
kml.write(f"{coords[0]},{coords[1]},0\n")
except:
pass
kml.write("</coordinates>\n")
kml.write("</LineString>\n")
kml.write("</Placemark>\n")
kml.write("</Document>\n")
kml.write("</kml>\n")
self.status_label.setText(f"KML file saved as {kml_file}")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = NmeaToKmlConverter()
sys.exit(app.exec_())
```
在上面的代码中,我们使用了PyQt5的各种部件来创建一个简单的界面。我们添加了两个按钮,一个用于选择要转换的NMEA文件,另一个用于执行转换。我们还添加了两个标签,一个用于显示当前选择的文件,另一个用于显示转换的状态。
在`select_file`方法中,我们使用`QFileDialog`对话框来选择要转换的NMEA文件,并将其路径保存在`selected_file_label`标签中。
在`convert_to_kml`方法中,我们使用Python的标准文件操作来读取NMEA文件并将其转换为KML格式。我们使用`pyproj`库将经纬度转换为Google地图所需的坐标。最后,我们将生成的KML文件保存在与NMEA文件相同的位置,并将其路径保存在状态标签中。
这是一个非常基本的例子,可能需要根据你的需求进行修改和扩展。
阅读全文