Select to view content in your preferred language

Calculate the number of points in a freehand polygon

994
4
03-12-2014 06:42 AM
ChristianDebono
Emerging Contributor
I'm drawing a free hand polygon on my map. Now I want to calculate the number of points found in that polygon.

I have done this code, which my now is giving me the number of points found in the map extent rather than the polygon. Now I'm stuck how I'm going to do it since I am new to JavaScript.

function computeZonalStats(evtObj) {
        var geometry = evtObj.geometry;
        map.showZoomSlider();
        map.graphics.clear();

        var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("dashdot", new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]));
        var graphic = new Graphic(geometry, symbol);

        map.graphics.add(graphic);
        toolbar.deactivate();

        var features = [];
        features.push(graphic);

        var featureSet = new FeatureSet();
        featureSet.features = features;

        var count = 0;
        dojo.forEach(featureLayer.graphics, function (feature) {
                count++;
        });
        alert(count);
    }        


Any idea how I could do this please?
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
You could do something like this:

array.forEach(map.graphics.graphics, function(feature, index){                            
  if(feature.geometry.type == 'polygon'){
      var count = 0;
      array.forEach(map.graphics.graphics[index].geometry.rings[0], function(ring){
        count++
    })
    
    console.log(count);
    }
})
0 Kudos
ChristianDebono
Emerging Contributor
Thank you for your reply. The code worked but it is not what I want.

To give you an example I have this map

[ATTACH=CONFIG]32176[/ATTACH]

And I want to calculate the number of points in the polygon drawn. In this case I want to display that 6 points are present in that area.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Sorry, Christian.  I misunderstood you.  Take a look at this jsfiddle.  I believe this is what you are looking for.
0 Kudos
ChristianDebono
Emerging Contributor
That's exactly what I need I needed. Thanks a lot for your help.
0 Kudos