Snippets

Piotr Szrajber Geospatial Portal SDK - randomly update query filters but preserve the styles and names

Created by Piotr Szrajber last modified
/**
 * Add buttons that randomly update query filters but preserve the styles and names of the queries. 
 *
 * This example depends on prior existaece of 3 analyses present in the workspace - q1, q2 and q3
 * that are performed on "{http://www.intergraph.com/geomedia/gml}OM_USA_STATES" feature class
 * from "http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get" WFS service.
 * IDs of these queries must be q1, q2 and q3.
 *
 * 2017-03-29 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 */
$GP.ready(function() {
    var mapServiceUrl = "http://demo.hexagongeospatial.com/GWM_WFS_NonTransactional/service.svc/get",
        featureClassId = "{http://www.intergraph.com/geomedia/gml}OM_USA_STATES",
        attributeId = "{http://www.intergraph.com/geomedia/gml}STATE_NAME",
        timeout = 60000,
        interval = 300,
        attemptsLeft = timeout / interval;

    function pickRandomSubset(array, size) {
        var subset = [],
            copy = array,
            index;
        while (subset.length !== size) {
            index = Math.floor(Math.random() * copy.length);
            subset.push(copy[index]);
            copy = copy.slice(0, index).concat(copy.slice(index + 1));
        }
        return subset;
    }

    function createRandomFilter(stateNames) {
        var filterSize = 3,
            randomSubset = pickRandomSubset(stateNames, filterSize);

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

    function addUpdateQueryButtons(result) {
        $GP.edit.getAttributeValues({
            mapServiceId: result.ids[0],
            featureClassId: featureClassId,
            attributeId: attributeId
        }, function(result2) {
            var stateNames = result2.values.map(function(x) {
                return x.value;
            });

            $GP.queries.find({
                name: /q\d/
            }, function(ret) {
                ret.analyses.forEach(function(analysis) {
                    $GP.ui.toolbar.add({
                        xtype: "tbbutton",
                        text: "update " + analysis.get_name(),
                        handler: function(b) {
                            $GP.queries.add({
                                featureClassId: featureClassId,
                                url: mapServiceUrl,
                                definitionName: "WFS",
                                queryId: analysis.get_id(),
                                filters: [createRandomFilter(stateNames)],
                            });
                        }
                    });
                });
            });
        });
    }

    function waitForMapService() {
        $GP.services.find({
            url: mapServiceUrl
        }, addUpdateQueryButtons, function() {
            if (attemptsLeft-- == 0) {
                $GP.ui.info("Couldn't find the map service. Is it initialized?");
            } else {
                setTimeout(waitForMapService, interval);
            }
        });
    }

    waitForMapService();
});

Comments (0)

HTTPS SSH

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