Identify task returns duplicates

2752
3
Jump to solution
03-29-2016 04:21 PM
AlexGole
Occasional Contributor II

Hi all, I encountering a problem I have never had to deal with before. I am trying to use the identify widget to return attributes from multiple layers  when clicked on. The issue is that it sometimes returns duplicates. Not sure why.

Here is my code:

1) The Identify params are called in the map.on load

map.on("load", function () {
                                 //coordinates on load


                                 map.on("mouse-move", showCoordinates);
                                 map.on("mouse-drag", showCoordinates);
                                 map.on("extent-change", showScale);




                                 //Show coordinates in bottom left corner of map
                                 function showCoordinates(evt) {
                                     //the map is in web mercator but display coordinates in geographic (lat, long)
                                     var mp = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
                                     //display mouse coordinates
                                     dom.byId("info").innerHTML = 'X, Y: ' + mp.x.toFixed(3) + ", " + mp.y.toFixed(3);
                                 }
                                 function showScale() {
                                     var currentScale = map.getScale();
                                     dom.byId("scaleVis").innerHTML = 'Scale 1:' + currentScale.toFixed(0);
                                 }
                                 var currentScale = map.getScale();
                                 dom.byId("scaleVis").innerHTML = 'Scale 1:' + currentScale.toFixed(0);
                                 var visible = [];
                             




                                 dynaLayer1.on('visible-layers-change', function (e, index) {
                                     visible.length = 0;
                                     console.log('The layer(s) changed: ', e.visibleLayers.join(', '));
                                     visible.push(e.visibleLayers)
                                 });
                                 //create identify tasks and setup parameters
                                 identifyTask = new IdentifyTask(dynaLayer1.url);


                                 identifyParams = new IdentifyParameters();
                                 identifyParams.tolerance = 3;
                                 identifyParams.returnGeometry = true;
                                 identifyParams.layerIds = visible;
                                 identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE;
                                 identifyParams.width = map.width;
                                 identifyParams.height = map.height;




                                 //Create Home button
                                 initialExtent = map.extent;
                                 dojo.create("div", {
                                     className: "esriSimpleSliderHomeButton",
                                     title: 'Zoom to Full Extent',
                                     onclick: function () {
                                         if (initialExtent === undefined) {
                                             initialExtent = map.extent;
                                         }
                                         map.setExtent(initialExtent);
                                     }
                                 }, dojo.query(".esriSimpleSliderIncrementButton")[0], "after");




                                 var GraphicsTHP = new GraphicsLayer();
                                 GraphicsTHP.id = 'graphicsTHP';


                                 // Add the graphic and Layers here


                                 map.addLayers([dynaLayer2, dynaLayer1, GraphicsTHP]);
                                 dynaLayer2.hide();


                             });//End of on load stuff here

