IdentifyTask Returning Scale-Dependent Layers When Outside of Scale Range

4721
12
10-11-2012 06:54 AM
AndrewBrown1
Occasional Contributor II
Hello,

I'm working on my second web mapping application, and I am implementing an identifyTask on a couple point layers that are scale-dependent. At first, the layers are disabled until the user zooms closer to the points. Eventually, the points are re-enabled because the current scale meets the scale criteria. If I turn on one of the n layers and attempt to identify an area around the point, my identifyTask will identify the layer that's turned on and the layers that are turned off. I believe this is a bug of some sort. My code is below:

identifyParams = new esri.tasks.IdentifyParameters();
            identifyParams.tolerance = 3;
            identifyParams.returnGeometry = true;
            identifyParams.layerIds = [1,2,3,4,5,6,7,9];
            identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;
            identifyParams.geometry = geometry;
            identifyParams.mapExtent = map.extent;
            identifyParams.width = map.width;
            identifyParams.height = map.height;
            identifyTask.execute(identifyParams, function (identifyResults) {


If I were to output identifyResults to the console, it would contain the visible and non-visible layers. As you can see, I specified LAYER_OPTION_VISIBLE, so it should only be identifying layers that are turned on, not the ones that are enabled due to scale dependency! I'm using 3.2compact as my API.

Thanks,
Andrew
0 Kudos
12 Replies
JohnGravois
Frequent Contributor
Andrew,

just to clarify, the issue i documented earlier in this thread is not the same issue that Adrian is referring to, and it is not a JS API bug, but rather a problem with the REST response itself.  i'd be happy to log a new NIM for this issue, but i'm not quite sure i understand the use case for formatting a request to explicitly include a layer that you don't want to be returned.
0 Kudos
DouglasHall
New Contributor III
***Just updated to add one more check and better variable naming***

Been dealing with this myself -- and it's different between jsapi 2.x and 3.x. Only what's showing on map will be identified on. Note the checking of subLayerIds for null, scale checking, after getting the visibleLayers from service.

Solution is for jaspi 3.x

function doIdentify(evt) {
    var referenceLayer = new esri.layers.ArcGISDynamicMapServiceLayer(referenceMapServiceName, {id: 'referenceLayer'});
    var aVisibleSubLayers = referenceLayer.visibleLayers,
        aLayersInScale = [],
        identifyParams = new esri.tasks.IdentifyParameters(),
        identifyTask = new esri.tasks.IdentifyTask(referenceLayer.url);

    //loop thru visible sublayers that are not group layers, then check within scale
    if (referenceLayer.visible === true && aVisibleSubLayers.length !== 0 && aVisibleSubLayers[0] !== -1) {
        dojo.forEach(referenceLayer.createDynamicLayerInfosFromLayerInfos(), function (dLayInfo) {
            if (dLayInfo.subLayerIds === null && dojo.indexOf(aVisibleSubLayers, dLayInfo.id) !== -1 &&
                    (dLayInfo.minScale === undefined || dLayInfo.minScale === 0 || map.getScale() <= dLayInfo.minScale) &&
                    (dLayInfo.maxScale === undefined || dLayInfo.maxScale === 0 || map.getScale() >= dLayInfo.maxScale)) {
                aLayersInScale.push(dLayInfo.id);
            }
        });
    }

    if (aLayersInScale.length > 0) {
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;
        identifyParams.tolerance = 3;
        identifyParams.returnGeometry = true;
        identifyParams.layerIds = aLayersInScale;
        identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
        identifyParams.width = map.width;
        identifyParams.height = map.height;
        identifyTask.execute(identifyParams, function(idResults) { 
            doSomethingWithIdentifyResults(idResults);
        });
    }
}


Bonus: Identifies on multiple services at once and return as one array using dojo.DeferredList

dojo.require("dojo.DeferredList");

referenceLayer = new esri.layers.ArcGISDynamicMapServiceLayer(referenceMapServiceName, {id: 'referenceLayer'});

//Operational layers
operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer(operationalMapServiceName, {id: 'operationalLayer'});

function executeIdentify(geom, serviceName, aLayerIds) {
    var identifyParams = new esri.tasks.IdentifyParameters(),
        identifyTask = new esri.tasks.IdentifyTask(serviceName);
    identifyParams.geometry = geom;
    identifyParams.mapExtent = map.extent;
    identifyParams.tolerance = 3;
    identifyParams.returnGeometry = true;
    identifyParams.layerIds = aLayerIds;
    identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    identifyParams.width = map.width;
    identifyParams.height = map.height;
    return identifyTask.execute(identifyParams);
}

function doIdentify(evt) {
    var geom = evt.mapPoint,
        aLayersInScale,
        aDeferreds = [],
        deferredList,
        currentScale = map.getScale(),
        aVisibleSubLayers,
        aAGSDynMapServicesToIdentify = [referenceLayer, operationalLayer];

    map.graphics.clear();
    identifyLayerKeyPrevious = undefined;

    //loop thru dynamic services, then thru visible sublayers that are not group layers, then check within scale
    dojo.forEach(aAGSDynMapServicesToIdentify, function (agsDynMapService) {
        aVisibleSubLayers = agsDynMapService.visibleLayers;
        if (agsDynMapService.visible === true && aVisibleSubLayers.length !== 0 && aVisibleSubLayers[0] !== -1) {
            aLayersInScale = [];
            dojo.forEach(agsDynMapService.createDynamicLayerInfosFromLayerInfos(), function (dLayInfo) {
                if (dLayInfo.subLayerIds === null && dojo.indexOf(aVisibleSubLayers, dLayInfo.id) !== -1 &&
                        (dLayInfo.minScale === undefined || dLayInfo.minScale === 0 || currentScale <= dLayInfo.minScale) &&
                        (dLayInfo.maxScale === undefined || dLayInfo.maxScale === 0 || currentScale >= dLayInfo.maxScale)) {
                    aLayersInScale.push(dLayInfo.id);
                }
            });
            if (aLayersInScale.length > 0) {
                aDeferreds.push(executeIdentify(evt.mapPoint, agsDynMapService.url, aLayersInScale));
            }
        }
    });

    // create a deferred list to aggregate the state for multiple asynchronous identify queries
    deferredList = new dojo.DeferredList(aDeferreds);
    deferredList.then(function (aIdentifyResults) {
        // "aIdentifyResults" is 2D array of results
        // array[0] boolean true or false, success or failure of individual call
        // array[1] is the array of identity results returned
        doSomethingWithIdentifyResults(aIdentifyResults);
    });
}
0 Kudos
KevinMacLeod1
Occasional Contributor III
Thank drhall001 and others. I will test your solution. However, is this still a workaround for an existing bug?

I don't understand why "LAYER_OPTION_VISIBLE " shouldn't work. Even with scale dependencies. However on my site it's not working, all layers show in popups. Here is the scenario: We have a simple site, and it calls one rest endpoint with around 100 layers in several groups and subgroups, bringing them in as one dynamic layer. I'm using the AGS JS TOC widget, to present it.  (Love that thing! )

I'm using the Identify task for popups. There are several scale categories for scale dependency I set in the MXD so certain layers would appear at one scale, more at another, and finally all, at the lowest zoom scale dependency.

LAYER_OPTION_VISIBLE  does nothing. Identify just presents popups for everything. At all scales.  I also tried using identifyParams.layerIds = LAYERNAME.visibleLayers   ... still no luck.

Based on the API docs for LAYER_OPTION_VISIBLE ... shouldn't this just work? So is this a known API 'bug'?  Or is this related to AGS JS TOC?

Thank you to everyone here! I will test the solution above soon this week or next and report back results.
0 Kudos