Browse Source

final changes to backport to QGIS v2.x

v2.1
nilsnolde 1 year ago
parent
commit
f394d7d17f
7 changed files with 29 additions and 27 deletions
  1. 2
    2
      auxiliary.py
  2. 1
    1
      client.py
  3. 0
    0
      config.yml
  4. 1
    1
      dialog.py
  5. 6
    6
      directions.py
  6. 12
    10
      isochrones.py
  7. 7
    7
      matrix.py

+ 2
- 2
auxiliary.py View File

@@ -41,7 +41,7 @@ def transformToWGS(old_layer, old_crs):
41 41
     new_layer = QgsVectorLayer("Polygon?crs=EPSG:4326", old_layer.name(), "memory")
42 42
     new_crs = QgsCoordinateReferenceSystem(4326)
43 43
     old_crs = QgsCoordinateReferenceSystem(old_crs)
44
-    xform = QgsCoordinateTransform(old_crs, new_crs, QgsProject.instance())
44
+    xform = QgsCoordinateTransform(old_crs, new_crs)
45 45
     feats = []
46 46
     for f in old_layer.getFeatures():
47 47
         g = f.geometry()
@@ -77,6 +77,6 @@ def pushProgressBar(iface):
77 77
     progress.setMaximum(100)
78 78
     progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
79 79
     progressMessageBar.layout().addWidget(progress)
80
-    iface.messageBar().pushWidget(progressMessageBar, level=Qgis.Info)
80
+    iface.messageBar().pushWidget(progressMessageBar, level=iface.messageBar().INFO)
81 81
     
82 82
     return progress, progressMessageBar

+ 1
- 1
client.py View File

@@ -221,7 +221,7 @@ class Client(object):
221 221
         elif self.base_url != _DEFAULT_BASE_URL:
222 222
             return path + "?" + _urlencode_params(params)
223 223
 
