<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to query only visible Feature Layers with Draw Polygon (3.x) in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1262564#M80416</link>
    <description>&lt;P&gt;Adam,&lt;/P&gt;&lt;P&gt;What would the script look like to query only visible features on screen point in 4.x?&lt;/P&gt;</description>
    <pubDate>Tue, 28 Feb 2023 16:56:44 GMT</pubDate>
    <dc:creator>Rice_GIS</dc:creator>
    <dc:date>2023-02-28T16:56:44Z</dc:date>
    <item>
      <title>How to query only visible Feature Layers with Draw Polygon (3.x)</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1035863#M72022</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I'm working on a slightly older app with ArcGIS JS API 3.x. I want to send a query only on the visible layers. The query works in such a way that a polygon is drawn by the user and only visible layers are queried. All other layers that are hidden should not be taken into account.&lt;BR /&gt;So far I have managed to draw a polygon and send a statistic definition query. It works OK. However, all layers are taken into account. And that's what I don't want&lt;/P&gt;&lt;P&gt;Here is my code. I am grateful for any help!&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        var fields = ["City", "System", "voltage", "kw"];

        var fc_0 = new FeatureLayer("https://arcgis/rest/services/Tracks/MapServer/0", {
            mode: FeatureLayer.MODE_ONDEMAND,
            supportsStatistics: true,
            outFields: ['*'],
            visible: false,
            id: "fLayer"
        });
		var fc_1 = new FeatureLayer("https://arcgis/rest/services/Tracks/MapServer/1", {
            mode: FeatureLayer.MODE_ONDEMAND,
            supportsStatistics: true,
            outFields: ['*'],
            visible: false,
            id: "fLayer"
        });
		
		var fc_2 = new FeatureLayer("https://arcgis/rest/services/Tracks/MapServer/2", {
            mode: FeatureLayer.MODE_ONDEMAND,
            supportsStatistics: true,
            outFields: ['*'],
            visible: false,
            id: "fLayer"
        });
		var fc_3 = new FeatureLayer("https://arcgis/rest/services/Tracks/MapServer/3", {
            mode: FeatureLayer.MODE_ONDEMAND,
            supportsStatistics: true,
            outFields: ['*'],
            visible: false,
            id: "fLayer"
        });
		
		map.addLayers([fc_0, fc_1, fc_2, fc_3]);
		
		  /******Selection Symbol*****/
        var color = new Color([48, 178, 255, 0.8]);
        var selectionSymbol = new SimpleMarkerSymbol({
            "color": color,
            "size": 6,
            "angle": -30,
            "xoffset": 0,
            "yoffset": 0,
            "type": "esriSMS",
            "style": "esriSMSCircle",
            "outline": {
                "color": [48, 255, 0, 0.3],
                "width": 1,
                "type": "esriSLS",
                "style": "esriSLSSolid"
            }
        });
        featureLayer.setSelectionSymbol(selectionSymbol);

        /************************************************************************************************/
        /*****StatistcDefinition*****/
        var getKWH = new StatisticDefinition();
        getKWH.statisticType = "sum";
        getKWH.onStatisticField = "kw";
        getKWH.outStatisticFieldName = "kwh";

        var queryParams = new Query();
        queryParams.outFields = fields;
        queryParams.returnGeometry = true;
        queryParams.outStatistics = [getKWH];
		
		
		
		 /******Draw Polygon*******/
        map.on("load", createToolbar);
        var polygonToolbar;

        function createToolbar(themap) {
            polygonToolbar = new Draw(map);
            polygonToolbar.on("draw-end", getPolygon);
        }

        function activateTool() {
            polygonToolbar.activate(Draw["POLYGON"])
            //deactivate InfoWindow when the user clicks on the draw toolbar.
            map.setInfoWindowOnClick(false);
        }

        function getPolygon(evt) {
            if (evt.geometry.type == "polygon") {
                polygonToolbar.deactivate();
            }
            var polygon = evt.geometry;
            queryParams.geometry = polygon;
            map.graphics.clear();

            switch (evt.geometry.type) {
                case "point":
                case "multipoint":
                    symbol = new SimpleMarkerSymbol();
                    break;
                case "polyline":
                    symbol = new SimpleLineSymbol();
                    break;
                default:
                    symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                        new SimpleLineSymbol(SimpleLineSymbol.STYLE_LONGDASHDOT,
                            new Color([255, 0, 0, 1]), 3), new Color([125, 125, 125, 0.30]));
                    break;
            }
            var graphic = new Graphic(evt.geometry, symbol);
            map.graphics.add(graphic)
			
			// this Part query only one Feature Layer
			
            fc_0.queryFeatures(queryParams, getStats, errback);
        }
		
        function getStats(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
        };
        /**********Event handler for Polygon Tool ***********/
        on(dom.byId('calculateButton'), 'click', function() {
            activateTool();
        });

        on(dom.byId('clearCalculatationButton'), "click", function() {
            $(".stats").empty();
            map.graphics.clear();
            map.setInfoWindowOnClick(true);
        });&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:13:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1035863#M72022</guid>
      <dc:creator>Den-GIS</dc:creator>
      <dc:date>2021-03-12T11:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to query only visible Feature Layers with Draw Polygon (3.x)</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037291#M72084</link>
      <description>&lt;P&gt;Hi Denis,&lt;/P&gt;&lt;P&gt;You can use the 'visible' property of a layer&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/3/jsapi/layer-amd.html#visible" target="_blank"&gt;https://developers.arcgis.com/javascript/3/jsapi/layer-amd.html#visible&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (fc_0.visible){
    fc_0.queryFeatures(queryParams, getStats, errback);
};&lt;/LI-CODE&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 19:54:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037291#M72084</guid>
      <dc:creator>nita14</dc:creator>
      <dc:date>2021-03-16T19:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to query only visible Feature Layers with Draw Polygon (3.x)</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037379#M72087</link>
      <description>&lt;P&gt;Hi Adam,&lt;/P&gt;&lt;P&gt;Thank you for your help! I just tested it and it works. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Only visible layers are queried. That's great. With that I am one step further. But I have another problem, which I overlooked before: I use Statistic Definition to sum up query layers. However, what I get are separate sums of all query layers. The goal, however, is to create a total that includes all of the sums from all layers. I think my approach to solving this is not a good one. It's not good because I keep repeating the getStats() function. And because of the local variable scope, I can't get it out of function. I posted my code below. How can that solve, only query visible layers and then add their sums together. Thank you for your help in advance.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (fc_0.visible){
    fc_0.queryFeatures(queryParams, getStats, errback);
}else {
console.log("fc_0 isn't visible");
};
if (fc_1.visible){
    fc_1.queryFeatures(queryParams, getStats1, errback);
}else {
console.log("fc_1 isn't visible");
};
if (fc_2.visible){
    fc_2.queryFeatures(queryParams, getStats2, errback);
}else {
console.log("fc_2 isn't visible");
};
if (fc_3.visible){
    fc_3.queryFeatures(queryParams, getStats3, errback);
}else {
console.log("fc_3 isn't visible");
};
if (fc_4.visible){
    fc_4.queryFeatures(queryParams, getStats4, errback);
}else {
console.log("fc_4 isn't visible");
};

