Snippets

Piotr Szrajber Geospatial Portal SDK - reproduce style lost during analysis update (fixed)

Created by Piotr Szrajber
/**
 * Updatable Styled Query
 * 
 * This example adds a toolbar button which creates an updatable styled query or updates it
 * if it is already created. Each update modifies query's filter and its style.
 * Query legend item can be saved in the workspace (explicitly in the user interface or from
 * code using $GP.user.workspaces)
 * 
 * The code below must be pasted manually in the javascript console  
 *
 * 2018-04-14 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 */
var featureClassId = "{http://www.intergraph.com/geomedia/gml}OM_USA_STATES",
    serviceUrl = "http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get",
    style1 = {
        styles: [{
            type: "SimpleLineStyle",
            color: "#ff0000",
            size: 3
        }, {
            type: "SimpleFillStyle",
            color: "#00cc00",
            translucency: 0.2
        }]
    },
    style2 = {
        styles: [{
            type: "SimpleLineStyle",
            color: "#00ff00",
            size: 5
        }, {
            type: "SimpleFillStyle",
            color: "#cc0000",
            translucency: 0.2
        }]
    },
    style3 = {
        styles: [{
            type: "SimpleLineStyle",
            color: "#0000ff",
            size: 3
        }, {
            type: "SimpleFillStyle",
            color: "#0000cc",
            translucency: 0.2
        }]
    },
    progressions = [
        createQueryProgression("First", [
            ["Washington", "Oregon"],
            ["Oregon", "California"],
            ["California", "Nevada"],
            ["Nevada", "Idaho"],
            ["Idaho", "Washington"],
        ], style1),
        createQueryProgression("Second", [
            ["Arizona", "Utah"],
            ["Utah", "Wyoming"],
            ["Wyoming", "Colorado"],
            ["Colorado", "Arizona"]
        ], style2),
        createQueryProgression("Second", [
            ["Mississippi", "Alabama"],
            ["Alabama", "Georgia"],
            ["Georgia", "Florida"],
            ["Florida", "Georgia"],
            ["Alabama", "Georgia"],
            ["Mississippi", "Alabama"]
        ], style3)
    ];


function createQuery(title, stateNames, style) {
    return {
        title: title,
        filters: [{
            operator: "OR",
            operands: stateNames.map(function(stateName) {
                return {
                    operator: "=",
                    operands: ["{http://www.intergraph.com/geomedia/gml}STATE_NAME", stateName]
                };
            })
        }],
        style: style
    };
}

function createQueryProgression(titlePrefix, filters, style) {
    // only the first one has an explicitly defined style
    var ret = [createQuery(titlePrefix + "#0", filters[0], style)];
    for (var i = 1; i < filters.length; i++) // in the non-working version style is not added and it is supposed to be taken automatically
        ret.push(createQuery(titlePrefix + "#" + i, filters[i], style));
    return ret;

}

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

function createProgressionHandler(queryName, progression) {
    var progressionIndex = 0,
        progressionLength = progression.length;
    return function() {
        executeQuery(queryName, progression[progressionIndex++ % progressionLength], function(result) {
            if (result.updated) {
                $GP.ui.info("Analysis updated #" + (progressionIndex + 1));
                return;
            } else {
                $GP.ui.info("Analysis created!");
            }
            $GP.queries.find({
                analysisId: result.analysisId,
            }, function(result2) {
                result2.analysis.addToLegend(function() {
                    $GP.legend.find({
                        name: queryName
                    }, function(ret) {
                        ret.legendItems[0].fitLayer();
                    });
                });
            });
        }, function() {
            $GP.ui.info("Something went wrong");
        });
    };
}

var p1 = createProgressionHandler("progression1", progressions[0]),
    p2 = createProgressionHandler("progression2", progressions[1]),
    p3 = createProgressionHandler("progression3", progressions[2]);;


function action() {
    p1();
    p2();
    p3();
}

$GP.ui.toolbar.add({
    categoryIndex: 0,
    xtype: "tbbutton",
    text: "Update query",
    handler: action
});

Comments (0)

HTTPS SSH

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