def run(self): QgsMessageLog.logMessage(f'Started task {self.description()}', 'TracingCAJ', Qgis.Info) epsg = self.__hidrometers.crs().postgisSrid() uri = "LineString?crs=epsg:" + str( epsg ) + "&field=id:integer" "&field=distance:double(20,2)&index=yes" dist = QgsVectorLayer(uri, 'dist', 'memory') QgsProject.instance().addMapLayer(dist) prov = dist.dataProvider() points_features = [ point_feature for point_feature in self.__hidrometers.getFeatures() ] feats = [] if len(points_features) > 0: for p in points_features: nearest_pipe = self.find_nearest_pipelines(p.geometry()) try: minDistPoint = nearest_pipe.closestSegmentWithContext( p.geometry().asPoint())[1] feat = QgsFeature() feat.setGeometry( QgsGeometry.fromPolylineXY( [p.geometry().asPoint(), minDistPoint])) feat.setAttributes( [points_features.index(p), feat.geometry().length()]) feats.append(feat) except Exception as e: print(p.id()) prov.addFeatures(feats)
def saveCurva(self): self.layer: QgsVectorLayer self.comboCurva: QtWidgets.QComboBox self.draw() if self.curvaFailed: return curvaFeats = featuresList(self.c.layer) features = [] if hasattr(self, "justStarted") and self.justStarted: i = 0 for f, tipo in zip(curvaFeats, self.c.dados): feat = QgsFeature(self.layer.fields()) feat.setGeometry(f.geometry()) feat.setAttributes([i, str(tipo[0]), "Traçado"]) features.append(feat) i += 1 self.justStarted = False elif len(curvaFeats) > 0: fid = 1 nomes = [] #Delete all features of self.layer and add layer geometry in between for i, feat in enumerate(self.layer.getFeatures()): if i > self.current_index and i < self.next_index: continue f = QgsFeature(self.layer.fields()) attr = feat.attributes() attr[0] = len(features) + 1 f.setAttributes(attr) if i == self.current_index: PI = p2QgsPoint(featureToPolyline(curvaFeats[0])[0]) f.setGeometry( QgsGeometry.fromPolyline( [p2QgsPoint(featureToPolyline(feat)[0]), PI])) features.append(f) for i, cf in enumerate(curvaFeats): f = QgsFeature(self.layer.fields()) attr = cf.attributes() attr = [len(features) + 1] + attr f.setAttributes(attr) f.setGeometry(cf.geometry()) features.append(f) if i == 0: nomes.append(str(attr[1])) nomes.append(str(attr[1])) elif i == self.next_index: PF = p2QgsPoint(featureToPolyline(curvaFeats[-1])[-1]) f.setGeometry( QgsGeometry.fromPolyline( [PF, p2QgsPoint(featureToPolyline(feat)[-1])])) features.append(f) else: f.setGeometry(feat.geometry()) features.append(f) self.layer.dataProvider().deleteFeatures( [f.id() for f in self.layer.getFeatures()]) self.layer.dataProvider().addFeatures(features) self.layer.updateExtents() try: QgsProject.instance().removeMapLayer(self.c.layer.id()) except: pass refreshCanvas(self.iface, self.layer)