Re: [Qgis-user] New Labeling: conditional labels
Hi On Thu, Dec 29, 2011 at 11:33 AM, Nathan Woodrow madman...@gmail.com wrote: It would be handy to add condition statements to QgsExpression so we can support this kind of thing, Martin would be the guy to talk to about adding this kind of thing. He knows the most about QgsExpression. SQL recognizes CASE statement with following alternate syntax: 1. if equivalent: CASE WHEN x 10 THEN 'big' ELSE 'small' END 2. switch equivalent: CASE x WHEN 5 THEN 'five' WHEN 6 THEN 'six' ELSE 'something else' END The else is not required, if there is no matching when then NULL is returned. Some SQL engines add further functions for convenience: - MySQL [1]: IF(), IFNULL(), NULLIF() - PostgreSQL [2]: COALESCE(), NULLIF() I would suggest to implement the CASE statement to stay compliant with SQL syntax (although I find It a bit verbose). Regards Martin [1] http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html [2] http://www.postgresql.org/docs/8.1/static/functions-conditional.html ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
On Fri, Jan 6, 2012 at 1:25 PM, Martin Dobias wonder...@gmail.com wrote: SQL recognizes CASE statement with following alternate syntax: 1. if equivalent: CASE WHEN x 10 THEN 'big' ELSE 'small' END Just pushed a commit implementing that. The syntax is: CASE WHEN cond1 THEN exp1 [WHEN cond2 THEN exp2]* [ELSE exp3] END There may be one or more WHEN/THEN clauses. If no condition matches, the expression from ELSE is evaluated - if none is specified then NULL is returned. Note: the other syntax with base expression (CASE x WHEN y THEN z ... END) is not supported yet. Have fun! Martin ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Love your work Martin! I'll add syntax highlighting for it tonight/tomorrow sometime. - Nathan On Sat, Jan 7, 2012 at 10:31 AM, Martin Dobias wonder...@gmail.com wrote: On Fri, Jan 6, 2012 at 1:25 PM, Martin Dobias wonder...@gmail.com wrote: SQL recognizes CASE statement with following alternate syntax: 1. if equivalent: CASE WHEN x 10 THEN 'big' ELSE 'small' END Just pushed a commit implementing that. The syntax is: CASE WHEN cond1 THEN exp1 [WHEN cond2 THEN exp2]* [ELSE exp3] END There may be one or more WHEN/THEN clauses. If no condition matches, the expression from ELSE is evaluated - if none is specified then NULL is returned. Note: the other syntax with base expression (CASE x WHEN y THEN z ... END) is not supported yet. Have fun! Martin ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Hi Giuseppe, Thanks for the further clarifications. With your additional hints I now fully understand how the substr function works and why we use *-1 here. Andreas On Thu, 29 Dec 2011 15:35:20 +0100, Giuseppe Sucameli wrote: On Thu, Dec 29, 2011 at 2:18 PM, Giuseppe Sucameli sucam...@faunalia.it wrote: On Thu, Dec 29, 2011 at 11:25 AM, Andreas Neumann a.neum...@carto.net wrote: What I do not understand is why I need the *-1 (multiplier). I usually use -1 in python to get the entire string ... The function help says: substr(string,startpos,length) so we need to use the string length, but also the startpos values starts from 1, not 0... (and the help doesn't help to understand it). looking at the code, here's what the susbstr function calls: string.mid( startpos -1, length ) Now looking at QString.mid() [1] I can confirm that using the susbstr function: startpos starts from position 1 (not 0) and if length is -1 then all chararcters available from startpos are returned. So you can use either substr(id, 1, (id 0)*-1) or substr(id, 1, (id 0)*length(id)), both are valid and working syntax. I think we should improve also the help to add those informations. Regards. [1] http://developer.qt.nokia.com/doc/qt-4.7/qstring.html#id-71632fd3-c671-4ae0-9791-bce0d200b511 -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
On Thu, Dec 29, 2011 at 2:18 PM, Giuseppe Sucameli sucam...@faunalia.it wrote: On Thu, Dec 29, 2011 at 11:25 AM, Andreas Neumann a.neum...@carto.net wrote: What I do not understand is why I need the *-1 (multiplier). I usually use -1 in python to get the entire string ... The function help says: substr(string,startpos,length) so we need to use the string length, but also the startpos values starts from 1, not 0... (and the help doesn't help to understand it). looking at the code, here's what the susbstr function calls: string.mid( startpos -1, length ) Now looking at QString.mid() [1] I can confirm that using the susbstr function: startpos starts from position 1 (not 0) and if length is -1 then all chararcters available from startpos are returned. So you can use either substr(id, 1, (id 0)*-1) or substr(id, 1, (id 0)*length(id)), both are valid and working syntax. I think we should improve also the help to add those informations. Regards. [1] http://developer.qt.nokia.com/doc/qt-4.7/qstring.html#id-71632fd3-c671-4ae0-9791-bce0d200b511 -- Giuseppe Sucameli ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Il 29/12/2011 11:25, Andreas Neumann ha scritto: If we could combine the rule-based renderer approach with the current labeler (with its new query builder), it would be awesome. Agreed, I think we must use the same approach everywhere (renderer, field calculator, raster calculator, actions), and the labelling approach is the best IMHO. Others are often a bit geeky. All the best. -- Paolo Cavallini - Faunalia www.faunalia.eu Full contact details at www.faunalia.eu/pc ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Hi Giuseppe, Thanks for your hint with the syntax - it works in my case. I think I sort of understand the syntax - at least the operator and the substr. What I do not understand is why I need the *-1 (multiplier). But I think the syntax is far from being intuitive, without some help. Maybe we can come up with a simple solution in the future? If we could combine the rule-based renderer approach with the current labeler (with its new query builder), it would be awesome. Thanks for your help! Andreas On Wed, 28 Dec 2011 20:01:43 +0100, Giuseppe Sucameli wrote: Hi Andreas, On Thu, Dec 22, 2011 at 2:31 PM, Andreas Neumann a.neum...@carto.net wrote: What I want is that a label is only drawn if a column value is larger than 0. I don't want to label features with a value of zero. Do you have any hints how the syntax works for such a case? I see operators but no if or when. there's no if operator, but returns 1 if true and 0 if false, e.g. (field 5)*10 let's give you 10 or 0 as result. The problem is that you don't want to display anything when the field value is 0, so we can use the substr function. Here's the solution to your problem: substr(id, 0, (id 0)*-1) that means if id = 0 then substr(id, 0, 0) else substr(id, 0, -1) In general: substr(output, 0, (condition)*-1) Cheers. If it is not possible, then I can still do it in Postgis, but it would be nice being able to do this in QGIS. Thanks for any hints, Andreas -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
It would be handy to add condition statements to QgsExpression so we can support this kind of thing, Martin would be the guy to talk to about adding this kind of thing. He knows the most about QgsExpression. I am also looking at adding Python support to any sections that also use QgsExpression; this would allow us to use a QGIS expression for simple-ish stuff but also Python if need be for doing more powerful stuff. I started a wiki page[0] before the last Hackfest about some of my ideas about adding rule based labeling, which would be very cool to have. [0] http://hub.qgis.org/wiki/quantum-gis/RuleBasedLabelingIdeas - Nathan On Thu, Dec 29, 2011 at 8:25 PM, Andreas Neumann a.neum...@carto.netwrote: Hi Giuseppe, Thanks for your hint with the syntax - it works in my case. I think I sort of understand the syntax - at least the operator and the substr. What I do not understand is why I need the *-1 (multiplier). But I think the syntax is far from being intuitive, without some help. Maybe we can come up with a simple solution in the future? If we could combine the rule-based renderer approach with the current labeler (with its new query builder), it would be awesome. Thanks for your help! Andreas On Wed, 28 Dec 2011 20:01:43 +0100, Giuseppe Sucameli wrote: Hi Andreas, On Thu, Dec 22, 2011 at 2:31 PM, Andreas Neumann a.neum...@carto.net wrote: What I want is that a label is only drawn if a column value is larger than 0. I don't want to label features with a value of zero. Do you have any hints how the syntax works for such a case? I see operators but no if or when. there's no if operator, but returns 1 if true and 0 if false, e.g. (field 5)*10 let's give you 10 or 0 as result. The problem is that you don't want to display anything when the field value is 0, so we can use the substr function. Here's the solution to your problem: substr(id, 0, (id 0)*-1) that means if id = 0 then substr(id, 0, 0) else substr(id, 0, -1) In general: substr(output, 0, (condition)*-1) Cheers. If it is not possible, then I can still do it in Postgis, but it would be nice being able to do this in QGIS. Thanks for any hints, Andreas -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland __**_ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/**mailman/listinfo/qgis-userhttp://lists.osgeo.org/mailman/listinfo/qgis-user -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland __**_ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/**mailman/listinfo/qgis-userhttp://lists.osgeo.org/mailman/listinfo/qgis-user ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Hi Andreas, On Thu, Dec 29, 2011 at 11:25 AM, Andreas Neumann a.neum...@carto.net wrote: I think I sort of understand the syntax - at least the operator and the substr. What I do not understand is why I need the *-1 (multiplier). when the condition is true I need the entire string, so if id 0 then substr(id, 0, length(id)) else substr(id, 0, 0). I usually use -1 in python to get the entire string, so I gave it a try, but even if it works well looking at function help I've noticed it's wrong. The function help says: substr(string,startpos,length) so we need to use the string length, but also the startpos values starts from 1, not 0... (and the help doesn't help to understand it). But I think the syntax is far from being intuitive, without some help. Maybe we can come up with a simple solution in the future? 1+, I agree Regards. If we could combine the rule-based renderer approach with the current labeler (with its new query builder), it would be awesome. Thanks for your help! Andreas On Wed, 28 Dec 2011 20:01:43 +0100, Giuseppe Sucameli wrote: Hi Andreas, On Thu, Dec 22, 2011 at 2:31 PM, Andreas Neumann a.neum...@carto.net wrote: What I want is that a label is only drawn if a column value is larger than 0. I don't want to label features with a value of zero. Do you have any hints how the syntax works for such a case? I see operators but no if or when. there's no if operator, but returns 1 if true and 0 if false, e.g. (field 5)*10 let's give you 10 or 0 as result. The problem is that you don't want to display anything when the field value is 0, so we can use the substr function. Here's the solution to your problem: substr(id, 0, (id 0)*-1) that means if id = 0 then substr(id, 0, 0) else substr(id, 0, -1) In general: substr(output, 0, (condition)*-1) Cheers. If it is not possible, then I can still do it in Postgis, but it would be nice being able to do this in QGIS. Thanks for any hints, Andreas -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user -- Giuseppe Sucameli ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] New Labeling: conditional labels
Hi Andreas, On Thu, Dec 22, 2011 at 2:31 PM, Andreas Neumann a.neum...@carto.net wrote: What I want is that a label is only drawn if a column value is larger than 0. I don't want to label features with a value of zero. Do you have any hints how the syntax works for such a case? I see operators but no if or when. there's no if operator, but returns 1 if true and 0 if false, e.g. (field 5)*10 let's give you 10 or 0 as result. The problem is that you don't want to display anything when the field value is 0, so we can use the substr function. Here's the solution to your problem: substr(id, 0, (id 0)*-1) that means if id = 0 then substr(id, 0, 0) else substr(id, 0, -1) In general: substr(output, 0, (condition)*-1) Cheers. If it is not possible, then I can still do it in Postgis, but it would be nice being able to do this in QGIS. Thanks for any hints, Andreas -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user -- Giuseppe Sucameli ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
[Qgis-user] New Labeling: conditional labels
Hi, Using Nathan's new query builder for labeling - how can I achieve a conditional label? What I want is that a label is only drawn if a column value is larger than 0. I don't want to label features with a value of zero. Do you have any hints how the syntax works for such a case? I see operators but no if or when. If it is not possible, then I can still do it in Postgis, but it would be nice being able to do this in QGIS. Thanks for any hints, Andreas -- -- Andreas Neumann Böschacherstrasse 10A 8624 Grüt (Gossau ZH) Switzerland ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user