I'm trying to make query from field calculator so that it would be possible to connect to PostGIS layer (or local file on computer) and select data according to condition so that use this selection afterwards for layer in project.

Is it possible?

I've found great functions in Reffunctions plugin - e.g. dbvalue, that retrieve first targetField value from targetLayer when keyField is equal to conditionValue;

dbquery, that retrieve first targetField value from targetLayer when whereClause is true (for file in project) - this function seems to not work

But all these functions require a layer in a project, altough I wonder to connect to PostGIS layer or local file in a query. I suppose I will need to create custom function in function builder, but I don't know how turn to local or PostGIS layer from QGIS via python.

This post and cookbook gives information about adding PostGIS layer in QGIS project, but still unsuccessfully - What's wrong? It gives me an error

"NameError: name 'self' is not defined"

here is my code

other variant

other variant

also don't add a PostGIS layer

I tried single quotes instead of double, but it doesn't help. Local file adds successfully after replacing single delimiter to double in path to file.

Any ideas?)

  • 1
    I would start doing this in python console in QGIS, once you get this than you may incorporate this logic into a custom function, see gis.stackexchange.com/questions/175245/…
    – artwork21
    Commented Jun 30, 2017 at 13:38
  • @artwork21, Thanks for the link! I have found in pyqgis developer cookbook information about it, but still unsuccessfully( I specified all connection parameters according to post\cookbook, but nevertheless I can't load PostGIS layer
    – Jane
    Commented Jul 1, 2017 at 9:37
  • if you are running directly in QGIS python console you do not need to use "self" in the run function argument.
    – artwork21
    Commented Jul 3, 2017 at 19:10


Your Answer

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

Browse other questions tagged or ask your own question.