-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteMappingCtrl.py
344 lines (311 loc) · 11.8 KB
/
routeMappingCtrl.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from route_mapping.config import Config
from route_mapping.factories.guiFactory import GuiFactory
import os, json
from qgis.utils import iface
class RouteMappingCtrl:
ROOT_PATH_ICON = os.path.join(
os.path.dirname(__file__),
'icons'
)
ROOT_PATH_QML_STYLE = os.path.join(
os.path.dirname(__file__),
'qmlStyles'
)
def __init__(self,
qgis,
messageFactory,
guiFactory=GuiFactory(),
):
self.qgis = qgis
self.messageFactory = messageFactory
self.guiFactory = guiFactory
self.actions = []
self.routeGeneratorDock = None
self.captureSourceCoordTool = None
self.captureTargetCoordTool = None
self.pluginToolBar = None
self.pluginToolBar = self.qgis.addToolBar("Ferramentas de Rotas")
self.routeGeneratorDock = self.guiFactory.getWidget('RouteGeneratorDock', mediator=self)
def getRouteGeneratorDock(self):
return self.routeGeneratorDock
def showInfoMessageBox(self, parent, title, message):
messageDlg = self.messageFactory.createMessage('InfoMessageBox')
messageDlg.show(parent, title, message)
def showErrorMessageBox(self, parent, title, message):
messageDlg = self.messageFactory.createMessage('ErrorMessageBox')
messageDlg.show(parent, title, message)
def showQuestionMessageBox(self, parent, title, message):
messageDlg = self.messageFactory.createMessage('QuestionMessageBox')
return messageDlg.show(parent, title, message)
def loadPlugin(self):
for actionSettings in self.getActionSettings():
action = self.qgis.createAction(
actionSettings['toolTip'],
actionSettings['iconPath'],
actionSettings['callback']
)
self.pluginToolBar.addAction(action)
self.actions.append(action)
def getActionSettings(self):
return [
{
'toolTip': 'Gerador de rotas',
'iconPath':os.path.join(self.ROOT_PATH_ICON, 'route.png'),
'callback': self.showRouteGeneratorDock
},
{
'toolTip': 'Restrição de rota',
'iconPath':os.path.join(self.ROOT_PATH_ICON, 'turnRestriction.png'),
'callback': self.activeCreateRelationshipTool
},
{
'toolTip': 'Gerar rede de rotas',
'iconPath':os.path.join(self.ROOT_PATH_ICON, 'transform.png'),
'callback': self.createRouteNetwork
},
{
'toolTip': 'Configurações',
'iconPath':os.path.join(self.ROOT_PATH_ICON, 'config.png'),
'callback': self.showConfigDialog
}
]
def unloadPlugin(self):
self.qgis.activeTool('CreateRelationship', unsetTool=True)
self.cleanRouteMarkers()
self.routeGeneratorDock.close()
iface.mainWindow().removeToolBar( self.pluginToolBar )
def activeCreateRelationshipTool(self):
if not self.validateSettings():
return
routeSettings = self.getRouteSettings()
settings = {
'maxSelection': 2,
'layer': {
'schema': 'edgv',
'name': 'rot_trecho_rede_rodoviaria_l',
'fieldName': 'id'
},
'relationship': {
'schema': 'edgv',
'name': 'rot_restricao',
'fieds': [ 'id_1', 'id_2']
}
}
self.qgis.activeTool('CreateRelationship', settings=settings)
def showRouteGeneratorDock(self):
self.qgis.addDockWidget(self.routeGeneratorDock, side='left')
def cleanRouteMarkers(self):
self.captureSourceCoordTool.resetRubberBand() if self.captureSourceCoordTool else ''
self.captureTargetCoordTool.resetRubberBand() if self.captureTargetCoordTool else ''
def activeCaptureSourceCoordinates(self, setCoordinate):
if self.captureSourceCoordTool:
self.qgis.setMapTool(self.captureSourceCoordTool)
return
settings = {
'callback': setCoordinate,
'svgIconPath': os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'icons',
'sourcePointMarker.svg'
)
}
self.captureSourceCoordTool = self.qgis.activeTool('GetClickCoordinates', settings=settings)
def setSourceCoordToolPoint(self, point):
self.captureSourceCoordTool.setCoordinate(*point) if self.captureSourceCoordTool else ''
def setTargetCoordToolPoint(self, point):
self.captureTargetCoordTool.setCoordinate(*point) if self.captureTargetCoordTool else ''
def activeCaptureTargetCoordinates(self, setCoordinate):
if self.captureTargetCoordTool:
self.qgis.setMapTool(self.captureTargetCoordTool)
return
settings = {
'callback': setCoordinate,
'svgIconPath': os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'icons',
'targetPointMarker.svg'
)
}
self.captureTargetCoordTool = self.qgis.activeTool('GetClickCoordinates', settings=settings)
def buildRouteWithoutRestriction(self,
sourcePoint,
targetPoint,
vehicle
):
if not self.validateSettings():
return
buildRoute = self.qgis.getMapFunction('BuildRouteWithoutRestriction')
routeSettings = self.getRouteSettings()
route = buildRoute.run(
sourcePoint,
targetPoint,
'edgv',
'rot_trecho_rede_rodoviaria_l',
'edgv',
'rot_restricao',
(
routeSettings['dbName'],
routeSettings['dbHost'],
routeSettings['dbPort'],
routeSettings['dbUser'],
routeSettings['dbPass']
),
vehicle,
qmlStyle=self.getFileData(
os.path.join(self.ROOT_PATH_QML_STYLE, 'rota.qml')
)
)
self.showRouteInfo(route)
def buildRoute(self,
sourcePoint,
targetPoint,
vehicle
):
if not self.validateSettings():
return
buildRoute = self.qgis.getMapFunction('BuildRoute')
routeSettings = self.getRouteSettings()
route = buildRoute.run(
sourcePoint,
targetPoint,
'edgv',
'rot_trecho_rede_rodoviaria_l',
'edgv',
'rot_restricao',
(
routeSettings['dbName'],
routeSettings['dbHost'],
routeSettings['dbPort'],
routeSettings['dbUser'],
routeSettings['dbPass']
),
vehicle,
qmlStyle=self.getFileData(
os.path.join(self.ROOT_PATH_QML_STYLE, 'rota.qml')
)
)
self.showRouteInfo(route)
def getFileData(self, filePath):
f = open(filePath)
data = f.read()
f.close()
return data
def showRouteInfo(self, route):
self.routeGeneratorDock.removeAllRouteSteps()
getNumberDecimal = lambda n: float(str(n-int(n))[1:] if str(n-int(n))[1:] != '' else 0)
def formatDistance(totalKm):
km = int(totalKm)
m = int(getNumberDecimal(totalKm)*1000)
return (km, m)
def formatTime(totalHours):
hours = int(totalHours)
minutes = int(getNumberDecimal(totalHours)*60)
seconds = int(getNumberDecimal(getNumberDecimal(totalHours)*60)*60)
return (hours, minutes, seconds)
totalKm = 0
totalHours = 0
for step in route:
name = step['name']
hours = step['hours'] if step['hours'] else 0
time = formatTime(hours)
km = step['distancekm'] if step['distancekm'] else 0
distance = formatDistance(km)
self.routeGeneratorDock.addRouteStepInfo(
name,
distance,
time,
step['initials'],
step['covering'],
step['tracks'],
step['velocity'],
step['note'],
step['wkt']
)
totalKm += km
totalHours += hours
distance = formatDistance(totalKm)
time = formatTime(totalHours)
self.routeGeneratorDock.setRouteInfo(distance, time)
def validateSettings(self):
if self.hasRouteSettings():
return True
self.showErrorMessageBox(
self.qgis.getMainWindow(),
'Erro',
'Preencha as configurações!'
)
return False
def hasRouteSettings(self):
return self.getRouteSettings()
def getRouteSettingsKey(self):
return 'routeSettings:v2'
def getRouteSettings(self):
routeSettings = self.qgis.getSettingsVariable(self.getRouteSettingsKey())
return json.loads(routeSettings) if routeSettings else {}
def setRouteSettings(self, routeSettings):
self.qgis.setSettingsVariable(self.getRouteSettingsKey(), json.dumps(routeSettings))
def showConfigDialog(self):
configDialog = self.guiFactory.getWidget('ConfigDialog', mediator=self)
configDialog.load(self.getRouteSettings()) if self.hasRouteSettings() else ''
configDialog.showUp()
def createRouteNetwork(self, b):
if not self.validateSettings():
return
if not self.showQuestionMessageBox(
self.qgis.getMainWindow(),
'Aviso',
'''<p>Gerar uma rede de rotas?</p>
<p style="color:red">Atenção: caso já exista uma rede de rotas ela será deletada.</p>'''
):
return
buildRouteStructure = self.qgis.getMapFunction('BuildRouteStructure')
routeSettings = self.getRouteSettings()
success = buildRouteStructure.run(
'edgv',
'rot_trecho_rede_rodoviaria_l',
routeSettings['dbName'],
routeSettings['dbHost'],
routeSettings['dbPort'],
routeSettings['dbUser'],
routeSettings['dbPass']
)
messageBox = self.showInfoMessageBox if success else self.showErrorMessageBox
messageBox(
self.qgis.getMainWindow(),
'Aviso' if success else 'Erro',
'Rede gerada com sucesso!' if success else 'Erro ao gerar rede!'
)
def zoomToWkt(self, wkt):
self.qgis.zoomToWkt(wkt)
def loadRouteLayers(self):
loadLayer = self.qgis.getMapFunction('LoadLayer')
routeSettings = self.getRouteSettings()
for lyrSettings in self.getRouteLayerSettings():
loadLayer.run(
lyrSettings['schema'],
lyrSettings['table'],
routeSettings['dbName'],
routeSettings['dbHost'],
routeSettings['dbPort'],
routeSettings['dbUser'],
routeSettings['dbPass'],
lyrSettings['isGeom'],
lyrSettings['styleQml']
)
def getRouteLayerSettings(self):
return [
{
'schema': 'edgv',
'table': 'rot_restricao',
'styleQml': '',
'isGeom': False
},
{
'schema': 'edgv',
'table': 'rot_trecho_rede_rodoviaria_l',
'styleQml': self.getFileData(
os.path.join(self.ROOT_PATH_QML_STYLE, 'rot_trecho_rede_rodoviaria_l.qml')
),
'isGeom': True
}
]