Browse Source

OSMtools_IncludeGraphHopper
Nils 2 years ago
parent
commit
5ccdcf2770
4 changed files with 109 additions and 101 deletions
  1. BIN
      icon.png
  2. 1
    1
      ors_tools.py
  3. 35
    31
      ors_tools_access.py
  4. 73
    69
      ors_tools_routing.py

BIN
icon.png View File


+ 1
- 1
ors_tools.py View File

@@ -185,7 +185,7 @@ class ORStools:
185 185
         icon_path = ':/plugins/ORStools/icon.png'
186 186
         self.add_action(
187 187
             icon_path,
188
-            text=self.tr(u'Gimme'),
188
+            text=self.tr(u'ORS Tools'),
189 189
             callback=self.run,
190 190
             parent=self.iface.mainWindow())
191 191
         

+ 35
- 31
ors_tools_access.py View File

@@ -140,40 +140,44 @@ class accessAnalysis:
140 140
         isochrone_list = []
141 141
         feat_list = []
142 142
 
143
-        for isochrone in access_path.findall("aas:Isochrone", self.ns):
144
-            feat_out = QgsFeature()
145
-            
146
-            coord_ext_list = []
147
-            coord_int_list = []
148
-            
149
-            # First find the exterior ring
150
-            for coords_ext in isochrone.findall("aas:IsochroneGeometry/"
143
+        try:
144
+            for isochrone in access_path.findall("aas:Isochrone", self.ns):
145
+                feat_out = QgsFeature()
146
+                
147
+                coord_ext_list = []
148
+                coord_int_list = []
149
+                
150
+                # First find the exterior ring
151
+                for coords_ext in isochrone.findall("aas:IsochroneGeometry/"
152
+                                                "gml:Polygon/"
153
+                                                "gml:exterior/"
154
+                                                "gml:LinearRing/"
155
+                                                "gml:pos",
156
+                                                self.ns):
157
+                    coords_ext_tuple = tuple([float(coord) for coord in coords_ext.text.split(" ")])
158
+                    qgis_coords_ext = QgsPoint(coords_ext_tuple[0], coords_ext_tuple[1])
159
+                    coord_ext_list.append(qgis_coords_ext)
160
+                
161
+                # Then find all interior rings
162
+                for ring_int in isochrone.findall("aas:IsochroneGeometry/"
151 163
                                             "gml:Polygon/"
152
-                                            "gml:exterior/"
153
-                                            "gml:LinearRing/"
164
+                                            "gml:interior",
165
+                                            self.ns):
166
+                    int_poly = []
167
+                    for coords_int in ring_int.findall("gml:LinearRing/"
154 168
                                             "gml:pos",
155 169
                                             self.ns):
156
-                coords_ext_tuple = tuple([float(coord) for coord in coords_ext.text.split(" ")])
157
-                qgis_coords_ext = QgsPoint(coords_ext_tuple[0], coords_ext_tuple[1])
158
-                coord_ext_list.append(qgis_coords_ext)
159
-            
160
-            # Then find all interior rings
161
-            for ring_int in isochrone.findall("aas:IsochroneGeometry/"
162
-                                        "gml:Polygon/"
163
-                                        "gml:interior",
164
-                                        self.ns):
165
-                int_poly = []
166
-                for coords_int in ring_int.findall("gml:LinearRing/"
167
-                                        "gml:pos",
168
-                                        self.ns):
169
-                    coords_int_tuple = tuple([float(coord) for coord in coords_int.text.split(" ")])
170
-                    qgis_coords_int = QgsPoint(coords_int_tuple[0], coords_int_tuple[1])
171
-                    int_poly.append(qgis_coords_int)
172
-                coord_int_list.append(int_poly)
173
-                    
174
-            feat_out.setGeometry(QgsGeometry.fromPolygon([coord_ext_list] + coord_int_list))
175
-            feat_list.append(feat_out)
176
-            isochrone_list.append(float(isochrone.get("time"))/60)
170
+                        coords_int_tuple = tuple([float(coord) for coord in coords_int.text.split(" ")])
171
+                        qgis_coords_int = QgsPoint(coords_int_tuple[0], coords_int_tuple[1])
172
+                        int_poly.append(qgis_coords_int)
173
+                    coord_int_list.append(int_poly)
174
+                        
175
+                feat_out.setGeometry(QgsGeometry.fromPolygon([coord_ext_list] + coord_int_list))
176
+                feat_list.append(feat_out)
177
+                isochrone_list.append(float(isochrone.get("time"))/60)
178
+        except AttributeError:
179
+            msg = "Request is not valid! Check parameters. TIP: Coordinates must plot within 1 km of a road."
180
+            qgis.utils.iface.messageBar().pushMessage(msg, level = qgis.gui.QgsMessageBar.CRITICAL)
177 181
         
178 182
         return feat_list, isochrone_list
179 183
 

+ 73
- 69
ors_tools_routing.py View File

@@ -273,77 +273,81 @@ class routing:
273 273
             if route[0] == route[1]:
274 274
                 continue
275 275
             else:
276
-                # Create URL
277
-                req = "{}api_key={}&start={}&end={}&routepref={}&weighting={}&maxspeed={}&instructions=False".format(self.url, 
278
-                                                    self.api_key, 
279
-                                                    route[0],
280
-                                                    route[1],
281
-                                                    self.mode_travel,
282
-                                                    self.mode_routing,
283
-                                                    self.speed_max
284
-                                                    )
285
-                print req
286
-                if route_via != "":
287
-                    req += "&via={}".format(route_via)
276
+                try:
277
+                    # Create URL
278
+                    req = "{}api_key={}&start={}&end={}&routepref={}&weighting={}&maxspeed={}&instructions=False".format(self.url, 
279
+                                                        self.api_key, 
280
+                                                        route[0],
281
+                                                        route[1],
282
+                                                        self.mode_travel,
283
+                                                        self.mode_routing,
284
+                                                        self.speed_max
285
+                                                        )
286
+                    print req
287
+                    if route_via != "":
288
+                        req += "&via={}".format(route_via)
289
+                        
290
+                    # Get response from API and read into element tree
291
+                    response = requests.get(req)
292
+                    root = ET.fromstring(response.content)
293
+                    access_path = root.find("xls:Response/"
294
+                                            "xls:DetermineRouteResponse",
295
+                                            self.ns)
288 296
                     
289
-                # Get response from API and read into element tree
290
-                response = requests.get(req)
291
-                root = ET.fromstring(response.content)
292
-                access_path = root.find("xls:Response/"
293
-                                        "xls:DetermineRouteResponse",
294
-                                        self.ns)
295
-                
296
-                feat_out = QgsFeature()
297
-                
298
-                # Read all coordinates
299
-                coords_list = []
300
-                for coords in access_path.findall("xls:RouteGeometry/gml:LineString/gml:pos", self.ns):
301
-                    coords_tuple = tuple([float(coord) for coord in coords.text.split(" ")])
302
-                    qgis_coords = QgsPoint(coords_tuple[0], coords_tuple[1])
303
-                    coords_list.append(qgis_coords)
304
-                
305
-                # Read total time
306
-                time_path = access_path.find("xls:RouteSummary/xls:TotalTime", self.ns)
307
-                time_text = time_path.text
308
-                if 'D' not in time_text:
309
-                    time_text = re.sub(r'(P)', r'P0D', time_text)
310
-                if 'H' not in time_text:
311
-                    time_text = re.sub(r'(T)', r'T0H', time_text)
312
-                if 'M' not in time_text:
313
-                    time_text = re.sub(r'(H)', r'H0M', time_text)
314
-                
315
-                time_list = list(reversed(re.split('DT|H|M', time_text[1:-1])))
316
-                while len(time_list) < 4:
317
-                    time_list.append('0')
318
-                secs, mins, hours, days = [int(x) for x in time_list]
319
-                hours += (days*24)
320
-                #hours = "{0:.3f}".format(hours)
321
-                                     
322
-                # Read total distance
323
-                distance = float(access_path.find("xls:RouteSummary/xls:TotalDistance", self.ns).get("value"))
324
-                
325
-                # Read X and Y
326
-                route_start_x, route_start_y = [float(coord) for coord in route[0].split(",")]
327
-                route_end_x, route_end_y = [float(coord) for coord in route[1].split(",")]
297
+                    feat_out = QgsFeature()
328 298
                     
329
-                # Set feature geometry and attributes
330
-                feat_out.setGeometry(QgsGeometry.fromPolyline(coords_list))
331
-                feat_out.setAttributes([distance,
332
-                                        hours,
333
-                                        mins,
334
-                                        secs,
335
-                                        self.mode_travel,
336
-                                        self.mode_routing,
337
-                                        route_start_y,
338
-                                        route_start_x,
339
-                                        route_end_y,
340
-                                        route_end_x,
341
-                                        route_ids[i][0],
342
-                                        route_ids[i][1]]) 
343
-                
344
-                layer_out_prov.addFeatures([feat_out])
345
-                
346
-                progress.setValue(i)
299
+                    # Read all coordinates
300
+                    coords_list = []
301
+                    for coords in access_path.findall("xls:RouteGeometry/gml:LineString/gml:pos", self.ns):
302
+                        coords_tuple = tuple([float(coord) for coord in coords.text.split(" ")])
303
+                        qgis_coords = QgsPoint(coords_tuple[0], coords_tuple[1])
304
+                        coords_list.append(qgis_coords)
305
+                    
306
+                    # Read total time
307
+                    time_path = access_path.find("xls:RouteSummary/xls:TotalTime", self.ns)
308
+                    time_text = time_path.text
309
+                    if 'D' not in time_text:
310
+                        time_text = re.sub(r'(P)', r'P0D', time_text)
311
+                    if 'H' not in time_text:
312
+                        time_text = re.sub(r'(T)', r'T0H', time_text)
313
+                    if 'M' not in time_text:
314
+                        time_text = re.sub(r'(H)', r'H0M', time_text)
315
+                    
316
+                    time_list = list(reversed(re.split('DT|H|M', time_text[1:-1])))
317
+                    while len(time_list) < 4:
318
+                        time_list.append('0')
319
+                    secs, mins, hours, days = [int(x) for x in time_list]
320
+                    hours += (days*24)
321
+                    #hours = "{0:.3f}".format(hours)
322
+                                         
323
+                    # Read total distance
324
+                    distance = float(access_path.find("xls:RouteSummary/xls:TotalDistance", self.ns).get("value"))
325
+                    
326
+                    # Read X and Y
327
+                    route_start_x, route_start_y = [float(coord) for coord in route[0].split(",")]
328
+                    route_end_x, route_end_y = [float(coord) for coord in route[1].split(",")]
329
+                        
330
+                    # Set feature geometry and attributes
331
+                    feat_out.setGeometry(QgsGeometry.fromPolyline(coords_list))
332
+                    feat_out.setAttributes([distance,
333
+                                            hours,
334
+                                            mins,
335
+                                            secs,
336
+                                            self.mode_travel,
337
+                                            self.mode_routing,
338
+                                            route_start_y,
339
+                                            route_start_x,
340
+                                            route_end_y,
341
+                                            route_end_x,
342
+                                            route_ids[i][0],
343
+                                            route_ids[i][1]]) 
344
+                    
345
+                    layer_out_prov.addFeatures([feat_out])
346
+                    
347
+                    progress.setValue(i)    
348
+                except AttributeError:
349
+                    msg = "Request is not valid! Check parameters. TIP: Coordinates must plot within 1 km of a road."
350
+                    qgis.utils.iface.messageBar().pushMessage(msg, level = qgis.gui.QgsMessageBar.CRITICAL)
347 351
         layer_out.updateExtents()
348 352
 
349 353
         QgsMapLayerRegistry.instance().addMapLayer(layer_out)

Loading…
Cancel
Save