1

I have a function which will allow the user to select a feature or more features in map canvas and performs buffer for the selected feature(s) and writes the output to a csv file. I dont have any problem if a single feature is selected in map canvas. But when many features are selected, the name of the cities in the output csv gets repeated which is shown below and my code is also attached.

def mapselection(self):
    for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        if layer.name() == "us_cities":
            lsNearCities = []
            filePath = self.dlg.fileNameEdit.text()
            if len(str(filePath)) ==0:
                QMessageBox.warning(self.dlg, "File Name","Give it a file name and try again")        
                return
            if self.dlg.DistancelineEdit.text():
                dist = float(self.dlg.DistancelineEdit.text())*(1609.344)
                if dist != 0:
                    selfeature = layer.selectedFeatures()
                    for feature in selfeature:
                        mbuf = feature.geometry().buffer(dist, 2)
                        iterFeat = layer.getFeatures()
                        for f in iterFeat:
                            geom2 = f.geometry()
                            valTest = QgsGeometry.within(geom2, mbuf)
                            if valTest:
                                idx = layer.fieldNameIndex('name')  #name of the attribute column with the cities in it
                                idx1 = layer.fieldNameIndex('state')
                                nearCity = f.attributes()[idx]
                                state = f.attributes()[idx1]
                                state_and_cities = str(nearCity) + "," + str(state)
                                lsNearCities.append(state_and_cities)
                elif dist == 0:
                    selfeature = layer.selectedFeatures()
                    for feature in selfeature:
                        idx = layer.fieldNameIndex('name')  #name of the attribute column with the cities in it
                        idx1 = layer.fieldNameIndex('state')
                        nearCity = feature.attributes()[idx]
                        state = feature.attributes()[idx1]
                        state_and_cities = str(nearCity) + "," + str(state)
                        lsNearCities.append(state_and_cities)



            else:
                selfeature = layer.selectedFeatures()
                for feature in selfeature:
                    idx = layer.fieldNameIndex('name')  #name of the attribute column with the cities in it
                    idx1 = layer.fieldNameIndex('state')
                    nearCity = feature.attributes()[idx]
                    state = feature.attributes()[idx1]
                    state_and_cities = str(nearCity) + "," + str(state)
                    lsNearCities.append(state_and_cities)


    #call the function to write them into a file
    self.writeToFile(lsNearCities, filePath)

enter image description here

  • Quick Tip: QgsMapLayerRegistry.instance().mapLayersByName('us_cities')[0] will give you the layer you need without the loop. – Nathan W Apr 6 '15 at 13:09
  • Another Tip: Avoid heavy nesting as it can make you code hard to read and debug. Sometimes a refactor can help find bugs. – Nathan W Apr 6 '15 at 13:09
  • 2
    Here is a quick refactor. I know you didn't ask for that but I find it normally helps to find issues gist.github.com/NathanW2/4de348da41113dbeba03 – Nathan W Apr 6 '15 at 13:24
  • Thank you Nathan for your efforts. i had just removed the duplicate values from my list and write in csv. That works. Thanks again. – User123 Apr 6 '15 at 17:29

Your Answer

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

Browse other questions tagged or ask your own question.