Browse Source

code structure: add gui and core submodules

feature/processing_qgs2
noerw 1 year ago
parent
commit
07079ec2c8

+ 4
- 6
Makefile View File

@@ -37,21 +37,19 @@ LOCALES =
37 37
 PLUGINNAME = OSMtools
38 38
 
39 39
 PY_FILES = \
40
-	auxiliary.py  dialog.py      geocode.py     matrix.py     resources.py \
41
-	client.py     directions.py  __init__.py    osm_tools.py  resources_rc.py \
42
-	convert.py    exceptions.py  isochrones.py  pointtool.py \
43
-	osmtools_processing
40
+	__init__.py osm_tools.py \
41
+	core gui osmtools_processing
44 42
 
45 43
 # translation
46 44
 SOURCES = $(PY_FILES)
47 45
 
48
-UI_FILES = *.ui
46
+UI_FILES = gui/*.ui
49 47
 
50 48
 EXTRAS = metadata.txt *.png config.yml
51 49
 
52 50
 EXTRA_DIRS =
53 51
 
54
-COMPILED_RESOURCE_FILES = resources.py resources_rc.py
52
+COMPILED_RESOURCE_FILES = gui/resources.py gui/resources_rc.py
55 53
 
56 54
 PEP8EXCLUDE=pydev,resources.py,conf.py,third_party,ui
57 55
 

+ 1
- 0
__init__.py View File

@@ -35,6 +35,7 @@ def classFactory(iface):  # pylint: disable=invalid-name
35 35
     from .osm_tools import OSMtools
36 36
     return OSMtools(iface)
37 37
 
38
+# TODO: test if this actually works on QGIS Server?
38 39
 # noinspection PyDocstring,PyPep8Naming
39 40
 def serverClassFactory(serverIface):
40 41
     from osmtools_processing.provider import OSMtoolsAlgoProvider

+ 0
- 0
core/__init__.py View File


auxiliary.py → core/auxiliary.py View File

@@ -21,10 +21,10 @@ script_dir = os.path.dirname(os.path.abspath(__file__))
21 21
 def checkCRS(layer, messageBar):
22 22
     """
23 23
     Check if layer CRS is EPSG:4326.
24
-    
24
+
25 25
     :param layer: Layer to be inspected.
26 26
     :type layer: QgsMapLayer
27
-    
27
+
28 28
     :param messageBar: QGIS interface message bar.
29 29
     :type messageBar: QgsMessageBar
30 30
     """
@@ -34,7 +34,7 @@ def checkCRS(layer, messageBar):
34 34
         messageBar.pushInfo('CRS conflict',
35 35
                                          'The input layer CRS is {}, the output layer '
36 36
                                          'CRS will be EPSG:4326'.format(layer_crs))
37
-    
37
+
38 38
     return layer
39 39
 
40 40
 def transformToWGS(old_layer, old_crs):
@@ -48,29 +48,29 @@ def transformToWGS(old_layer, old_crs):
48 48
         g.transform(xform)
49 49
         f.setGeometry(g)
50 50
         feats.append(f)
51
-    
51
+
52 52
     new_layer.dataProvider().addFeatures(feats)
53 53
     attrs = old_layer.dataProvider().fields().toList()
54 54
     new_layer.dataProvider().addAttributes(attrs)
55
-    new_layer.updateFields()    
56
-    
55
+    new_layer.updateFields()
56
+
57 57
     return new_layer
58 58
 
59 59
 
60 60
 def readConfig():
61
-    with open(os.path.join(script_dir, "config.yml")) as f:
61
+    with open(os.path.join(script_dir, '../config.yml')) as f:
62 62
         doc = yaml.safe_load(f)
63
-        
63
+
64 64
     return doc
65 65
 
66 66
 def writeConfig(key, value):
67
-    
67
+
68 68
     doc = readConfig()
69 69
     doc[key] = value
70
-    with open(os.path.join(script_dir, "config.yml"), 'w') as f:
70
+    with open(os.path.join(script_dir, '../config.yml'), 'w') as f:
71 71
         yaml.safe_dump(doc, f)
72
-        
73
-        
72
+
73
+
74 74
 def pushProgressBar(iface):
75 75
     progressMessageBar = iface.messageBar().createMessage("Requesting analysis from ORS...")
76 76
     progress = QProgressBar(progressMessageBar)
@@ -78,5 +78,5 @@ def pushProgressBar(iface):
78 78
     progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
79 79
     progressMessageBar.layout().addWidget(progress)
80 80
     iface.messageBar().pushWidget(progressMessageBar, level=iface.messageBar().INFO)
81
-    
82
-    return progress, progressMessageBar
81
+
82
+    return progress, progressMessageBar

client.py → core/client.py View File

@@ -15,7 +15,7 @@ import collections
15 15
 from urllib.parse import urlencode
16 16
 
17 17
 import OSMtools
18
-from OSMtools import exceptions, auxiliary
18
+from OSMtools.core import exceptions, auxiliary
19 19
 
20 20
 _USER_AGENT = "ORSClientQGIS/%s".format(OSMtools.__version__)
21 21
 _RETRIABLE_STATUSES = [503]

convert.py → core/convert.py View File


exceptions.py → core/exceptions.py View File


geocode.py → core/geocode.py View File

@@ -6,20 +6,20 @@ Created on Sun Feb 18 00:49:36 2018
6 6
 @author: nilsnolde
7 7
 """
8 8
 
9
-from OSMtools import convert
9
+from OSMtools.core import convert
10 10
 
11 11
 def reverse_geocode(client, point_in):
12 12
     params = dict()
13 13
     point_in_list = convert._comma_list([point_in.x(), point_in.y()])
14 14
     params['location'] = point_in_list
15
-    
15
+
16 16
     try:
17 17
         response = client.request('/geocoding', params)['features'][0]
18 18
     except:
19 19
         raise #ValueError("Your input coordinates are invalid for geocoding.")
20
-    
20
+
21 21
     response_dict = dict()
22
-    
22
+
23 23
     x, y = response['geometry'].get('coordinates',None)
24 24
     response_dict['Lon'] = x
25 25
     response_dict['Lat'] = y
@@ -29,5 +29,5 @@ def reverse_geocode(client, point_in):
29 29
     response_dict['POSTALCODE'] = response['properties'].get('postal_code', None)
30 30
     response_dict['STREET'] = response['properties'].get('street', None)
31 31
     response_dict['NUMBER'] = response['properties'].get('house_number', None)
32
-                       
33
-    return response_dict
32
+
33
+    return response_dict

+ 0
- 0
gui/__init__.py View File


dialog.py → gui/dialog.py View File

@@ -31,14 +31,15 @@ from PyQt4.QtGui import (QPixmap,
31 31
                          QApplication,
32 32
                          QComboBox,
33 33
                          QPushButton,)
34
-from qgis.core import (QgsProject, 
35
-                       QgsLayerTreeLayer, 
34
+from qgis.core import (QgsProject,
35
+                       QgsLayerTreeLayer,
36 36
                        QgsMapLayer,
37 37
                        QGis,
38 38
                        QgsMapLayerRegistry
39 39
                        )
40 40
 
41
-from OSMtools import pointtool, geocode, auxiliary, client
41
+from OSMtools.gui import pointtool
42
+from OSMtools.core import auxiliary, client, geocode
42 43
 
43 44
 FORM_CLASS, _ = loadUiType(os.path.join(
44 45
     os.path.dirname(__file__), 'osm_tools_dialog_base.ui'))
@@ -60,7 +61,7 @@ preferences = ['fastest', 'shortest']
60 61
 units = ['time', 'distance']
61 62
 
62 63
 # For matrix API only
63
-metrics =['duration', 'distance', 'distance|duration'] 
64
+metrics =['duration', 'distance', 'distance|duration']
64 65
 
65 66
 class OSMtoolsDialog(QDialog, FORM_CLASS):
66 67
     def __init__(self, iface):
@@ -74,11 +75,11 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
74 75
         self.setupUi(self)
75 76
         self.iface = iface
76 77
         self.script_dir = os.path.dirname(os.path.abspath(__file__))
77
-        
78
+
78 79
         self.mapInstance = QgsMapLayerRegistry.instance()
79
-        
80
+
80 81
         # Programmtically invoke ORS logo
81
-        header_pic = QPixmap(os.path.join(self.script_dir, "openrouteservice.png"))
82
+        header_pic = QPixmap(os.path.join(self.script_dir, "../openrouteservice.png"))
82 83
         self.pixmap = header_pic.scaled(150, 50,
83 84
                                         aspectRatioMode=Qt.KeepAspectRatio,
84 85
                                         transformMode=Qt.SmoothTransformation
@@ -86,41 +87,41 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
86 87
         self.header_pic.setPixmap(self.pixmap)
87 88
         self.header_text.setAlignment(Qt.AlignHCenter)
88 89
         self.header_subpic.setAlignment(Qt.AlignHCenter)
89
-        
90
+
90 91
         # Set fonts for few widgets, which dialog.ui somehow messes up
91 92
         self.tabWidget.setFont(QFont("Sans Serif", 10))
92 93
 #        self.groupBox_2.setFont(QFont("Sans Serif", 9))
93
-        
94
+
94 95
         # Read API key file
95 96
         self.api_key_dlg.setText(auxiliary.readConfig()['api_key'])
96
-            
97
+
97 98
         self.api_key = self.api_key_dlg.text()
98 99
         self.project = QgsProject.instance()
99
-        
100
+
100 101
         self.route_mode_combo.addItems(profiles)
101 102
         self.access_mode_combo.addItems(profiles)
102 103
         self.matrix_mode_combo.addItems(profiles)
103 104
 #        self.matrix_metric_combo.addItems(metrics)
104 105
         self.route_pref_combo.addItems(preferences)
105 106
         self.access_unit_combo.addItems(units)
106
-        
107
+
107 108
         #### Set up signals/slots ####
108
-        
109
+
109 110
         # API key text line
110 111
         self.api_key_dlg.textChanged.connect(self._keyWriter)
111
-        
112
+
112 113
         # Matrix tab
113 114
         self.matrix_start_combo.currentIndexChanged.connect(self._layerSeletedChanged)
114 115
         self.matrix_end_combo.currentIndexChanged.connect(self._layerSeletedChanged)
115 116
         self.matrix_start_refresh.clicked.connect(self._layerTreeChanged)
116 117
         self.matrix_end_refresh.clicked.connect(self._layerTreeChanged)
117
-        
118
+
118 119
         # Isochrone tab
119 120
         self.access_map_button.clicked.connect(self._initMapTool)
120 121
         self.access_unit_combo.currentIndexChanged.connect(self._unitChanged)
121 122
         self.access_layer_check.stateChanged.connect(self._accessLayerChanged)
122
-        self.access_layer_refresh.clicked.connect(self._layerTreeChanged) 
123
-        
123
+        self.access_layer_refresh.clicked.connect(self._layerTreeChanged)
124
+
124 125
         # Routing tab
125 126
         self.start_map_button.clicked.connect(self._initMapTool)
126 127
         self.via_map_button.clicked.connect(self._initMapTool)
@@ -129,39 +130,39 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
129 130
         self.end_buttongroup.buttonReleased[int].connect(self._mappingMethodChanged)
130 131
         self.via_clear_button.clicked.connect(self._clearVia)
131 132
         self.mapInstance.layerWasAdded.connect(self._layerTreeChanged)
132
-        self.mapInstance.layersRemoved.connect(self._layerTreeChanged) 
133
-        self.start_layer_refresh.clicked.connect(self._layerTreeChanged) 
134
-        self.end_layer_refresh.clicked.connect(self._layerTreeChanged) 
133
+        self.mapInstance.layersRemoved.connect(self._layerTreeChanged)
134
+        self.start_layer_refresh.clicked.connect(self._layerTreeChanged)
135
+        self.end_layer_refresh.clicked.connect(self._layerTreeChanged)
135 136
         self.start_layer_combo.currentIndexChanged[int].connect(self._layerSeletedChanged)
136 137
         self.end_layer_combo.currentIndexChanged[int].connect(self._layerSeletedChanged)
137
-        
138
+
138 139
 
139 140
     def _accessLayerChanged(self):
140
-        for child in self.sender().parentWidget().children():  
141
+        for child in self.sender().parentWidget().children():
141 142
             if not child.objectName() == self.sender().objectName():
142 143
                 child.setEnabled(self.sender().isChecked())
143
-            
144
-    
144
+
145
+
145 146
     def _layerTreeChanged(self):
146 147
         """
147
-        Re-populate layers for dropdowns dynamically when layers were 
148
+        Re-populate layers for dropdowns dynamically when layers were
148 149
         added/removed.
149 150
         """
150
-        
151
+
151 152
         # First get all point layers in map canvas
152 153
         layer_names = []
153 154
         root = self.project.layerTreeRoot()
154 155
         for child in root.children():
155 156
             if isinstance(child, QgsLayerTreeLayer):
156 157
                 layer = child.layer()
157
-                # Handle weird project startup behaviour of QGIS (apparently 
158
+                # Handle weird project startup behaviour of QGIS (apparently
158 159
                 # doesn't find layers on project startup and throws AttributeError)
159 160
                 try:
160 161
                     if layer.type() == QgsMapLayer.VectorLayer and layer.wkbType() == QGis.WKBPoint:
161 162
                         layer_names.append(layer.name())
162 163
                 except AttributeError:
163 164
                     continue
164
-                
165
+
165 166
         comboboxes = [self.start_layer_combo,
166 167
                       self.end_layer_combo,
167 168
                       self.access_layer_combo,
@@ -175,67 +176,67 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
175 176
                 box.addItem(layer)
176 177
             new_text_id = box.findText(old_text)
177 178
             box.setCurrentIndex(new_text_id)
178
-                    
179
-                    
179
+
180
+
180 181
     def _mappingMethodChanged(self, index):
181 182
         """ Generic method to enable/disable all comboboxes and buttons in the
182
-        children of the parent widget of the calling radio button. 
183
-        
183
+        children of the parent widget of the calling radio button.
184
+
184 185
         :param index: Index of the calling radio button within the QButtonGroup.
185 186
         :type index: int
186 187
         """
187 188
         parent_widget = self.sender().button(index).parentWidget()
188 189
         parent_widget_name = parent_widget.objectName()
189 190
         grandparent_widget = parent_widget.parentWidget()
190
-        
191
+
191 192
         for parent in grandparent_widget.children():
192 193
             if parent.objectName() == parent_widget_name:
193 194
                 for child in parent.findChildren((QComboBox, QPushButton)):
194 195
                     child.setEnabled(True)
195
-            else: 
196
+            else:
196 197
                 for child in parent.findChildren((QComboBox, QPushButton)):
197 198
                     child.setEnabled(False)
198
-        
199
+
199 200
         condition = self.end_layer_radio.isChecked() and self.start_layer_radio.isChecked()
200 201
         self.row_by_row.setEnabled(condition)
201 202
         self.many_by_many.setEnabled(condition)
202
-        
203
-        
203
+
204
+
204 205
     def _layerSeletedChanged(self, index):
205 206
         """
206 207
         Populates dropdowns with QgsProject layers.
207
-        
208
+
208 209
         :param index: Index of previously selected layer in dropdown. -1 if no
209 210
             layer was selected.
210 211
         :type index: int
211 212
         """
212
-        
213
+
213 214
         if index != -1:
214 215
             sending_widget = self.sender()
215 216
             sending_widget_name = sending_widget.objectName()
216 217
             parent_widget = self.sender().parentWidget()
217 218
             layer_selected = [lyr for lyr in self.mapInstance.mapLayers().values() if lyr.name() == sending_widget.currentText()][0]
218 219
             for widget in parent_widget.findChildren(QComboBox):
219
-                if widget.objectName() != sending_widget_name:   
220
+                if widget.objectName() != sending_widget_name:
220 221
                     widget.clear()
221 222
                     widget.addItems([field.name() for field in layer_selected.fields()])
222
-                
223
-    
223
+
224
+
224 225
     def _clearVia(self):
225 226
         """
226 227
         Clears the 'via' coordinates label.
227 228
         """
228 229
         self.via_label.setText("Long,Lat")
229
-            
230
-            
230
+
231
+
231 232
     def _keyWriter(self):
232 233
         """
233 234
         Writes key to text file when api key text field changes.
234 235
         """
235 236
         auxiliary.writeConfig('api_key',
236 237
                                   self.api_key_dlg.text())
237
-           
238
-            
238
+
239
+
239 240
     def _unitChanged(self):
240 241
         """
241 242
         Connector to change unit label text when changing unit
@@ -250,42 +251,42 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
250 251
         """
251 252
         Initialize the mapTool to select coordinates in map canvas.
252 253
         """
253
-        
254
+
254 255
         self.setWindowState(Qt.WindowMinimized)
255 256
         sending_button = self.sender().objectName()
256 257
         self.mapTool = pointtool.PointTool(self.iface.mapCanvas(), sending_button)
257 258
         self.iface.mapCanvas().setMapTool(self.mapTool)
258 259
         self.mapTool.canvasClicked.connect(self._writeCoordinateLabel)
259
-        
260
-        
260
+
261
+
261 262
     # Write map coordinates to text fields
262 263
     def _writeCoordinateLabel(self, point, button):
263 264
         """
264 265
         Writes the selected coordinates from map canvas to its accompanying label.
265
-        
266
+
266 267
         :param point: Point selected with mapTool.
267 268
         :type point: QgsPointXY
268
-        
269
+
269 270
         :param button: Button name which intialized mapTool.
270 271
         :param button: str
271 272
         """
272
-        
273
+
273 274
         x, y = point
274
-        
275
+
275 276
         if button == self.start_map_button.objectName():
276 277
             self.start_map_label.setText("{0:.5f},{1:.5f}".format(x, y))
277
-            
278
+
278 279
         if button == self.end_map_button.objectName():
279 280
             self.end_map_label.setText("{0:.5f},{1:.5f}".format(x, y))
280
-            
281
+
281 282
         if button == self.via_map_button.objectName():
282 283
             self.via_label.setText("{0:.5f},{1:.5f}".format(x, y))
283
-            
284
+
284 285
         if button == self.access_map_button.objectName():
285 286
             clt = client.Client(self.iface)
286 287
             loc_dict = geocode.reverse_geocode(clt,
287 288
                                               point)
288
-            
289
+
289 290
             out_str = u"{0:.6f}\n{1:.6f}\n{2}\n{3}\n{4}".format(loc_dict.get('Lon', ""),
290 291
                                                             loc_dict.get('Lat', ""),
291 292
                                                             loc_dict.get('CITY', "NA"),
@@ -293,9 +294,8 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
293 294
                                                             loc_dict.get('COUNTRY', "NA")
294 295
                                                             )
295 296
             self.access_map_label.setText(out_str)
296
-        
297
+
297 298
         # Restore normal behavior
298 299
         self.showNormal()
299 300
         QApplication.restoreOverrideCursor()
300 301
         self.mapTool.canvasClicked.disconnect()
301
-        

directions.py → gui/directions.py View File

@@ -16,14 +16,14 @@ from PyQt4.QtGui import (QComboBox,
16 16
 from PyQt4.QtCore import QVariant
17 17
 
18 18
 from qgis.core import (QgsVectorLayer,
19
-                       QgsField, 
19
+                       QgsField,
20 20
                        QgsPoint,
21 21
                        QgsGeometry,
22 22
                        QgsFeature,
23 23
                        QgsMapLayerRegistry
24 24
                        )
25 25
 
26
-from OSMtools import convert, geocode, auxiliary
26
+from OSMtools.core import convert, geocode, auxiliary
27 27
 
28 28
 class directions:
29 29
     """
@@ -33,35 +33,35 @@ class directions:
33 33
         """
34 34
         :param dlg: Main OSMtools dialog window.
35 35
         :type dlg: QDialog
36
-        
36
+
37 37
         :param client: Client to ORS API.
38 38
         :type client: OSMtools.client.Client()
39
-        
39
+
40 40
         :param iface: A QGIS interface instance.
41 41
         :type iface: QgisInterface
42 42
         """
43 43
         self.dlg = dlg
44 44
         self.client = client
45 45
         self.iface = iface
46
-        
47
-        self.url = '/directions'        
48
-        
46
+
47
+        self.url = '/directions'
48
+
49 49
         self.radio_buttons = (self.dlg.start_layer_radio,
50 50
                               self.dlg.end_layer_radio)
51
-        
51
+
52 52
         # API parameters
53 53
         self.route_mode = self.dlg.route_mode_combo.currentText()
54 54
         self.route_pref = self.dlg.route_pref_combo.currentText()
55 55
         avoid_boxes = self.dlg.avoid_frame.findChildren(QCheckBox)
56
-        
56
+
57 57
         self.params = {'profile': self.route_mode,
58 58
                     'preference': self.route_pref,
59 59
                     'geometry': 'true',
60 60
                     'geometry_format': 'geojson',
61 61
                     'instructions': 'false'
62 62
                     }
63
-        
64
-        
63
+
64
+
65 65
         # Check if avoid features is checked
66 66
         self.avoid_dict = dict()
67 67
         if any(box.isChecked() for box in avoid_boxes):
@@ -71,44 +71,44 @@ class directions:
71 71
                     avoid_features.append((box.text()))
72 72
             avoid_features = convert._pipe_list(avoid_features)
73 73
             self.avoid_dict = dict()
74
-            
74
+
75 75
             self.avoid_dict['avoid_features'] = avoid_features
76
-        
76
+
77 77
         if self.avoid_dict:
78 78
             self.params['options'] = str(self.avoid_dict)
79
-    
80
-                    
79
+
80
+
81 81
     def directions_calc(self):
82 82
         """
83 83
         Main method to perform the actual request
84 84
         """
85
-        
85
+
86 86
         # create route_dict, {'radio_button_name': {'geometries': list of coords,
87 87
         #                                           'values': list of values}}
88 88
         route_dict = self._selectInput()
89
-        
90
-        # generate lists with locations and values         
89
+
90
+        # generate lists with locations and values
91 91
         (start_layer_name,
92 92
          end_layer_name) = [x.objectName() for x in self.radio_buttons]
93
-        
93
+
94 94
         locations_list = list(product(route_dict[start_layer_name]['geometries'],
95 95
                                       route_dict[end_layer_name]['geometries']))
96 96
         values_list = list(product(route_dict[start_layer_name]['values'],
97 97
                                    route_dict[end_layer_name]['values']))
98
-        
98
+
99 99
         # If row-by-row in two-layer mode, then only zip the locations
100 100
         if all([button.isChecked() for button in self.radio_buttons]) and self.dlg.row_by_row.isChecked():
101 101
             locations_list = list(zip(route_dict[start_layer_name]['geometries'],
102 102
                                           route_dict[end_layer_name]['geometries']))
103 103
             values_list = list(zip(route_dict[start_layer_name]['values'],
104 104
                                        route_dict[end_layer_name]['values']))
105
-    
105
+
106 106
         route_via = None
107 107
         if self.dlg.via_label.text() != 'Long,Lat':
108 108
             route_via = [float(x) for x in self.dlg.via_label.text().split(",")]
109
-                
109
+
110 110
         message_bar, progress_widget = auxiliary.pushProgressBar(self.iface)
111
-        
111
+
112 112
         responses = []
113 113
         delete_values = []
114 114
         for i, coords_tuple in enumerate(locations_list):
@@ -120,39 +120,39 @@ class directions:
120 120
                 # add via coords
121 121
                 coords_tuple = list(coords_tuple)
122 122
                 coords_tuple.insert(1, route_via)
123
-            
123
+
124 124
             # Update progress bar
125 125
             percent = (i/float(len(locations_list))) * 100
126 126
             message_bar.setValue(percent)
127
-            
127
+
128 128
             # Make the request
129 129
             self.params['coordinates'] = convert._build_coords(coords_tuple)
130 130
             responses.append(self.client.request(self.url, self.params))
131
-        
131
+
132 132
         # Delete entries in values_list where coords where the same
133 133
         values_list = [value for idx, value in enumerate(values_list) if idx not in delete_values]
134
-            
134
+
135 135
         # Only proceed when there actual responses
136
-        if responses:        
136
+        if responses:
137 137
             layer_out = self._addLine(responses, values_list)
138 138
             layer_out.updateExtents()
139
-            
139
+
140 140
             QgsMapLayerRegistry.instance().addMapLayer(layer_out)
141
-            
141
+
142 142
         self.iface.messageBar().popWidget(progress_widget)
143
-        
144
-        
143
+
144
+
145 145
     def _addLine(self, responses, values_list):
146 146
         """
147 147
         :param responses: Collection of HTTP responses.
148 148
         :type responses: list
149
-        
149
+
150 150
         :param values_list: List of feature ID's.
151 151
         :type values_list: list
152
-        
152
+
153 153
         :rtype: QgsMapLayer
154 154
         """
155
-        
155
+
156 156
         # Create memory routing layer with fields
157 157
         layer_out = QgsVectorLayer("LineString?crs=EPSG:4326", "Route_ORS", "memory")
158 158
         layer_out_prov = layer_out.dataProvider()
@@ -163,10 +163,10 @@ class directions:
163 163
         layer_out_prov.addAttributes([QgsField("AVOID_TYPE", QVariant.String)])
164 164
         layer_out_prov.addAttributes([QgsField("FROM_ID", QVariant.String)])
165 165
         layer_out_prov.addAttributes([QgsField("TO_ID", QVariant.String)])
166
-        
166
+
167 167
         layer_out.updateFields()
168
-        
169
-        
168
+
169
+
170 170
         for i, response in enumerate(responses):
171 171
             resp_minified = response['routes'][0]
172 172
             feat = QgsFeature()
@@ -184,13 +184,13 @@ class directions:
184 184
                                values_list[i][1]
185 185
                                ])
186 186
             layer_out.dataProvider().addFeatures([feat])
187
-                
187
+
188 188
         return layer_out
189
-                
189
+
190 190
     def _selectInput(self):
191 191
         """
192 192
         Selects start and end features and returns them as a dict.
193
-        
193
+
194 194
         :rtype: dict, {'radio_button_name': {'geometries': list of coords,
195 195
             'values': list of values}, 'other_radio_button':...}
196 196
         """
@@ -199,45 +199,45 @@ class directions:
199 199
             if radio_button.isChecked():
200 200
                 # Find layer combo box
201 201
                 all_combos = radio_button.parent().findChildren(QComboBox)
202
-                layer_combo = [combo for combo in all_combos if combo.objectName().endswith('layer_combo')][0]  
203
-                # Get selected layer                              
202
+                layer_combo = [combo for combo in all_combos if combo.objectName().endswith('layer_combo')][0]
203
+                # Get selected layer
204 204
                 layer_name = layer_combo.currentText()
205 205
                 layer = [layer for layer in self.iface.mapCanvas().layers() if layer.name() == layer_name][0]
206
-                
206
+
207 207
                 # Check CRS and transform if necessary
208 208
                 auxiliary.checkCRS(layer,
209 209
                              self.iface.messageBar())
210
-                
210
+
211 211
                 # If features are selected, calculate with those
212 212
                 if layer.selectedFeatureCount() == 0:
213 213
                     feats = layer.getFeatures()
214 214
                 else:
215 215
                     feats = layer.selectedFeatures()
216
-                    
216
+
217 217
                 # Get features
218 218
                 point_geom = [feat.geometry().asPoint() for feat in feats]
219
-                
219
+
220 220
                 # Find field combo box
221
-                field_combo = [combo for combo in all_combos if combo.objectName().endswith('layer_id')][0] 
221
+                field_combo = [combo for combo in all_combos if combo.objectName().endswith('layer_id')][0]
222 222
                 field_id = layer.fields().indexFromName(field_combo.currentText())
223 223
                 field_values = [feat[field_id] for feat in feats]
224
-                
224
+
225 225
             else:
226 226
                 parent_widget = radio_button.parentWidget()
227 227
                 parent_widget_name = parent_widget.objectName()
228 228
                 grandparent_widget = parent_widget.parentWidget()
229 229
                 parent_widget_label = [child for child in grandparent_widget.children() if child.objectName() != parent_widget_name][1]
230
-                
230
+
231 231
                 point_label = parent_widget_label.findChild(QLabel)
232 232
                 point_coords = [float(x) for x in point_label.text().split(",")]
233
-                
233
+
234 234
                 point_geom = [QgsPoint(*point_coords)]
235 235
                 response_dict = geocode.reverse_geocode(self.client, *point_geom)
236
-                
236
+
237 237
                 field_values = [response_dict.get('CITY', point_label.text())]
238
-            
238
+
239 239
             # Get all id attributes from field
240 240
             route_dict[radio_button.objectName()] = {'geometries': point_geom,
241 241
                                                      'values': field_values}
242
-            
243
-        return route_dict
242
+
243
+        return route_dict

isochrones.py → gui/isochrones.py View File

@@ -19,10 +19,10 @@ from qgis.core import (QgsPoint,
19 19
                        QgsRendererCategoryV2,
20 20
                        QgsCategorizedSymbolRendererV2)
21 21
 
22
-from OSMtools import (geocode,
23
-                      convert,
24
-                      auxiliary
25
-                      )
22
+from OSMtools.core import (geocode,
23
+                           convert,
24
+                           auxiliary
25
+                          )
26 26
 
27 27
 class isochrones:
28 28
     """

matrix.py → gui/matrix.py View File

@@ -19,7 +19,7 @@ from qgis.core import (QgsVectorLayer,
19 19
                        QgsMapLayerRegistry
20 20
                        )
21 21
 
22
-from OSMtools import auxiliary
22
+from OSMtools.core import auxiliary
23 23
 
24 24
 
25 25
 class matrix:
@@ -30,10 +30,10 @@ class matrix:
30 30
         """
31 31
         :param dlg: Main OSMtools dialog window.
32 32
         :type dlg: QDialog
33
-        
33
+
34 34
         :param client: Client to ORS API.
35 35
         :type client: OSMtools.client.Client()
36
-        
36
+
37 37
         :param iface: A QGIS interface instance.
38 38
         :type iface: QgisInterface
39 39
         """
@@ -41,42 +41,42 @@ class matrix:
41 41
         self.client = client
42 42
         self.iface = iface
43 43
         self.plugin_dir = os.path.dirname(__file__)
44
-        
45
-        self.url = '/matrix'        
46
-        
44
+
45
+        self.url = '/matrix'
46
+
47 47
         self.combo_boxes = (self.dlg.matrix_start_combo, self.dlg.matrix_end_combo)
48
-        
48
+
49 49
         # API parameters
50 50
         self.matrix_mode = self.dlg.matrix_mode_combo.currentText()
51 51
 #        self.matrix_metrics = self.dlg.matrix_metric_combo.currentText()
52
-        
52
+
53 53
         self.params = {'profile': self.dlg.matrix_mode_combo.currentText(),
54 54
                     'metrics': 'distance|duration'
55 55
                     }
56
-        
57
-    
56
+
57
+
58 58
     def matrix_calc(self):
59 59
         """
60 60
         Main method to perform the actual request
61 61
         """
62
-        
62
+
63 63
         # create route_dict, {'combobox_name': {'geometries': list of coords,
64 64
         #                                       'values': list of values}}
65 65
         route_dict = self._selectInput()
66
-        
66
+
67 67
         start_box_name  = self.combo_boxes[0].objectName()
68 68
         end_box_name  = self.combo_boxes[1].objectName()
69
-        # generate lists with locations and values      
69
+        # generate lists with locations and values
70 70
         locations_list = route_dict[start_box_name]['geometries'] + \
71 71
                          route_dict[end_box_name]['geometries']
72 72
         values_list = route_dict[start_box_name]['values'] + \
73 73
                          route_dict[end_box_name]['values']
74
-        
74
+
75 75
         ids = list(range(len(locations_list)))
76 76
         destinations_amount = len(route_dict[end_box_name]['geometries'])
77 77
         sources = ids[:destinations_amount]
78
-        destinations = ids[destinations_amount:]        
79
-        
78
+        destinations = ids[destinations_amount:]
79
+
80 80
         values_list = list(product(values_list[:destinations_amount],
81 81
                                    values_list[destinations_amount:]))
82 82
 
@@ -85,35 +85,35 @@ class matrix:
85 85
         self.params['sources'] = sources
86 86
         self.params['destinations'] = destinations
87 87
         response = self.client.request(self.url, {}, post_json=self.params)
88
-        
89
-        
88
+
89
+
90 90
         durations = [item for sublist in response['durations'] for item in sublist]
91 91
         distances = [item for sublist in response['distances'] for item in sublist]
92
-        
92
+
93 93
         layer_out = QgsVectorLayer('None', "Matrix_ORS", "memory")
94 94
         layer_out_prov = layer_out.dataProvider()
95 95
         layer_out_prov.addAttributes([QgsField("FROM_ID", QVariant.String)])
96 96
         layer_out_prov.addAttributes([QgsField("TO_ID", QVariant.String)])
97 97
         layer_out_prov.addAttributes([QgsField("DURATION_HOURS", QVariant.String)])
98 98
         layer_out_prov.addAttributes([QgsField("DISTANCE_KM", QVariant.String)])
99
-        
100
-        for row in range(len(values_list)):    
99
+
100
+        for row in range(len(values_list)):
101 101
             feat = QgsFeature()
102 102
             feat.setAttributes([values_list[row][0],
103 103
                              values_list[row][1],
104 104
                              durations[row]/3600,
105 105
                              distances[row]/1000])
106 106
             layer_out_prov.addFeatures([feat])
107
-        
107
+
108 108
         layer_out.updateFields()
109
-    
109
+
110 110
         QgsMapLayerRegistry.instance().addMapLayer(layer_out)
111
-        
112
-        
111
+
112
+
113 113
     def _selectInput(self):
114 114
         """
115 115
         Selects start and end features and returns them as a dict.
116
-        ues_list)):    
116
+        ues_list)):
117 117
 #            feat = QgsFeature()
118 118
 #            feat.setAttributes([values_list[row][0],
119 119
 #                             values_list[row][1],
@@ -124,34 +124,33 @@ class matrix:
124 124
             'values': list of values}, 'other_radio_button':...}
125 125
         """
126 126
         route_dict = dict()
127
-        for combo in self.combo_boxes: 
128
-            # Get selected layer                              
127
+        for combo in self.combo_boxes:
128
+            # Get selected layer
129 129
             layer_name = combo.currentText()
130 130
             layer = [layer for layer in QgsMapLayerRegistry.instance().mapLayers().values() if layer.name() == layer_name][0]
131
-            
131
+
132 132
             # Check CRS and transform if necessary
133 133
             auxiliary.checkCRS(layer,
134 134
                          self.iface.messageBar())
135
-            
135
+
136 136
             # If features are selected, calculate with those
137 137
             if layer.selectedFeatureCount() == 0:
138 138
                 feats = layer.getFeatures()
139 139
             else:
140 140
                 feats = layer.selectedFeatures()
141
-                
141
+
142 142
             # Get features
143 143
             point_geom = [list(feat.geometry().asPoint()) for feat in feats]
144
-            
144
+
145 145
             # Find field combo box
146 146
             all_combos = combo.parent().findChildren(QComboBox)
147
-            field_combo = [box for box in all_combos if box.objectName().endswith('_id')][0] 
147
+            field_combo = [box for box in all_combos if box.objectName().endswith('_id')][0]
148 148
             field_id = layer.fields().indexFromName(field_combo.currentText())
149 149
             field_values = [feat[field_id] for feat in feats]
150
-            
150
+
151 151
             # Get all id attributes from field
152 152
             route_dict[combo.objectName()] = {'geometries': point_geom,
153 153
                                               'values': field_values}
154
-            
154
+
155 155
         return route_dict
156
-        
157
-        
156
+

osm_tools_dialog_base.ui → gui/osm_tools_dialog_base.ui View File


pointtool.py → gui/pointtool.py View File

@@ -19,39 +19,39 @@ def resolve(name, basepath=None):
19 19
     if not basepath:
20 20
       basepath = os.path.dirname(os.path.realpath(__file__))
21 21
     return os.path.join(basepath, name)
22
-    
23 22
 
24
-class PointTool(QgsMapTool):   
23
+
24
+class PointTool(QgsMapTool):
25 25
     def __init__(self, canvas, button):
26 26
         QgsMapTool.__init__(self, canvas)
27
-        self.canvas = canvas    
27
+        self.canvas = canvas
28 28
         self.button = button
29
-        self.imgdir = resolve('icon_locate.png')
29
+        self.imgdir = resolve('../icon_locate.png')
30 30
         self.cursor = QCursor(QPixmap(self.imgdir).scaledToWidth(24), 12, 12)
31
-        
32
-        #QApplication.setOverrideCursor(QCursor(QPixmap('/icon_locate.png')))
33
-    
31
+
32
+        #QApplication.setOverrideCursor(QCursor(QPixmap('../icon_locate.png')))
33
+
34 34
     canvasClicked = pyqtSignal(['QgsPoint', 'QString', 'Qt::MouseButton'])
35 35
     def canvasReleaseEvent(self, event):
36 36
         #Get the click and emit a transformed point
37
-        
37
+
38 38
         # mapSettings() was only introduced in QGIS 2.4, keep compatibility
39 39
         try:
40 40
             crsSrc = self.canvas.mapSettings().destinationCrs()
41 41
         except:
42 42
             crsSrc = self.canvas.mapRenderer().destinationCrs()
43
-            
43
+
44 44
         crsWGS = QgsCoordinateReferenceSystem(4326)
45
-    
45
+
46 46
         point_oldcrs = self.toMapCoordinates(event.pos())
47
-        
47
+
48 48
         xform = QgsCoordinateTransform(crsSrc, crsWGS)
49 49
         point_newcrs = xform.transform(point_oldcrs)
50
-        
50
+
51 51
         QApplication.restoreOverrideCursor()
52
-        
52
+
53 53
         self.canvasClicked.emit(point_newcrs, self.button, event.button())
54
-        
54
+
55 55
     def activate(self):
56 56
         QApplication.setOverrideCursor(self.cursor)
57 57
 
@@ -62,4 +62,4 @@ class PointTool(QgsMapTool):
62 62
         return False
63 63
 
64 64
     def isEditTool(self):
65
-        return True
65
+        return True

resources.py → gui/resources.py View File


resources.qrc → gui/resources.qrc View File


resources_rc.py → gui/resources_rc.py View File


+ 3
- 2
osm_tools.py View File

@@ -33,8 +33,9 @@ from PyQt4.Qt import PYQT_VERSION_STR
33 33
 from PyQt4.QtGui import QIcon, QAction, QApplication
34 34
 from processing.core.Processing import Processing
35 35
 
36
-from OSMtools.dialog import OSMtoolsDialog
37
-from OSMtools import isochrones, client, directions, exceptions, matrix
36
+from OSMtools.core import client, exceptions
37
+from OSMtools.gui import isochrones, directions, matrix
38
+from OSMtools.gui.dialog import OSMtoolsDialog
38 39
 from OSMtools.osmtools_processing.provider import OSMtoolsAlgoProvider
39 40
 
40 41
 import logging

+ 2
- 2
osmtools_processing/isochrones.py View File

@@ -38,8 +38,8 @@ from processing.core.parameters import ParameterVector, ParameterString, Paramet
38 38
 from processing.core.outputs import OutputVector
39 39
 from processing.tools.dataobjects import getObjectFromUri
40 40
 
41
-from OSMtools.exceptions import InvalidParameterException
42
-from OSMtools.convert import _comma_list
41
+from OSMtools.core.exceptions import InvalidParameterException
42
+from OSMtools.core.convert import _comma_list
43 43
 
44 44
 class IsochronesGeoAlg(GeoAlgorithm):
45 45
     """

Loading…
Cancel
Save