JavaScript Support in MapServer

I’ve just pushed a first implementation of the javascript support in MapServer master. We can now style our features programmatically rather than writing multiple classes. We have choosen the library V8 as javascript engine. It is an experimental feature but it looks very promising. Here’s an example:

mapfile:

1
2
3
4
5
6
7
8
9
LAYER
    NAME "my_vector_layer"
    TYPE POINT
    STATUS ON
    ...
    STYLEITEM "javascript:///path/to/my/file.js"
    CLASS
    END
END

javascript file that returns a random style:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require('./global_variables.js'); // for symbols array

// random symbol
var symbol = symbols[Math.floor(Math.random()*symbols.length)];
var style = style = "STYLE SIZE 12 SYMBOL '"+symbol+"'";

if (shape.attributes.COULEUR != '#000000') {
    style += "COLOR '"+shape.attributes.COULEUR+"' END";
} else {
    // random color
    red = Math.random()*255;
    green = Math.random()*255;
    blue = Math.random()*255;
    style += "COLOR "+red+" "+green+" "+blue+" END";
}

// return the style to MapServer
style;

result:

For more information, please refer to the RFC: Support of Styleitem JavaScript Plugin

Comments