How to re-use an expression, stored as a string, in QGIS Field Calculator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












3















As discussed in this question, QGIS automatically saves recently used expressions in the field calculator. But recently used expressions are deleted after a while. Sometimes I need to re-use an expression that I used a while ago, and it's no longer available.



So I needed a way to store expressions for re-use in the Field Calculator at a much later time.



The method I figured out is to store the expression as in a text field. When I need to re-use the expression, I copy it from the attribute table into the Field Calculator. Here's what that looks like with a simple example:



enter image description here



Of course it's trivial to remember and re-enter the expression in this example. I would use this method with much more complicated expressions, such as "Population"/$area*3.58701e-8/"Population", which calculates population density (people per square mile) after first converting square feet to square miles.




Now I'm looking for a better way than manually copying the expression. It seems like there should be a way in the Field Calculator to take the field name as input, interpret it as an expression, and output the result of that expression. Here's what I've tried so far:



Just using the field name outputs the formula as a string.



enter image description here



Is there a function that will force the Field Calculator to interpret a string as an expression? By comparison to the expressions to_real() and as_string(), I'm thinking it would be something like as_expression() or as_formula(). So the output of as_expression('$area') would be the calculated area of the polygon feature.










share|improve this question


























    3















    As discussed in this question, QGIS automatically saves recently used expressions in the field calculator. But recently used expressions are deleted after a while. Sometimes I need to re-use an expression that I used a while ago, and it's no longer available.



    So I needed a way to store expressions for re-use in the Field Calculator at a much later time.



    The method I figured out is to store the expression as in a text field. When I need to re-use the expression, I copy it from the attribute table into the Field Calculator. Here's what that looks like with a simple example:



    enter image description here



    Of course it's trivial to remember and re-enter the expression in this example. I would use this method with much more complicated expressions, such as "Population"/$area*3.58701e-8/"Population", which calculates population density (people per square mile) after first converting square feet to square miles.




    Now I'm looking for a better way than manually copying the expression. It seems like there should be a way in the Field Calculator to take the field name as input, interpret it as an expression, and output the result of that expression. Here's what I've tried so far:



    Just using the field name outputs the formula as a string.



    enter image description here



    Is there a function that will force the Field Calculator to interpret a string as an expression? By comparison to the expressions to_real() and as_string(), I'm thinking it would be something like as_expression() or as_formula(). So the output of as_expression('$area') would be the calculated area of the polygon feature.










    share|improve this question
























      3












      3








      3








      As discussed in this question, QGIS automatically saves recently used expressions in the field calculator. But recently used expressions are deleted after a while. Sometimes I need to re-use an expression that I used a while ago, and it's no longer available.



      So I needed a way to store expressions for re-use in the Field Calculator at a much later time.



      The method I figured out is to store the expression as in a text field. When I need to re-use the expression, I copy it from the attribute table into the Field Calculator. Here's what that looks like with a simple example:



      enter image description here



      Of course it's trivial to remember and re-enter the expression in this example. I would use this method with much more complicated expressions, such as "Population"/$area*3.58701e-8/"Population", which calculates population density (people per square mile) after first converting square feet to square miles.




      Now I'm looking for a better way than manually copying the expression. It seems like there should be a way in the Field Calculator to take the field name as input, interpret it as an expression, and output the result of that expression. Here's what I've tried so far:



      Just using the field name outputs the formula as a string.



      enter image description here



      Is there a function that will force the Field Calculator to interpret a string as an expression? By comparison to the expressions to_real() and as_string(), I'm thinking it would be something like as_expression() or as_formula(). So the output of as_expression('$area') would be the calculated area of the polygon feature.










      share|improve this question














      As discussed in this question, QGIS automatically saves recently used expressions in the field calculator. But recently used expressions are deleted after a while. Sometimes I need to re-use an expression that I used a while ago, and it's no longer available.



      So I needed a way to store expressions for re-use in the Field Calculator at a much later time.



      The method I figured out is to store the expression as in a text field. When I need to re-use the expression, I copy it from the attribute table into the Field Calculator. Here's what that looks like with a simple example:



      enter image description here



      Of course it's trivial to remember and re-enter the expression in this example. I would use this method with much more complicated expressions, such as "Population"/$area*3.58701e-8/"Population", which calculates population density (people per square mile) after first converting square feet to square miles.




      Now I'm looking for a better way than manually copying the expression. It seems like there should be a way in the Field Calculator to take the field name as input, interpret it as an expression, and output the result of that expression. Here's what I've tried so far:



      Just using the field name outputs the formula as a string.



      enter image description here



      Is there a function that will force the Field Calculator to interpret a string as an expression? By comparison to the expressions to_real() and as_string(), I'm thinking it would be something like as_expression() or as_formula(). So the output of as_expression('$area') would be the calculated area of the polygon feature.







      qgis field-calculator function






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 6 at 18:36









      cskcsk

      8,6921035




      8,6921035




















          1 Answer
          1






          active

          oldest

          votes


















          6














          I found the answer to my own question almost as soon as I posted it. The function I was looking for is called eval.




          function eval



          Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.



          Syntax eval(expression)



          Arguments



          expression an expression string



          Examples




          • eval(''nice'') → 'nice'


          • eval(@expression_var) → [whatever the result of evaluating @expression_var might be...]



          So for my example where an expression is stored as a string in a field called "Expression", use this in the Field Calculator:



          eval("Expression")





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311279%2fhow-to-re-use-an-expression-stored-as-a-string-in-qgis-field-calculator%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            I found the answer to my own question almost as soon as I posted it. The function I was looking for is called eval.




            function eval



            Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.



            Syntax eval(expression)



            Arguments



            expression an expression string



            Examples




            • eval(''nice'') → 'nice'


            • eval(@expression_var) → [whatever the result of evaluating @expression_var might be...]



            So for my example where an expression is stored as a string in a field called "Expression", use this in the Field Calculator:



            eval("Expression")





            share|improve this answer



























              6














              I found the answer to my own question almost as soon as I posted it. The function I was looking for is called eval.




              function eval



              Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.



              Syntax eval(expression)



              Arguments



              expression an expression string



              Examples




              • eval(''nice'') → 'nice'


              • eval(@expression_var) → [whatever the result of evaluating @expression_var might be...]



              So for my example where an expression is stored as a string in a field called "Expression", use this in the Field Calculator:



              eval("Expression")





              share|improve this answer

























                6












                6








                6







                I found the answer to my own question almost as soon as I posted it. The function I was looking for is called eval.




                function eval



                Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.



                Syntax eval(expression)



                Arguments



                expression an expression string



                Examples




                • eval(''nice'') → 'nice'


                • eval(@expression_var) → [whatever the result of evaluating @expression_var might be...]



                So for my example where an expression is stored as a string in a field called "Expression", use this in the Field Calculator:



                eval("Expression")





                share|improve this answer













                I found the answer to my own question almost as soon as I posted it. The function I was looking for is called eval.




                function eval



                Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.



                Syntax eval(expression)



                Arguments



                expression an expression string



                Examples




                • eval(''nice'') → 'nice'


                • eval(@expression_var) → [whatever the result of evaluating @expression_var might be...]



                So for my example where an expression is stored as a string in a field called "Expression", use this in the Field Calculator:



                eval("Expression")






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 6 at 18:41









                cskcsk

                8,6921035




                8,6921035



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311279%2fhow-to-re-use-an-expression-stored-as-a-string-in-qgis-field-calculator%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown






                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)