function getStats(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
  };
function getStats1(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
        };
function getStats2(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
        };
function getStats3(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
        };
function getStats4(results) {
            var _sumkWh, _sum;
            arrayUtils.forEach(results.features, function(feature) {
                _sumkWh = feature.attributes.kwh;
                _sum = _sumkWh.toFixed(2);
                console.log("Sum kWH:", _sumkWh);
            });
            dom.byId("sumKWH").innerHTML = _sum + " " + "[kW]";
        };&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 22:20:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037379#M72087</guid>
      <dc:creator>Den-GIS</dc:creator>
      <dc:date>2021-03-16T22:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to query only visible Feature Layers with Draw Polygon (3.x)</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037517#M72091</link>
      <description>&lt;P&gt;Hi Denis,&lt;/P&gt;&lt;P&gt;I reckon you need to take a look on tihis sample to manage your queries:&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/3/jssamples/query_deferred_list.html" target="_blank"&gt;https://developers.arcgis.com/javascript/3/jssamples/query_deferred_list.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 08:33:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1037517#M72091</guid>
      <dc:creator>nita14</dc:creator>
      <dc:date>2021-03-17T08:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to query only visible Feature Layers with Draw Polygon (3.x)</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1262564#M80416</link>
      <description>&lt;P&gt;Adam,&lt;/P&gt;&lt;P&gt;What would the script look like to query only visible features on screen point in 4.x?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 16:56:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-query-only-visible-feature-layers-with-draw/m-p/1262564#M80416</guid>
      <dc:creator>Rice_GIS</dc:creator>
      <dc:date>2023-02-28T16:56:44Z</dc:date>
    </item>
  </channel>
</rss>

