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

Nathan Woodrow madmanwoo at gmail.com
Thu Oct 24 01:31:49 PDT 2013


Hey Mathieu,

Great use of a custom user function.  If you add that into startup.py in
.qgis2/python/ it will always be there when you load QGIS. I also have a
project called Expression+ (
http://plugins.qgis.org/plugins/qgsexpressionsplus/) which adds some extra
functions, I could also add it in there as people might find it handy.

- Nathan


On Thu, Oct 24, 2013 at 6:04 PM, Mathieu Bossaert (CEN L-R)
<sig at cenlr.org>wrote:

> 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
>
> ------------------------------
> *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
>
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20131024/e7a80258/attachment.html>


More information about the Qgis-user mailing list