Code Monkey home page Code Monkey logo

qgs2js's Introduction

exp2js

Converts a QGIS Expression string into a JS function.

Work in progress...

Main ussage at the moment is for qgis2web (https://github.com/tomchadwin/qgis2web) by Tom

Usage

import qgs2js
functionjs, name = qgs2js.compile("1 + 1")

# Put the functionjs and call to name in your JS string.
outputjs = functionjs
outputjs += name + "(context)"

Example

Work in progress examples

A super simple example.

var feature = {
            COLA: 1,
            COLB: 2,
            WAT: 'Hello World'
        };

var context = {
            feature: feature,
            variables: {}
        };

function wqjr_eval_expression(context) {
    // (1 + 1) * 3 + 5

    var feature = context.feature;
    
    return (((1 + 1) * 3) + 5);
}

var result = wqjr_eval_expression(context);
console.log(result);
function ueun_eval_expression(context) {
    // NOT var('myvar') = format('some string %1 %2', 'Hello', 'World')

    var feature = context.feature;
    
    return ! (fnc_var(['myvar'], context) == fnc_format(['some string %1 %2','Hello','World'], context)) ;
}

var result = ueun_eval_expression(context);
console.log(result);

A more complicated example with columns, functions, case, and OR.

function rgpj_eval_expression(context) {
    // CASE WHEN to_int(123.52) = var('myvar') THEN to_real(123) WHEN 1 + 2 = 3 THEN 2 ELSE to_int(1) END OR 2 * 2 + 5 = 4

    var feature = context.feature;
    function _CASE() {
    if (fnc_to_int([123.52], context) == fnc_var(['myvar'], context)) {
          return fnc_to_real([123], context);
        }
        else if ((1 + 2) == 3) {
          return 2;
        }
    else {
     return fnc_to_int([1], context);
    }
    };
    return (_CASE() || (((2 * 2) + 5) == 4));
}

var result = rgpj_eval_expression(context);
console.log(result);

Comparing columns in a case statement.

function ppwa_eval_expression(context) {
    // CASE WHEN COLA = 1 THEN 1 WHEN 1 + 2 = 3 THEN 2 ELSE 3 END

    var feature = context.feature;
    function _CASE() {
    if (feature['COLA']  == 1) {
          return 1;
        }
        else if ((1 + 2) == 3) {
          return 2;
        }
    else {
     return 3;
    }
    };
    return _CASE();
}

var result = ppwa_eval_expression(context);
console.log(result);

How does this work?

QGIS will convert the expression into a AST (Abstract Syntax Tree) which makes it easy to walk down each node. We handle each node and translate it to the JS version of it.

qgs2js's People

Contributors

nathanw2 avatar tomchadwin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.