Snippets

Piotr Szrajber Geospatial Portal SDK - update style and filter of grouped queries

Created by Piotr Szrajber
/**
 * Randomly update styles and filters for queries in the legend grouped under one node
 * 
 * Assumption: A1, A2 and A3 queries are already in the Legend->Layers
 * 
 * This script can be tested with the following workspace:
 * http://demo.hexagongeospatial.com/communityportal/Examples.aspx?gpw=bc775c72-52cc-44a4-889c-d32070ef908b
 * 
 * Paste this code in the javascript console. "A1", "A2" and "A3" buttons will appear in the Tools menu. Clicking
 * on each of these buttons causes changing filter and style of the associated layer. Filter and style are chosen
 * randomly.
 *  
 * 2017-03-22 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 */
var defaultLayerName = "Hexagon Geospatial OGC Web Feature Service",
    featureClassId = "{http://www.intergraph.com/geomedia/gml}OM_USA_STATES",
    serviceUrl = "http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get",
    scenes = [{
        title: "First Scene",
        filters: [{
            operator: "OR",
            operands: [{
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Washington"]
            }, {
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Ohio"]
            }, {
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "California"]
            }]
        }],
        style: {
            styles: [{
                type: "SimpleLineStyle",
                color: "#ff0000",
                size: 3
            }, {
                type: "SimpleFillStyle",
                color: "#00cc00",
                translucency: 0.2
            }]
        }
    }, {
        title: "Second Scene",
        filters: [{
            operator: "OR",
            operands: [{
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Texas"]
            }, {
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Utah"]
            }, {
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Arizona"]
            }]
        }],
        style: {
            styles: [{
                type: "SimpleLineStyle",
                color: "#00ff00",
                size: 5
            }, {
                type: "SimpleFillStyle",
                color: "#cc0000",
                translucency: 0.2
            }]
        }
    }, {
        title: "Third Scene",
        filters: [{
            operator: "OR",
            operands: [{
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Florida"]
            }, {
                operator: "=",
                operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", "Georgia"]
            }]
        }],
        style: {
            styles: [{
                type: "SimpleLineStyle",
                color: "#0000ff",
                size: 3
            }, {
                type: "SimpleFillStyle",
                color: "#0000cc",
                translucency: 0.2
            }]
        }
    }];

function executeQuery(queryName, filters, style, callback, errback) {
    $GP.queries.add({
        featureClassId: featureClassId,
        url: serviceUrl,
        definitionName: "WFS",
        queryName: queryName,
        addToLegend: false,
        filters: filters,
        style: new Intergraph.WebSolutions.Core.WebClient.Platform.Style.AreaStyle(null, style)
    }, callback, errback);
}

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

function executeNextQuery(queryName, parentLayerName) {
    var style = scenes[getRandomInt(0, scenes.length - 1)].style,
        filters = scenes[getRandomInt(0, scenes.length - 1)].filters;

    executeQuery(queryName, filters, style, function(result) {
        if (result.updated) {
            $GP.ui.info("Analysis updated");
            return;
        } else {
            $GP.ui.info("Analysis created!");
            
            $GP.queries.find({
                analysisId: result.analysisId,
            },  function (result2) {
                result2.analysis.addToLegend({
                    parentLayerName: parentLayerName
                });
            });
        }    
    }, function() {
        $GP.ui.info("Something went wrong");
    });
}

["A1", "A2", "A3"].forEach(function (item) {
    $GP.ui.toolbar.add({
        categoryIndex: 0,
        xtype: "tbbutton",
        text: item,
        handler: function(b) {
            executeNextQuery(item, defaultLayerName);
        }
    });
});

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.