2) My function that executes the identify

  function executeIdentifyTask(event) {
                             $("#dialog").dialog("close");
                             identifyParams.geometry = event.mapPoint;
                             identifyParams.mapExtent = map.extent;
                             var deferred = identifyTask
                               .execute(identifyParams)
                               .addCallback(function (response) {
                                   // response is an array of identify result objects
                                   // Let's return an array of features.
                                   return arrayUtils.map(response, function (result, index) {
                                       var feature = result.feature;
                                       var layerName = result.layerName;


                                       feature.attributes.layerName = layerName;
                                       if (layerName === 'Timber Harvest Plans') {
                                           var THPs1 = new InfoTemplate("Timber Harvest Plans",
                                             "<b>County: </b>${County}<br/>" +
                                               "<b>HarvestDoc_Num: </b>${HarvestDoc_Num}<br/>" +
                                               "<b>Silviculture: </b>${Silviculture}<br/>" +
                                               "<b>Silviculture2: </b>${Silviculture2}<br/>" +
                                               "<b>HarvestDoc_Num: </b>${HarvestDoc_Num}<br/>" +
                                               "<b>Yarding: </b>${Yarding}<br/>" +
                                               "<b>Status_of_Plan: </b>${Status_of_Plan}<br/>" +
                                               "<b>Timber_Owner: </b>${Timber_Owner}<br/>" +
                                               "<b>Landowner: </b>${Landowner}<br/>" +
                                               "<b>Acres: </b>${Acres}<br/>"
                                               );
                                           feature.setInfoTemplate(THPs1);
                                       }
                                       else if (layerName === 'Public Land Survey System - Township, Range, & Section') {


                                           var test = new InfoTemplate("PLSS",
                                             "<b>SECTION: </b>${SECTION}<br/>" +
                                               "<b>MERIDIAN: </b>${CDFPWSNAME}<br/>" +
                                               "<b>TOWNSHIP: </b>${TOWNSHIP}<br/>" +
                                               "<b>RANGE: </b>${RANGE}<br/>" +
                                               "<b>MERIDIAN: </b>${MERIDIAN}<br/>"
                                                );
                                           feature.setInfoTemplate(test);
                                       }
                                       else if (layerName === 'Emergency Notices (King Fire, 2014)') {


                                           var THPs2 = new InfoTemplate("Emergency Notices",
                                             "<b>Acres: </b>${Acres}<br/>" +
                                               "<b>OWNER: </b>${OWNER}<br/>" +
                                               "<b>STATUS: </b>${STATUS}<br/>" +
                                               "<b>EMERGENCYNUM: </b>${EMERGENCYNUM}<br/>" +
                                               "<b>Treatment: </b>${Treatment}<br/>"
                                               );
                                           feature.setInfoTemplate(THPs2);
                                       }


                                       else if (layerName === 'Coastal Anadromy Zone (NOAA NMFS)') {


                                           var NOAA = new InfoTemplate("Coastal Anadromy Zone",
                                             "<b>Area Type: </b>${Area Type}<br/>");
                                           feature.setInfoTemplate(NOAA);
                                       }
                                       else if (layerName === 'Calwater - Planning Watersheds') {


                                           var Calwater = new InfoTemplate("Planning Watersheds",
                                             "<b>CALWNUM: </b>${CALWNUM}<br/>" +
                                               "<b>CDFPWSNAME: </b>${CDFPWSNAME}<br/>" +
                                               "<b>Coho_ESU_Status: </b>${Coho_ESU_Status}<br/>" +
                                               "<b>Chinook_ESU_Status: </b>${Chinook_ESU_Status}<br/>" +
                                               "<b>Steelhead_DPS_Status: </b>${Steelhead_DPS_Status}<br/>" +
                                               "<b>Coho_Restorability: </b>${Coho_Restorability}<br/>" +
                                               "<b>Steelhead_Restorability: </b>${Steelhead_Restorability}<br/>" +
                                               "<b>UpStream_ASP: </b>${UpStream_ASP}<br/>" +
                                               "<b>Coastal_Anadromy_Zone_FPRs: </b>${Coastal_Anadromy_Zone_FPRs}<br/>" +
                                               "<b>Status_303d_2010: </b>${Status_303d_2010}<br/>" +
                                               "<b>County: </b>${County}<br/>" +
                                               "<b>Acres: </b>${Acres}<br/>");
                                           feature.setInfoTemplate(Calwater);
                                       }
                                       
                                       else if (layerName === 'Special Treatment Areas') {


                                           var Calwater3 = new InfoTemplate("Special Treatment Areas",
                                             "<b>Area_Type: </b>${Area_Type}<br/>" +
                                               "<b>Area_Name: </b>${Area_Name}<br/>" +
                                               "<b>County: </b>${County}<br/>" +
                                               "<b>Quad_Name: </b>${Quad_Name}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater3);
                                       }
                                       else if (layerName === 'Coastal Zone Boundary') {


                                           var Calwater4 = new InfoTemplate("Coastal Zone Boundary",
                                            "<b>AREA_TYPE: </b>${AREA_TYPE}<br/>" +
                                               "<b>CODE_SOURCE: </b>${CODE_SOURCE}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater4);
                                       }
                                       else if (layerName === '303(d) Channels, 2010') {


                                           var Calwater8 = new InfoTemplate("303(d) Channel 2010",
                                            "<b>TDCKEY: </b>${TDCKEY}<br/>" +
                                               "<b>RF3RCHID: </b>${RF3RCHID}<br/>" +
                                               "<b>PNAME: </b>${PNAME}<br/>" +
                                               "<b>PNMCD: </b>${PNMCD}<br/>" +
                                               "<b>RFORGFLAG: </b>${RFORGFLAG}<br/>" +
                                               "<b>UPDATE2: </b>${UPDATE2}<br/>" +
                                               "<b>UPDTCD2: </b>${UPDTCD2}<br/>" +
                                               "<b>UPDTSRC2: </b>${UPDTSRC2}<br/>" +
                                               "<b>FILLER: </b>${FILLER}<br/>" +
                                               "<b>WBID: </b>${WBID}<br/>" +
                                               "<b>WBNAME: </b>${WBNAME}<br/>" +
                                               "<b>REGION: </b>${REGION}<br/>" +
                                               "<b>REGION_NM: </b>${REGION_NM}<br/>" +


                                               "<b>WBTYPE: </b>${WBTYPE}<br/>" +
                                               "<b>WBTYPE_NM: </b>${WBTYPE_NM}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater8);
                                       }


                                       else if (layerName === '303(d) Waterbodies, 2010') {


                                           var Calwater9 = new InfoTemplate("303(d) Waterbody 2010",
                                            "<b>WATER: </b>${WATER}<br/>" +


                                               "<b>NAME: </b>${NAME}<br/>" +
                                               "<b>WRCBLAKES: </b>${WRCBLAKES}<br/>" +
                                               "<b>GNIS_ID: </b>${GNIS_ID}<br/>" +
                                               "<b>IFDKEY: </b>${IFDKEY}<br/>" +


                                               "<b>WBID: </b>${WBID}<br/>" +
                                               "<b>WBNAME: </b>${WBNAME}<br/>" +
                                               "<b>REGION: </b>${REGION}<br/>" +
                                               "<b>REGION_NM: </b>${REGION_NM}<br/>" +
                                               "<b>WBID: </b>${WBID}<br/>" +
                                               "<b>WBTYPE: </b>${WBTYPE}<br/>" +
                                               "<b>WBTYPE_NM: </b>${WBTYPE_NM}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater9);
                                       }
                                       else if (layerName === 'Rainfall 1900 – 1960 (USGS)') {


                                           var Calwater5 = new InfoTemplate("Rainfall 1900 – 1960",


                                               "<b>Mean Annual Rainfall (inches): </b>${Mean Annual Rainfall (inches)}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater5);
                                       }
                                       else if (layerName === 'Coho Status') {


                                           var Calwater6 = new InfoTemplate("Coho Status",


                                               "<b>ESU/DPS: </b>${ESU/DPS}<br/>" +
                                               "<b>Status: </b>${Status}<br/>" +
                                               "<b>Class: </b>${Class}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater6);
                                       }
                                       else if (layerName === 'Chinook Status') {


                                           var Calwater10 = new InfoTemplate("Chinook Status",


                                               "<b>ESU/DPS: </b>${ESU/DPS}<br/>" +
                                               "<b>Status: </b>${Status}<br/>" +
                                               "<b>Class: </b>${Class}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater10);
                                       }
                                       else if (layerName === 'Steelhead Status') {


                                           var Calwater16 = new InfoTemplate("Steelhead Status",


                                               "<b>ESU/DPS: </b>${ESU/DPS}<br/>" +
                                               "<b>Status: </b>${Status}<br/>" +
                                               "<b>Class: </b>${Class}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater16);
                                       }
                                       else if (layerName === 'Forest Districts') {


                                           var Calwater20 = new InfoTemplate("Forest Districts",


                                               "<b>Name: </b>${Name}<br/>" 
                                                );
                                           feature.setInfoTemplate(Calwater20);
                                       }
                                       else if (layerName === 'CA County Boundaries') {


                                           var Calwater8 = new InfoTemplate("County Boundary",


                                               "<b>County Name: </b>${County Name}<br/>"
                                                );
                                           feature.setInfoTemplate(Calwater8);
                                       }
                                       return feature;
                                   });
                               });


                             // InfoWindow expects an array of features from each deferred
                             // object that you pass. If the response from the task execution
                             // above is not an array of features, then you need to add a callback
                             // like the one above to post-process the response and return an
                             // array of features.
                             map.infoWindow.setFeatures([deferred]);
                             map.infoWindow.show(event.mapPoint);
                         }

Any idea is welcome.

Thanks,

Alex

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Alex,

  Can you check your visible array that you use as the layerIds parameter for the IdentifyParams to see if it contains any duplicate layer IDs?

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Alex,

  Can you check your visible array that you use as the layerIds parameter for the IdentifyParams to see if it contains any duplicate layer IDs?

0 Kudos
AlexGole
Occasional Contributor II

I am using this function to check if layers are visible and push layers that are visible or not:

  1. dynaLayer1.on('visible-layers-change', function (e, index) { 
  2.                                      visible.length = 0
  3.                                      console.log('The layer(s) changed: ', e.visibleLayers.join(', ')); 
  4.                                      visible.push(e.visibleLayers) 
  5.                                  }); 

When loading the map it indicates that these layers IDs are visible:

The layer(s) changed:  0, 4, 5, , 6, 7, 8, 9, 19

However, since I am dealing with scale dependent layers, the layer id 0 is not visible for instance when loading the map at an extent that shows all California.

Not too sure how to address that issue.

0 Kudos
AlexGole
Occasional Contributor II

I actually found the issue in my code:

it was here in this section of the code"

map.on('layers-add-result', function (evt) {

                             dynaLayer1.setVisibleLayers([0, 4, 5, , 6, 7, 8, 9, 19]);

                             toc = new TOC({

                                 map: map,

                                 layerInfos: [{

                                     layer: dynaLayer1,

                                     title: "Layers"

                                 }]

                             }, 'tocdiv');

                             toc.startup();

                         });

I removed this line:

dynaLayer1.setVisibleLayers([0, 4, 5, , 6, 7, 8, 9, 19]);

It is all back to normal now.

0 Kudos