-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaza_route_dockwidget.py
207 lines (167 loc) · 8.16 KB
/
plaza_route_dockwidget.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
PlazaRouteDockWidget
A QGIS plugin
Pedestrian and Public Transport Routing
-------------------
begin : 2017-11-14
git sha : $Format:%H$
copyright : (C) 2017 by Jonas Matter / Robin Suter
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
import ConfigParser
from PyQt4 import QtGui, uic
from PyQt4.QtCore import pyqtSignal, Qt, QTime
from qgis.gui import QgsMessageBar
from util import validator as validator
from util.point_transformer import PointTransformer
from observer import Observer
from plaza_route_map_tool import PlazaRouteMapTool
from plaza_route_directions_generator import PlazaRouteDirectionsGenerator
from plaza_route_routing_service import PlazaRouteRoutingService
FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'plaza_route_dockwidget_base.ui'))
class PlazaRouteDockWidget(QtGui.QDockWidget, FORM_CLASS):
closingPlugin = pyqtSignal()
coordinate_source = None # start or destination
def __init__(self, iface, parent=None):
super(PlazaRouteDockWidget, self).__init__(parent)
self.setupUi(self)
self.iface = iface
self.canvas = self.iface.mapCanvas()
self.cross_cursor = QtGui.QCursor(Qt.CrossCursor)
self.config_file_path = os.path.join(os.path.dirname(__file__), 'config.ini')
self.config = ConfigParser.ConfigParser()
self.config.read(self.config_file_path)
self.plaza_route_routing_service = PlazaRouteRoutingService(self._handle_route, self._handle_error, self.config)
self.routing_generator = PlazaRouteDirectionsGenerator()
self.point_transformer = PointTransformer(self.iface)
self.map_tool = PlazaRouteMapTool(self.iface, self.point_transformer)
self.map_tool.attach(self)
self.canvas.setMapTool(self.map_tool)
self._register_events()
self._reset()
def closeEvent(self, event):
self.closingPlugin.emit()
self._reset()
event.accept()
def update(self, arg):
update_type = arg['type']
value = arg['value']
if update_type == 'coordinate_update':
self.coordinate_source = \
value['coordinate_source'] if 'coordinate_source' in value else self.coordinate_source
self._set_coordinate(value['coordinate'], self.coordinate_source)
elif update_type == 'map_tool_event':
self.coordinate_source = None
def _register_events(self):
self.start_select_btn.clicked.connect(self._select_start)
self.destination_select_btn.clicked.connect(self._select_destination)
self.departure_refresh_btn.clicked.connect(self._refresh_departure)
self.reset_btn.clicked.connect(self._reset)
self.show_route_btn.clicked.connect(self._show_route)
self.config_btn.clicked.connect(self._show_config_dialog)
def _show_route(self):
QtGui.QApplication.setOverrideCursor(Qt.WaitCursor)
if not self._validate_routing_params():
QtGui.QApplication.restoreOverrideCursor()
return
if validator.is_address(self.start_value.text()):
start = self.start_value.text()
else:
start = self.point_transformer.transform_project_to_base_crs_str(
self.point_transformer.str_to_point(self.start_value.text()))
if validator.is_address(self.destination_value.text()):
destination = self.destination_value.text()
else:
destination = self.point_transformer.transform_project_to_base_crs_str(
self.point_transformer.str_to_point(self.destination_value.text()))
departure = self.departure_value.text()
precise_public_transport_stops = self.precise_public_transport_stops_cb.isChecked()
self.plaza_route_routing_service.get_route(start, destination, departure, precise_public_transport_stops)
def _validate_routing_params(self):
if validator.has_empty_fields(self.start_value.text(),
self.destination_value.text(),
self.departure_value.text()):
self._add_qgis_msg(validator.ERROR_MSGS['empty_field'])
return False
if not validator.is_valid_project_coordinate(self.start_value.text(), self.point_transformer) or \
not validator.is_valid_project_coordinate(self.destination_value.text(), self.point_transformer):
self._add_qgis_msg(validator.ERROR_MSGS['invalid_coordinate'])
return False
if not validator.is_valid_departure(self.departure_value.text()):
self._add_qgis_msg(validator.ERROR_MSGS['invalid_departure'])
return False
return True
def _handle_route(self, route):
try:
self.map_tool.draw_route(route)
self._add_routing(route)
finally:
QtGui.QApplication.restoreOverrideCursor()
def _handle_error(self, msg):
try:
self._add_qgis_msg(msg)
finally:
QtGui.QApplication.restoreOverrideCursor()
def _add_routing(self, route):
self.direction_value.clear()
self.direction_value.append(self.routing_generator.generate_directions(route))
def _set_coordinate(self, point, source):
if not source:
return
coordinate = "{}, {}".format(point.x(), point.y())
if source == 'start':
self.start_value.setText(coordinate)
elif source == 'destination':
self.destination_value.setText(coordinate)
self.map_tool.set_coordinate(point, source)
def _select_start(self):
self.coordinate_source = 'start'
self._handle_crosshairs_selection()
def _select_destination(self):
self.coordinate_source = 'destination'
self._handle_crosshairs_selection()
def _handle_crosshairs_selection(self):
self.canvas.setMapTool(self.map_tool)
self.canvas.setCursor(self.cross_cursor)
def _refresh_departure(self):
current_time = QTime()
current_time.start()
self.departure_value.setTime(current_time)
def _reset(self):
self.map_tool.reset_map()
self._refresh_departure()
self.start_value.clear()
self.destination_value.clear()
self.direction_value.clear()
def _show_config_dialog(self):
config_dialog = self._create_config_dialog()
ok = config_dialog.exec_()
plaza_routing_url = config_dialog.textValue()
if ok:
self.config.set('plazaroute', 'plaza_routing_url', plaza_routing_url)
with open(self.config_file_path, 'wb') as config_file:
self.config.write(config_file)
self.plaza_route_routing_service.update_config(self.config)
def _create_config_dialog(self):
config_dialog = QtGui.QInputDialog(self)
config_dialog.setInputMode(QtGui.QInputDialog.TextInput)
config_dialog.setWindowTitle('Configuration')
config_dialog.setLabelText('Plaza Routing URL:')
config_dialog.setTextValue(self.config.get('plazaroute', 'plaza_routing_url'))
config_dialog.resize(500, 100)
return config_dialog
def _add_qgis_msg(self, msg, level=QgsMessageBar.CRITICAL):
self.iface.messageBar().pushMessage('Error', msg, level=level)
Observer.register(PlazaRouteDockWidget)