0

I have been using the L.esri.Query (Esri Leaflet) method "count" to add up the total amount of features falling within a given area. That works fine when using individual features classes at a time (so querying within one file/feature class only), but if I want to add the total features falling within my area regardless of feature class (e.g., points and polygons), I can't seem to get a correct output. Maybe my JavaScript code is wrong... Here is what I have so far:

var stops_count = L.esri.query({
    url: stopsUrl
}).within(neigh_match);

var parks_count = L.esri.query({
    url: parksUrl
}).within(neigh_match);

var count1 = stops_count.count(function(error, count){
});         

var count2 = parks_count.count(function(error, count){
});

var total_counts = count1 + count2;
console.log("counts = " + total_counts);

But what the console actually shows is: counts = [object XMLHttpRequest][object XMLHttpRequest]

So I assume I am not using correct code to add up these counts from the two features classes, one being light rail stops and the other parks. Again, if I run the functions independently I get individual totals (2 for the stops and 14 for parks), but I want to be able to get a summary count of 16 for all the contained features in my area.

I got these examples for querying counts from https://esri.github.io/esri-leaflet/api-reference/tasks/query.html.

Any ideas?

1

this is happening to you because the method count() executes asynchronously. because a web request is made to ask the server for the actual count, the result is not something that you can assign immediately to the method itself, its only available within the callback.

stops_count.count(function(error, stopCount){
  // parksCount is not defined here
  console.log(stopCount);
});         

parks_count.count(function(error, parksCount){
  // stopCount is not defined here
  console.log(stopCount);
});

one way to solve your problem would be to wait to fire the second query after the first has returned a value so that you can add the two together.

a more clever solution would be to use something like a Promise to determine when both asynchronous web requests have been resolved.

2
  • var stCount = Promise.resolve(stops_count.count(function(error, stopsCount){ console.log("stops count = ", stopsCount); })); var pkCount = Promise.resolve(stCount); pkCount.then(function(totCount) { parks_count.count(function(error, parksCount){ console.log("parks count = ", parksCount); console.log("TOTAL = ", parksCount + stCount); }) // run stCount then pkCount }); I tried this and it didn't work. I am not sure how else to proceed to either save the results of each count to a variable (and then add them up), or add them together into one totals variable? – spartmar Sep 27 '16 at 2:32
  • I just need to be able to pass the values from the totals outside of their functions, so I thought using variables would work, but I'm still very stuck on how to do this. – spartmar Sep 27 '16 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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