Count all points in all polygons in a feature layer when map is loaded

1648
5
Jump to solution
04-21-2014 12:41 AM
ChristianDebono
New Contributor II
I have two layers. One containing points and the other containing polygons.

I would like to count the number of points belonging to a polygon when the map loads for all the polygons in the feature layer using ArcGIS Javascript API. I have this functionality but it is affecting the performance of the map, since it is updating when the map is updated.

     var polygonFeature = new FeatureLayer("feature layer with polygons", {             mode: FeatureLayer.MODE_ONDEMAND,             outFields: ["*"]         });     map.on("update-end", function () {                     array.forEach(polygonFeature.graphics, function (feature) {                         var name = feature.attributes.NAME;                         var query = new Query();                         var queryTask = new QueryTask("feature layer with points");                         query.geometry = feature.geometry                         query.returnGeometry = true;                          queryTask.executeForCount(query, function (count) {                             console.log("Locality " + name + " has " + count + " points");                         });                     });                 });
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
This code works and keeps the ability to zoom in and out. Are there other things going on in your code that are causing errors? For example, when I used your code to get the number of cities in each state, I started running into proxy errors (too many city geometries returned, possibly) and other problems. Do you need the geometry returned if you're only doing a count?

<!DOCTYPE html> <html> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />     <title>Simple Map</title>     <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">     <style>         html, body, #map {             height: 100%;             width: 100%;             margin: 0;             padding: 0;         }          body {             background-color: #FFF;             overflow: hidden;             font-family: "Trebuchet MS";         }     </style>     <script src="http://js.arcgis.com/3.9/"></script>     <script>         var map;          require(["esri/map", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "dojo/on", "dojo/_base/array", "dojo/domReady!"],             function (Map, FeatureLayer, Query, QueryTask, on, array) {                 map = new Map("map", {                     basemap: "topo",                     center: [-122.45, 37.75], // longitude, latitude                     zoom: 7                 });                  var polygonLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2", {                     mode: FeatureLayer.MODE_ONDEMAND,                     outFields: ["*"]                 });                  var pointsLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0", {                     mode: FeatureLayer.MODE_ONDEMAND,                     outFields: ["*"]                 });                  map.addLayers([polygonLayer, pointsLayer]);                  on.once(map, "update-end", function () {                     var query = new Query();                     query.where = "STATE_NAME = 'California'";                     var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");                     queryTask.executeForCount(query, function (count) {                         console.log("California has " + count + " cities");                     });                      //array.forEach(polygonLayer.graphics, function (feature) {                     //    var name = feature.attributes.STATE_NAME;                     //    var query = new Query();                     //    var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");                     //    query.geometry = feature.geometry                     //    query.returnGeometry = true;                      //    queryTask.executeForCount(query, function (count) {                     //        console.log("Locality " + name + " has " + count + " points");                     //    });                     //});                 });              });     </script> </head>  <body>     <div id="map"></div> </body> </html> 

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
If you're adding the layers to the map using map.addLayers, then you can use the event "layers-add-result". This event fires after all the layers have been added to the map.

You could also still use the "update-end" event, but have it run only a single time. The dojo/on event handler has the once function, which will remove the event listener after running once.

require(["dojo/on"], function(on){
  on.once(map, "update-end", function(){
    // will only fire once...
  });
});
0 Kudos
ChristianDebono
New Contributor II
Thanks for the reply Ken Buja, I have tried the "on.once" it works, but its not letting me zoom on the map
0 Kudos
KenBuja
MVP Esteemed Contributor
Can you post your code to see what's going on?
0 Kudos
ChristianDebono
New Contributor II
function countReport() {
    require(["dojo/on", "dojo/_base/array", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol", "esri/graphic", "dojo/_base/Color"],
    function (on, array, FeatureLayer, Query, QueryTask, SimpleFillSymbol, SimpleLineSymbol, Graphic, Color) {
        var polygonFeature = new FeatureLayer("polygon feature layer", {
            mode: FeatureLayer.MODE_ONDEMAND,
            outFields: ["*"]
        });

        map.addLayer(polygonFeature);

        on.once(map, "update-end", function () {
            array.forEach(polygonFeature.graphics, function (feature) {
                var name = feature.attributes.NAME;
                var query = new Query();
                var queryTask = new QueryTask("points feature layer");
                query.geometry = feature.geometry
                query.returnGeometry = true;

                queryTask.executeForCount(query, function (count) {
                    console.log("Locality " + name + " has " + count + " points");                   
                });
            });
        });

    });
}
0 Kudos
KenBuja
MVP Esteemed Contributor
This code works and keeps the ability to zoom in and out. Are there other things going on in your code that are causing errors? For example, when I used your code to get the number of cities in each state, I started running into proxy errors (too many city geometries returned, possibly) and other problems. Do you need the geometry returned if you're only doing a count?

<!DOCTYPE html> <html> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />     <title>Simple Map</title>     <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">     <style>         html, body, #map {             height: 100%;             width: 100%;             margin: 0;             padding: 0;         }          body {             background-color: #FFF;             overflow: hidden;             font-family: "Trebuchet MS";         }     </style>     <script src="http://js.arcgis.com/3.9/"></script>     <script>         var map;          require(["esri/map", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "dojo/on", "dojo/_base/array", "dojo/domReady!"],             function (Map, FeatureLayer, Query, QueryTask, on, array) {                 map = new Map("map", {                     basemap: "topo",                     center: [-122.45, 37.75], // longitude, latitude                     zoom: 7                 });                  var polygonLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2", {                     mode: FeatureLayer.MODE_ONDEMAND,                     outFields: ["*"]                 });                  var pointsLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0", {                     mode: FeatureLayer.MODE_ONDEMAND,                     outFields: ["*"]                 });                  map.addLayers([polygonLayer, pointsLayer]);                  on.once(map, "update-end", function () {                     var query = new Query();                     query.where = "STATE_NAME = 'California'";                     var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");                     queryTask.executeForCount(query, function (count) {                         console.log("California has " + count + " cities");                     });                      //array.forEach(polygonLayer.graphics, function (feature) {                     //    var name = feature.attributes.STATE_NAME;                     //    var query = new Query();                     //    var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");                     //    query.geometry = feature.geometry                     //    query.returnGeometry = true;                      //    queryTask.executeForCount(query, function (count) {                     //        console.log("Locality " + name + " has " + count + " points");                     //    });                     //});                 });              });     </script> </head>  <body>     <div id="map"></div> </body> </html> 
0 Kudos