3

On QGIS, I am using a simple script to zoom the selected features. I write them in the Text Editor, but it didn't work until I used the console... and it does work. What kind of problem is this? Is there a token or anything I should introduce in the text editor?

municipios = QgsVectorLayer("\\","name","ogr")
QgsMapLayerRegistry.instance().addMapLayer(municipios)
l = iface.mapCanvas().layers()
expr = QgsExpression( "" )
it = l.getFeatures( QgsFeatureRequest( expr ))
itera = [i.id() for i in it]
l.setSelectedFeatures( itera )
iface.mapCanvas().zoomToSelected()

This is the error message (but not having it in my console, only with Text Editor of QGIS)

Traceback (most recent call last): File "", line 1, in File "c:/users/miguel~1/appdata/local/temp/tmpmlvs3w.py", line 9, in it = l.getFeatures( QgsFeatureRequest( expr )) AttributeError: 'list' object has no attribute 'getFeatures'

  • Hi, please paste a minimal code example that works in the console but not in the text editor. – Matthias Kuhn Mar 15 '16 at 12:22
  • Oh sorry, you are right – user64823 Mar 15 '16 at 12:26
  • Can you paste it as text? It will make it much easier for people to help you ;) – Matthias Kuhn Mar 15 '16 at 12:30
  • 1
    Just so I understand. You run this code in an external text editor, separate from QGIS? – Matthias Kuhn Mar 15 '16 at 12:39
  • Well, I thinks this is right, I managed to write the code correctly – user64823 Mar 15 '16 at 12:39
3

You could try using the following code in the Python Console Editor which I took from you, modified it a little and tested it on an example layer using the expression "Year" = 2005:

municipios = QgsVectorLayer("path/to/shapefile","name","ogr")
l = QgsMapLayerRegistry.instance().addMapLayer(municipios)
expr = QgsExpression( """ "Year" = 2005 """ )
itera = [i.id() for i in l.getFeatures(QgsFeatureRequest(expr))]
l.setSelectedFeatures( itera )
iface.mapCanvas().zoomToSelected()

The following image shows the extent of the selected features (marked as yellow):

Result

  • Thank you, it works but does nothing in my layer... It is a polygon layer, and I select a field, called MUNICIPIOS. Then, the expression is: " ' MUNICIPIOS ' = 'Alcobendas' " – user64823 Mar 15 '16 at 14:17
  • @user64823 - You have to be careful here with expressions as sometimes it needs to know when to escape the number of quotation marks. I began using triple double quotes on either end of an expression so in your case: """ "MUNICIPIOS" = 'Alcobendas' """. Or did you already use the triple quotes? – Joseph Mar 15 '16 at 14:19
  • well, I am using triple double quotes: """ "MUNICIPIOS" = 'Alcobendas' """ as you posted... It still doesn't work, I am reviewing all the code, it is like yours, and I watched some others, but they give me problems about the class properties. Then, I think the expression syntax is wrong. I have a field called MUNICIPIOS and a attribute called Alcobendas – user64823 Mar 15 '16 at 14:30
  • @user64823 - Interesting, which QGIS version are you using? Could you upload your shapefile to a public server so that it could be tested? – Joseph Mar 15 '16 at 14:46
  • 1
    I did so, (ArcGIS...I am getting used to QGIS) then, exported to Shapefile. Probably, this changed something in the field. I have already added a new field and deleted the previous one, "MUNICIPIOS". It works. (I was thinking about the other fields which have accents, for example, there is one field called "Álamo, El" but it is not a problem By the way, which public server is most recommended to upload data? This is for the next time.. Thank you very much, I hope this could have helped all of us for studies and for professional works. – user64823 Mar 15 '16 at 15:10

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.