1

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.

2

Keep things simple, you don't need a special function selectedfeatures here, you can do everything at once (try your script in the Python console before).

1) you don't need the Valtestand state_and_cities variables:

layer = iface.activeLayer()
idx = layer.fieldNameIndex('name')
idx1 = feature.fieldNameIndex('state')
for feature in layer.selectedFeatures():
    mbuf = feature.geometry().buffer(dist, 2)
    for f in layer.getFeatures():
        geom2 = f.geometry()
        if QgsGeometry.within(geom2, mbuf):
             lsNearCities.append(str(f.attributes()[idx]) + "," + str(f.attributes()[idx1])) 

2) If you want to further simplify (only one for loop), use the itertools standard Python module.

import itertools
for f, feature in itertools.product(layer.getFeatures(), layer.selectedFeatures()):
   geom2 = f.geometry()
   mbuf = feature.geometry().buffer(dist, 2)
   if QgsGeometry.within(geom2, mbuf):
       lsNearCities.append(str(f.attributes()[idx]) + "," + str(f.attributes()[idx1]))
 self.writeToFile(lsNearCities, filePath)

3) And you can do that without lsNearCities:

with open(filepath, 'w') as output:
    for f, feature in itertools.product(layer.getFeatures(), layer.selectedFeatures()):
       geom2 = f.geometry()
       mbuf = feature.geometry().buffer(dist, 2)
           if QgsGeometry.within(geom2, mbuf):
               output.write(str(f.attributes()[idx]) + "," + str(f.attributes()[idx1])+ '\n')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.