Monday 1 June 2015

How to count point features in polygon using ArcGIS API for JavaScript?


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?



Answer



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. Clicking on feature to create particular new map using ArcGIS API for JavaScript?


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);
)};

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...