def test_compute_paths(self): self.test_prepare() try: self.r.reset() except Exception as err: self.fail("Path result resetting failed - {}".format(err.__str__())) path_computation(origin, dest, self.g, self.r) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") self.r.compute_path(origin, dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results")
def test_compute_paths(self): path_computation(origin, dest, self.g, self.r) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") self.r.compute_path(origin, dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") if list(self.r.path_link_directions) != [-1, -1, -1]: self.fail("Path computation failed. Wrong link directions") self.r.compute_path(dest, origin) if list(self.r.path_link_directions) != [1, 1, 1]: self.fail("Path computation failed. Wrong link directions")
def test_compute_paths(self): path_computation(5, 2, self.g, self.r) self.assertEqual(list(self.r.path), [12, 14], "Path computation failed. Wrong sequence of links") self.assertEqual(list(self.r.path_link_directions), [1, 1], "Path computation failed. Wrong link directions") self.assertEqual(list(self.r.path_nodes), [5, 6, 2], "Path computation failed. Wrong sequence of path nodes") self.assertEqual(list(self.r.milepost), [0, 4, 9], "Path computation failed. Wrong milepost results")
def produces_path(self): self.to_but.setEnabled(True) if self.path_from.text().isdigit() and self.path_to.text().isdigit(): self.res.reset() path_computation(int(self.path_from.text()), int(self.path_to.text()), self.graph, self.res) if self.res.path is not None: ## If you want to do selections instead of new layers, this is how to do it # f = self.cb_link_id_field.currentText() # t = '' # for k in self.res.path[:-1]: # t = t + f + "=" + str(k) + ' or ' # t = t + f + "=" + str(self.res.path[-1]) # expr = QgsExpression(t) # it = self.line_layer.getFeatures(QgsFeatureRequest(expr)) # # ids = [i.id() for i in it] # self.line_layer.setSelectedFeatures(ids) # If you want to create new layers # This way is MUCH faster crs = self.line_layer.dataProvider().crs().authid() vl = QgsVectorLayer( "LineString?crs={}".format(crs), self.path_from.text() + " to " + self.path_to.text(), "memory") pr = vl.dataProvider() # add fields pr.addAttributes(self.line_layer.dataProvider().fields()) vl.updateFields( ) # tell the vector layer to fetch changes from the provider # add a feature all_links = [] for k in self.res.path: fet = self.link_features[k] all_links.append(fet) # add all links to the temp layer pr.addFeatures(all_links) # add layer to the map QgsProject.instance().addMapLayer(vl) symbol = vl.renderer().symbol() symbol.setWidth(1) qgis.utils.iface.mapCanvas().refresh() else: qgis.utils.iface.messageBar().pushMessage( "No path between " + self.path_from.text() + ' and ' + self.path_to.text(), '', level=3)
def test_update_trace(self): self.test_prepare() try: self.r.reset() except: self.fail('Path result resetting failed') path_computation(origin, dest, self.g, self.r) if list(self.r.path) != [53, 52, 13]: self.fail('Path computation failed. Wrong sequence of links') if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail('Path computation failed. Wrong sequence of path nodes') if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail('Path computation failed. Wrong milepost results')
def produces_path(self): self.to_but.setEnabled(True) if self.path_from.text().isdigit() and self.path_to.text().isdigit(): self.res.reset() path_computation(int(self.path_from.text()), int(self.path_to.text()), self.graph, self.res) if self.res.path is not None: # If you want to do selections instead of new layers if self.rdo_selection.isChecked(): self.create_path_with_selection() # If you want to create new layers else: self.create_path_with_scratch_layer() else: qgis.utils.iface.messageBar().pushMessage( "No path between " + self.path_from.text() + " and " + self.path_to.text(), "", level=3)
def finished(self, procedure): ns = self.worker_thread.node_sequence if len(ns) < 2: return all_links = [] for i in range(1, len(ns)): self.res.reset() path_computation(ns[i - 1], ns[i], self.graph, self.res) all_links.extend(list(self.res.path)) if self.rdo_new_layer.isChecked(): self.create_path_with_scratch_layer(all_links) else: self.create_path_with_selection(all_links) self.close() if self.worker_thread.report is not None: dlg2 = ReportDialog(self.iface, self.worker_thread.report) dlg2.show() dlg2.exec_()
def produces_path(self): self.to_but.setEnabled(True) if self.path_from.text().isdigit() and self.path_to.text().isdigit(): self.res.reset() path_computation(int(self.path_from.text()), int(self.path_to.text()), self.graph, self.res) if self.res.path is not None: ## If you want to do selections instead of new layers, this is how to do it # f = self.cb_link_id_field.currentText() # t = '' # for k in self.res.path[:-1]: # t = t + f + "=" + str(k) + ' or ' # t = t + f + "=" + str(self.res.path[-1]) # expr = QgsExpression(t) # it = self.line_layer.getFeatures(QgsFeatureRequest(expr)) # # ids = [i.id() for i in it] # self.line_layer.setSelectedFeatures(ids) # If you want to create new layers # This way is MUCH faster crs = self.line_layer.dataProvider().crs().authid() vl = QgsVectorLayer( "LineString?crs={}".format(crs), self.path_from.text() + " to " + self.path_to.text(), "memory") pr = vl.dataProvider() # add fields pr.addAttributes(self.line_layer.dataProvider().fields()) vl.updateFields( ) # tell the vector layer to fetch changes from the provider # add a feature all_links = [] for k in self.res.path: fet = self.link_features[k] all_links.append(fet) # add all links to the temp layer pr.addFeatures(all_links) # add layer to the map QgsMapLayerRegistry.instance().addMapLayer(vl) # format the layer with a thick line registry = QgsSymbolLayerV2Registry.instance() lineMeta = registry.symbolLayerMetadata("SimpleLine") symbol = QgsLineSymbolV2() lineLayer = lineMeta.createSymbolLayer({ 'width': '1', 'color': self.random_rgb(), 'offset': '0', 'penstyle': 'solid', 'use_custom_dash': '0', 'joinstyle': 'bevel', 'capstyle': 'square' }) symbol.deleteSymbolLayer(0) symbol.appendSymbolLayer(lineLayer) renderer = QgsSingleSymbolRendererV2(symbol) vl.setRendererV2(renderer) qgis.utils.iface.mapCanvas().refresh() else: qgis.utils.iface.messageBar().pushMessage( "No path between " + self.path_from.text() + ' and ' + self.path_to.text(), '', level=3)