[Qgis-user] How to make use of python variables in Qgis filter expression

Mathieu Bossaert (CEN L-R) sig at cenlr.org
Thu Oct 24 01:04:46 PDT 2013


Hello everyone, 

I found ! Looking around a solution to my problem, I understand that python would be the solution . 

Thanks to the QGis documentation and to those 3 resources : 

- http://nathanw.net/2012/11/10/user-defined-expression-functions-for-qgis/ 
- http://www.3liz.com/blog/rldhont/index.php?post/2012/11/06/Op%C3%A9rateurs-spatiaux-dans-les-expressions-de-QGIS 
- http://gis.stackexchange.com/questions/28625/how-to-create-a-polygon-based-on-view-extent-in-qgis-1-8 

So I created a python script ( userfunctions.py ), which contains a function called current_canvas_extent , which returns a Polygon representing the current influence of the window. 


[code] 
from qgis.utils import qgsfunction 
from qgis.utils import iface 
from qgis.core import QGis 

@qgsfunction(0, "Python") 
def current_canvas_extent(values, feature, parent): 
""" 
retourne l etendue courante de la carte 
""" 
##extend = QgsGeometry.fromRect( iface.mapCanvas().extent() ) 
##extend = iface.mapCanvas().extent().toString() 
extend = iface.mapCanvas().extent().asWktPolygon() 
return extend 
[/code] 

QGis call the script on start (add the option " - code path \ to \ userfunctions.py " ) 

So I now have access to this "variable" inside the expression editor. 
And I can ask QGis restrict the list to the objects intersecting the current canvas. 

[code] 
intersects ($ geometry , geomFromWKT ($ current_canvas_extent ) ) 
[/code] 

Thanks a lot for this great software you give us! 


Mathieu BOSSAERT 
-- 
Responsable du système d'information 

Membre de la Fédération des Conservatoires d'espaces naturels www.reseau-cen.org 

----- Mail original -----

De: "Mathieu Bossaert (CEN L-R)" <mathieu.bossaert at cenlr.org> 
À: qgis-user at lists.osgeo.org 
Envoyé: Mardi 1 Octobre 2013 11:26:55 
Objet: How to make use of python variables in Qgis filter expression 



Good morning, 

thanks to the new Qgis, I can make a really pratical interface to my postgis database. I want to make use of relationnal value in the editing form. 
The table I use to populate the list is a spatial table. 

I would like to restrict the list of available values in the form to the objects within the current extent of my map. 

I found an example wich is ok but I would like to make use of a variable instead of a fixed extend (GeomFromWKT()). 
The example I tried is here : http://www.3liz.com/blog/rldhont/index.php?post/2012/11/06/Spatial-operators-in-QGIS-expression 

Thanks a lot, 

Mathieu 


-- 
Mathieu BOSSAERT 
Responsable du système d'information 

Membre de la Fédération des Conservatoires d'espaces naturels www.reseau-cen.org 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20131024/de6694e7/attachment.html>


More information about the Qgis-user mailing list