Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have published in my ArcGIS Server two layers: A polygon one and a point feature one. Points are contained in polygons.

I would like to know how to count the number of points belonging/contained to a polygon using ArcGIS Javascript API. So, the functionality would be, clicking in one of the polygons and then, present somewhere an integer (in an Infowindow or textbox in sidebar) saying "There are X points in this polygon".

Could you provide any ideas or examples?

share|improve this question
up vote 4 down vote accepted

Check out this post I just wrote on selecting a polygon and highlighting it. Once you have the polygon that was clicked on, you simply do a query with intersection. ArcGIS JS Api: Click on feature, create a particular new map?

You could substitute below into the mapOnClick(evt) function above...

var queryTask = new esri.tasks.QueryTask(YourServiceName),
    query = new esri.tasks.Query(),
    countOfFeatures = 0;

query.geometry = geom;
query.returnGeometry = true;
query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
queryTask.execute(query, function (results) {
    if (results.features && results.features.length > 0) {
        dojo.forEach(results.features, function (feature) {
    alert("Number of points in polygon " + countOfFeatures);
share|improve this answer
Yes, that is exactly what I needed. Thank you very much! – iamgin Nov 15 '12 at 18:26

If you can't process the data beforehand, another method may be to add the layers as separate FeatureLayers in the map, and use the geometry of the selected polygon to query the point layer. When the selection is complete, do a count of the features using FeatureLayer::getSelectedFeatures and use that count to provide information in your popup.

Here is a sample where the FeaureLayer is selected using an extent as the query geometry.

share|improve this answer

One good way to do this would be to cheat:

Calculate Statistics for the polygon/point data apriori, adding the relevant field attributes. Use JS identify to simply pop up the relevant field information on click.

share|improve this answer
That's a good workaround - but a potential problem is that you'll need to re-calculate the statistics anytime either layer changes. If you can't guarantee that will happen (especially if you're building this site for someone else to maintain) it could lead to future problems – Stephen Lead Jan 26 '15 at 22:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.