Snippets

Piotr Szrajber Smart M.App - gsp.bi.stage.findSelectedFeatures workaround for choropleth rendered with V2 engine

Created by Piotr Szrajber last modified
/**
* gsp.bi.stage.findSelectedFeatures method does not work with V2 painter
* due to lack of "key" property in undecorated Feature objects.
*
* This is a replacement workaround method that is using gsp.bi.stage.findSelectedFeatureIds 
*
* 2018-03-07 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
*/

function findSelectedFeatures(config, callback, errback) {
    config = config || {};
    gsp.bi.stage.findWidgets({
        descriptors: [{
            chartM: {
                chart: "choropleth"
            }
        }],
        stageId: config.stageId
    }, function(widgets) {
        if (!widgets || !widgets[0]) {
            if (typeof errback === "function")
                errback("Cannot find any choropleth!");
        }
        debugger;
        var chart = widgets[0].chart,
            chartModel = widgets[0].chartM,
            keyProperty = chartModel.key,
            decorator = chart.featureData(),
            geoJson = chart.geoJson(),
            featureIds = chart.dimension().top(Infinity).map(function(d) {
                return d[keyProperty]
            }),
            decoratedFeatures = geoJson.features.map(decorator),
            filteredFeatures = decoratedFeatures.filter(function(feature) {
                return featureIds.indexOf(feature.key) !== -1;
            }),
            undecoratedFeatures = filteredFeatures.map(function(feature) {
                return {
                    type: "Feature",
                    geometry: feature.geometry,
                    properties: feature.properties
                    // it is possible to pass computed aggregations by accessing feature.value.*
                };
            });
        callback({
            type: "FeatureCollection",
            crs: geoJson.crs,
            features: undecoratedFeatures
        });
    });
}

// usage: findSelectedFeatures({stageId: "yourStageId"}, console.log)

Comments (1)

HTTPS SSH

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