224
-        raise ValueError("No API key specified. "
224
+        raise exceptions.ApiError("No API key specified. "
225 225
                          "Visit https://go.openrouteservice.org/dev-dashboard/ "
226 226
                          "to create one.")
227 227
         

+ 0
- 0
config.yml View File


+ 1
- 1
dialog.py View File

@@ -253,7 +253,7 @@ class OSMtoolsDialog(QDialog, FORM_CLASS):
253 253
         
254 254
         self.setWindowState(Qt.WindowMinimized)
255 255
         sending_button = self.sender().objectName()
256
-        self.mapTool = pointtool.PointTool(self.iface.mapCanvas(), sending_button)        
256
+        self.mapTool = pointtool.PointTool(self.iface.mapCanvas(), sending_button)
257 257
         self.iface.mapCanvas().setMapTool(self.mapTool)
258 258
         self.mapTool.canvasClicked.connect(self._writeCoordinateLabel)
259 259
         

+ 6
- 6
directions.py View File

@@ -20,7 +20,7 @@ from qgis.core import (QgsVectorLayer,
20 20
                        QgsPoint,
21 21
                        QgsGeometry,
22 22
                        QgsFeature,
23
-                       QgsProject
23
+                       QgsMapLayerRegistry
24 24
                        )
25 25
 
26 26
 from OSMtools import convert, geocode, auxiliary
@@ -122,7 +122,7 @@ class directions:
122 122
                 coords_tuple.insert(1, route_via)
123 123
             
124 124
             # Update progress bar
125
-            percent = (i/len(locations_list)) * 100
125
+            percent = (i/float(len(locations_list))) * 100
126 126
             message_bar.setValue(percent)
127 127
             
128 128
             # Make the request
@@ -137,7 +137,7 @@ class directions:
137 137
             layer_out = self._addLine(responses, values_list)
138 138
             layer_out.updateExtents()
139 139
             
140
-            QgsProject.instance().addMapLayer(layer_out)
140
+            QgsMapLayerRegistry.instance().addMapLayer(layer_out)
141 141
             
142 142
         self.iface.messageBar().popWidget(progress_widget)
143 143
         
@@ -174,7 +174,7 @@ class directions:
174 174
             distance = resp_minified['summary']['distance']
175 175
             duration = resp_minified['summary']['duration']
176 176
             qgis_coords = [QgsPoint(x, y) for x, y in coordinates]
177
-            feat.setGeometry(QgsGeometry.fromPolylineXY(qgis_coords))
177
+            feat.setGeometry(QgsGeometry.fromPolyline(qgis_coords))
178 178
             feat.setAttributes(["{0:.3f}".format(distance/1000),
179 179
                                "{0:.3f}".format(duration/3600),
180 180
                                self.route_mode,
@@ -183,7 +183,7 @@ class directions:
183 183
                                values_list[i][0],
184 184
                                values_list[i][1]
185 185
                                ])
186
-            layer_out.dataProvider().addFeature(feat)
186
+            layer_out.dataProvider().addFeatures([feat])
187 187
                 
188 188
         return layer_out
189 189
                 
@@ -219,7 +219,7 @@ class directions:
219 219
                 
220 220
                 # Find field combo box
221 221
                 field_combo = [combo for combo in all_combos if combo.objectName().endswith('layer_id')][0] 
222
-                field_id = layer.fields().lookupField(field_combo.currentText())
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:

+ 12
- 10
isochrones.py View File

@@ -13,7 +13,7 @@ from qgis.core import (QgsPoint,
13 13
                        QgsFeature,
14 14
                        QgsField,
15 15
                        QgsGeometry,
16
-                       QgsProject,
16
+                       QgsMapLayerRegistry,
17 17
                        QgsSymbolV2,
18 18
                        QgsSimpleFillSymbolLayerV2,
19 19
                        QgsRendererCategoryV2,
@@ -118,12 +118,13 @@ class isochrones:
118 118
             
119 119
             point_out = self._addPoint(response_dict, out_point_geom, name_ext)
120 120
             point_out.updateExtents()
121
-            QgsProject.instance().addMapLayer(point_out)        
121
+            QgsMapLayerRegistry.instance().addMapLayer(point_out)
122
+        
122 123
         
123 124
         poly_out.updateExtents()
124 125
         
125 126
         self._stylePoly(poly_out)
126
-        QgsProject.instance().addMapLayer(poly_out)
127
+        QgsMapLayerRegistry.instance().addMapLayer(poly_out)
127 128
 #        self.iface.mapCanvas().zoomToFeatureExtent(poly_out.extent())
128 129
         
129 130
             
@@ -159,7 +160,7 @@ class isochrones:
159 160
         
160 161
         # Add clicked point feature to point feature class
161 162
         point_feat = QgsFeature()
162
-        point_feat.setGeometry(QgsGeometry.fromPointXY(point_geom))
163
+        point_feat.setGeometry(QgsGeometry.fromPoint(point_geom))
163 164
         point_feat.setAttributes([response_dict.get("Lat", None),
164 165
                                 response_dict.get("Lon", None),
165 166
                                 response_dict.get("NAME", None),
@@ -206,10 +207,10 @@ class isochrones:
206 207
                 coordinates = isochrone['geometry']['coordinates']
207 208
                 iso_value = isochrone['properties']['value']
208 209
                 qgis_coords = [QgsPoint(x, y) for x, y in coordinates[0]]
209
-                feat.setGeometry(QgsGeometry.fromPolygonXY([qgis_coords]))
210
+                feat.setGeometry(QgsGeometry.fromPolygon([qgis_coords]))
210 211
                 feat.setAttributes([iso_value / 60 if self.dimension == 'time' else iso_value,
211 212
                                    self.iso_mode])
212
-                poly_out.dataProvider().addFeature(feat)
213
+                poly_out.dataProvider().addFeatures([feat])
213 214
         
214 215
         return poly_out
215 216
     
@@ -227,7 +228,7 @@ class isochrones:
227 228
         else:
228 229
             field_name = 'AA_METERS'
229 230
             legend_suffix = ' m'
230
-        field = layer.fields().lookupField(field_name)
231
+        field = layer.fields().indexFromName(field_name)
231 232
         unique_values = sorted(layer.uniqueValues(field))
232 233
             
233 234
         colors = {0: QColor('#2b83ba'),
@@ -249,7 +250,8 @@ class isochrones:
249 250
         
250 251
             # configure a symbol layer
251 252
             symbol_layer = QgsSimpleFillSymbolLayerV2(color=colors[cid],
252
-                                                    strokeColor=QColor('#000000'))
253
+                                                    #strokeColor=QColor('#000000')
254
+                                                    )
253 255
         
254 256
             # replace default symbol layer with the configured one
255 257
             if symbol_layer is not None:
@@ -265,7 +267,7 @@ class isochrones:
265 267
         
266 268
         # assign the created renderer to the layer
267 269
         if renderer is not None:
268
-            layer.setRenderer(renderer)
269
-        layer.setOpacity(0.5)
270
+            layer.setRendererV2(renderer)
271
+        layer.setLayerTransparency(50)
270 272
         
271 273
         layer.triggerRepaint()

+ 7
- 7
matrix.py View File

@@ -16,7 +16,7 @@ from PyQt4.QtCore import QVariant
16 16
 from qgis.core import (QgsVectorLayer,
17 17
                        QgsField,
18 18
                        QgsFeature,
19
-                       QgsProject
19
+                       QgsMapLayerRegistry
20 20
                        )
21 21
 
22 22
 from OSMtools import auxiliary
@@ -103,11 +103,11 @@ class matrix:
103 103
                              values_list[row][1],
104 104
                              durations[row]/3600,
105 105
                              distances[row]/1000])
106
-            layer_out_prov.addFeature(feat)
106
+            layer_out_prov.addFeatures([feat])
107 107
         
108 108
         layer_out.updateFields()
109 109
     
110
-        QgsProject.instance().addMapLayer(layer_out)
110
+        QgsMapLayerRegistry.instance().addMapLayer(layer_out)
111 111
         
112 112
         
113 113
     def _selectInput(self):
@@ -127,7 +127,7 @@ class matrix:
127 127
         for combo in self.combo_boxes: 
128 128
             # Get selected layer                              
129 129
             layer_name = combo.currentText()
130
-            layer = [layer for layer in self.iface.mapCanvas().layers() if layer.name() == layer_name][0]
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,
@@ -144,10 +144,10 @@ class matrix:
144 144
             
145 145
             # Find field combo box
146 146
             all_combos = combo.parent().findChildren(QComboBox)
147
-            field_combo = [combo for combo in all_combos if combo.objectName().endswith('_id')][0] 
148
-            field_id = layer.fields().lookupField(field_combo.currentText())
147
+            field_combo = [box for box in all_combos if box.objectName().endswith('_id')][0] 
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}

Loading…
Cancel
Save