Select to view content in your preferred language

How to count features in a polygon

4796
12
Jump to solution
03-24-2014 11:24 PM
ChristianDebono
Emerging Contributor
I have published 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 user clicks it using ArcGIS Javascript API. I have already done the functionality of highlighting the polygon when the user clicks it. Now I want that an alert message will show up with the number of features in that polygon when the polygon is clicked. I already tried to do some code but its not giving the correct answer.

function selectByCouncil() {                 var localCouncils = new FeatureLayer("feature layer with polygons", {                     mode: FeatureLayer.MODE_SNAPSHOT,                     outFields: ["*"]                 });                  map.addLayer(localCouncils);                  var highlightSymbol = new SimpleFillSymbol(                   SimpleFillSymbol.STYLE_SOLID,                   new SimpleLineSymbol(                     SimpleLineSymbol.STYLE_SOLID,                     new Color([255, 0, 0]), 1),                   new Color([125, 125, 125, 0.35])                 );                  map.on("load", function () {                     map.graphics.enableMouseEvents();                 });                  localCouncils.on("click", function (evt) {                     map.graphics.clear();                     var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol);                     map.graphics.add(highlightGraphic);                      var query = new Query();                     var queryTask = new QueryTask(feature layer with points);                     query.geometry = evt.graphic.geometry;                     query.returnGeometry = true;                      var countOfFeatures = 0;                      queryTask.execute(query, function (results) {                         dojo.forEach(results.features, function (feature) {                             countOfFeatures++;                                                     });                          alert("Number of points in polygon " + countOfFeatures);                     });                                      });             }


Any ideas on how this could be done are much appreciated. Thanks
0 Kudos
12 Replies
ChristianDebono
Emerging Contributor
Thanks Jake Skinner, its everything I wanted 🙂
0 Kudos
ChristianDebono
Emerging Contributor
I have another problem with the function that is done on click of the polygon. I want to calculate the centre of the polygon to print a label in it. I am using the geometry service label point, but the function is not firing and I'm suspecting this is happening because of the polygon geometry.

function selectByPolygon() {
                //change the feature layer according to the requirments. This feature layer should contain polygons
                var polygonLayer= new FeatureLayer("Feature layer with polygons", { "opacity": 0.5 }, {
                    mode: FeatureLayer.MODE_ONDEMAND,
                    outFields: ["*"]
                });

                map.addLayer(polygonLayer);

                var highlightSymbol = new SimpleFillSymbol(
                  SimpleFillSymbol.STYLE_SOLID,
                  new SimpleLineSymbol(
                    SimpleLineSymbol.STYLE_SOLID,
                    new Color([255, 0, 0]), 1),
                  new Color([125, 125, 125, 0.35])
                );

                localCouncils.on("click", function (evt) {
                    map.graphics.clear();
                    var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol);
                    map.graphics.add(highlightGraphic);

                    var query = new Query();
                    var queryTask = new QueryTask("feature layer with points");
                    query.geometry = evt.graphic.geometry;
                    query.returnGeometry = true;
                    query.outSpatialReference = map.spatialReference;
                    query.outFields = ["*"];

                    queryTask.execute(query, function (results) {                               

                        geometryService.labelPoints(evt.graphic.geometry, function (labelPoints) {                           
                            var font = new Font("20px", Font.STYLE_NORMAL, Font.VARIANT_NORMAL, Font.WEIGHT_BOLDER);
                            array.forEach(labelPoints, function (labelPoint) {

                                var textSymbol = new TextSymbol(
                                "X: " + number.format(labelPoint.x) + ", Y: " + number.format(labelPoint.y),
                                font, new Color([0, 0, 0])
                            );
                                var labelPointGraphic = new Graphic(labelPoint, textSymbol);
                                map.graphics.add(labelPointGraphic);
                            });
                        });
                    });
                });
            }
0 Kudos
ChristianDebono
Emerging Contributor
You can use the following function to report how many points are within each polygon once the map is loaded:

var polygonFeature= new FeatureLayer(my feature layer with polygons, {
                    mode: FeatureLayer.MODE_ONDEMAND,
                    outFields: ["*"]
                });

map.addLayer(polygonFeature);

map.on("update-end", function(){
    array.forEach(polygonFeature.graphics, function(feature){
        var OID = feature.attributes.OBJECTID;
        var query = new Query();
        var queryTask = new QueryTask("url to points");
        query.geometry = feature.geometry
        query.returnGeometry = true;       
        
        queryTask.executeForCount(query, function(count){
            console.log("ObjectID " + OID + " has " + count + " points");
        })
    })
})


You will need to add the dojo/_base/array module to your code.  For adding the text to your map, take a look at the TextSymbol class.


Is it possible to do this functionality not in the update-end of the map (I wish to do this only once), because I am noticing that it is effecting the performance of my application drastically since it is always updating even when I zoom on the map
0 Kudos