I am writing a simple plugin that finds buffer. My gui has 2 combo box and 1 Line edit. The first combo box will have a drop down of values from column 'States' and 2nd combo will have a drop down of values from column 'Cities'. Note that the Cities will be filtered in the combo box depending on the states selected. And 3rd Lineedit gets a distance from the user. As a result, I want buffer for the cities (from the 2nd combo box) with the user specified 'distance value' given in the lineedit box.I tried QgsGeometryAnalyzer().buffer() function in QGIS API. But the first argument is a layer for the above mentioned function. Instead i want my gui to take value from the city combo and lineedit and find buffer. How to accomplish. Sorry for the lengthy post.

1 Answer 1


I think this can help you:

# Get buffer distance and selected city 
dist = float( myLineEdit.text() )
city = myComboBo.currentText()

# Get the feature corresponding to the selected city
expr = QgsExpression('"Cities" = \'' + city + '\'' )
it = myLayer.getFeatures( QgsFeatureRequest( expr ) )
feature = it.next()

# Perform a buffer on the feature geometry
myBufferPolygon = feature.geometry().buffer(dist, 50)

Then you could use the myBufferPolygon to create a new feature and store it in another vector layer or something like that.

  • Can you explain the what QgsExpression(' "Cities" = \ '' + city + '\ '') does exactly?
    – User123
    Feb 14, 2015 at 4:14
  • 1
    It's equivalent to something like "Cities" = 'myCity' but escapes the quotes properly. Since you need to get a feature in order to get its geometry and apply a buffer, you can get it by using a filter expression. "Cities" = 'myCity' is an expression that lets you get exactly the feature whose Cities value is the selected by the user. In summary, QgsExpression('"Cities" = \'' + city + '\'' ) allows you to define a filter in order to get a feature by attribute. Feb 14, 2015 at 4:25
  • Great Explanation gcarrillo. Will check and let you know. Thanks.
    – User123
    Feb 14, 2015 at 4:28
  • 1
    Thankyou gcarrillo, the code works!
    – User123
    Feb 14, 2015 at 12:37
  • I tried the above code. It is now showing the error. feature = it.next() stopIteration
    – User123
    Feb 20, 2015 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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