-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgui.py
206 lines (157 loc) · 6.84 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import signal
import sys
from lxml import etree
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QFileDialog, QMainWindow
from PyQt5.QtWidgets import QPushButton, QMessageBox, QLabel
from opendriveparser import parse_opendrive
from opendrive2lanelet import Network
from viewer import MainWindow as ViewerWidget
class MainWindow(QWidget):
def __init__(self, argv):
super().__init__()
self.loadedRoadNetwork = None
self._initUserInterface()
self.show()
if len(argv) >= 2:
self.loadOpenDriveFile(argv[1])
self.viewLaneletNetwork()
def _initUserInterface(self):
self.setWindowTitle("OpenDRIVE 2 Lanelets Converter")
self.setFixedSize(560, 345)
self.loadButton = QPushButton('Load OpenDRIVE', self)
self.loadButton.setToolTip('Load a OpenDRIVE scenario within a *.xodr file')
self.loadButton.move(10, 10)
self.loadButton.resize(130, 35)
self.loadButton.clicked.connect(self.openOpenDriveFileDialog)
self.inputOpenDriveFile = QLineEdit(self)
self.inputOpenDriveFile.move(150, 10)
self.inputOpenDriveFile.resize(400, 35)
self.inputOpenDriveFile.setReadOnly(True)
self.statsText = QLabel(self)
self.statsText.move(10, 55)
self.statsText.resize(540, 235)
self.statsText.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.statsText.setTextFormat(Qt.RichText)
self.exportCommonRoadButton = QPushButton('Export as CommonRoad', self)
self.exportCommonRoadButton.move(10, 300)
self.exportCommonRoadButton.resize(170, 35)
self.exportCommonRoadButton.setDisabled(True)
self.exportCommonRoadButton.clicked.connect(self.exportAsCommonRoad)
self.viewOutputButton = QPushButton('View Road Network', self)
self.viewOutputButton.move(190, 300)
self.viewOutputButton.resize(170, 35)
self.viewOutputButton.setDisabled(True)
self.viewOutputButton.clicked.connect(self.viewLaneletNetwork)
def resetOutputElements(self):
self.exportCommonRoadButton.setDisabled(True)
self.viewOutputButton.setDisabled(True)
def openOpenDriveFileDialog(self):
self.resetOutputElements()
path, _ = QFileDialog.getOpenFileName(
self,
"QFileDialog.getOpenFileName()",
"",
"OpenDRIVE files *.xodr (*.xodr)",
options=QFileDialog.Options()
)
if not path:
return
self.loadOpenDriveFile(path)
def loadOpenDriveFile(self, path):
filename = os.path.basename(path)
self.inputOpenDriveFile.setText(filename)
# Load road network and print some statistics
try:
fh = open(path, 'r')
openDriveXml = parse_opendrive(etree.parse(fh).getroot())
fh.close()
except (etree.XMLSyntaxError) as e:
errorMsg = 'XML Syntax Error: {}'.format(e)
QMessageBox.warning(self, 'OpenDRIVE error', 'There was an error during the loading of the selected OpenDRIVE file.\n\n{}'.format(errorMsg), QMessageBox.Ok)
return
except (TypeError, AttributeError, ValueError) as e:
errorMsg = 'Value Error: {}'.format(e)
QMessageBox.warning(self, 'OpenDRIVE error', 'There was an error during the loading of the selected OpenDRIVE file.\n\n{}'.format(errorMsg), QMessageBox.Ok)
return
self.loadedRoadNetwork = Network()
self.loadedRoadNetwork.loadOpenDrive(openDriveXml)
self.statsText.setText("Name: {}<br>Version: {}<br>Date: {}<br><br>OpenDRIVE Version {}.{}<br><br>Number of roads: {}<br>Total length of road network: {:.2f} meters".format(
openDriveXml.header.name if openDriveXml.header.name else "<i>unset</i>",
openDriveXml.header.version,
openDriveXml.header.date,
openDriveXml.header.revMajor,
openDriveXml.header.revMinor,
len(openDriveXml.roads),
sum([road.length for road in openDriveXml.roads])
))
self.exportCommonRoadButton.setDisabled(False)
self.viewOutputButton.setDisabled(False)
def exportAsCommonRoad(self):
if not self.loadedRoadNetwork:
return
path, _ = QFileDialog.getSaveFileName(
self,
"QFileDialog.getSaveFileName()",
"",
"CommonRoad files *.xml (*.xml)",
options=QFileDialog.Options()
)
if not path:
return
try:
fh = open(path, "wb")
fh.write(self.loadedRoadNetwork.exportCommonRoadScenario().export_to_string())
fh.close()
except (IOError) as e:
QMessageBox.critical(self, 'CommonRoad file not created!', 'The CommonRoad file was not exported due to an error.\n\n{}'.format(e), QMessageBox.Ok)
return
QMessageBox.information(self, 'CommonRoad file created!', 'The CommonRoad file was successfully exported.', QMessageBox.Ok)
def viewLaneletNetwork(self):
class ViewerWindow(QMainWindow):
def __init__(self, parent=None):
super(ViewerWindow, self).__init__(parent)
self.viewer = ViewerWidget(self)
self.setCentralWidget(self.viewer)
viewer = ViewerWindow(self)
viewer.viewer.openScenario(self.loadedRoadNetwork.exportCommonRoadScenario())
viewer.show()
# def viewLaneletNetwork(self):
# import matplotlib.pyplot as plt
# from fvks.visualization.draw_dispatch import draw_object
# from fvks.scenario.lanelet import Lanelet as FvksLanelet
# def convert_to_fvks_lanelet(ll):
# return FvksLanelet(
# left_vertices=ll.left_vertices,
# center_vertices=ll.center_vertices,
# right_vertices=ll.right_vertices,
# lanelet_id=ll.lanelet_id
# )
# scenario = self.loadedRoadNetwork.exportCommonRoadScenario(filterTypes=[
# 'driving',
# 'onRamp',
# 'offRamp',
# 'stop',
# 'parking',
# 'special1',
# 'special2',
# 'special3',
# 'entry',
# 'exit',
# ])
# # Visualization
# fig = plt.figure()
# ax = fig.add_subplot(111)
# for lanelet in scenario.lanelet_network.lanelets:
# draw_object(convert_to_fvks_lanelet(lanelet), ax=ax)
# ax.set_aspect('equal', 'datalim')
# plt.axis('off')
# plt.show()
if __name__ == '__main__':
# Make it possible to exit application with ctrl+c on console
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Startup application
app = QApplication(sys.argv)
ex = MainWindow(sys.argv)
sys.exit(app.exec_())