I have a function say selectedfeatures this function would be called when a selection changed event happens in mapcanvas.
self.canvas.selectionChanged.connect(self.selectedfeatures) QObject.connect(self.dlg.runbutton, SIGNAL("clicked()"), self.mapselection) def selectedfeatures(self): layer = iface.activeLayer() idx = layer.fieldNameIndex('name') self.feature = layer.selectedFeatures() QMessageBox.information(self.dlg, "features were selected:", str(len(self.feature)) + " features were selected " +"" )
And for that selected features in the above function i want to perform buffer using the below function.I am using the self.feature in mapselection function like below
def mapselection (self): for feature in self.feature: mbuf = feature.geometry().buffer(dist, 2) iterFeat = layer.getFeatures() for f in iterFeat: geom2 = f.geometry() valTest = QgsGeometry.within(geom2, mbuf) if valTest: idx = feature.fieldNameIndex('name') #name of the attribute column with the cities in it idx1 = feature.fieldNameIndex('state') nearCity = f.attributes()[idx] state = f.attributes()[idx1] state_and_cities = str(state) + "," + str(nearCity) lsNearCities.append(state_and_cities) #call the function to write them into a file self.writeToFile(lsNearCities, filePath)
The output is not as expected though there are features selected in mapcanvas, it is saying 0 cities were there in the list lsNearCities.I want to know should i want to include any event separately for selection process completed in mapcanvas? If so how should that be done? Please tell me where i am wrong.