Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

Thanks for your help,

share|improve this question
add comment

3 Answers

up vote 3 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) {
            countOfFeatures++;
        )};
    }
    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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.