Is there any way to get a count of the number of features in a FeatureLayer?

8534
27
12-19-2016 06:19 AM
JohnAdams
New Contributor III

I have a FeatureLayer map service from which I would like to get a count of the number of features. I would have thought such a thing would be a basic kind of functionality, but looking through the Javascript API for FeatureLayer, I can find nothing that looks like it does that. Unless I'm missing something?

What I would like to do is something like this:

var myLayer = new FeatureLayer("service url here with maybe a filter");

var count = myLayer.count;

Seems to be a basic thing to have - right? I guess not?

Tags (2)
27 Replies
RobertScheitlin__GISP
MVP Emeritus

John,

   You just do a queryCount.

FeatureLayer | API Reference | ArcGIS API for JavaScript 3.18 | queryCount 

and use a where clause like 1=1

0 Kudos
JohnAdams
New Contributor III

Yeah I just discovered that. Not really what I want, since the feature layers I'm using have already been created using a filter/query, so it's pretty redundant to have to ask the exact same query again on a layer that's already been created using that same query. Would be much simpler to just have something like myLayer.count.

I noticed the 4.1 API has the query as an optional input, but unfortunately I have to use 3.18. I guess it'll have to do.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   If you already have a definition query applied and the number of features returned is less then the max feature count then you can just use this simple line of code:

var len = FeatureLayer.graphics.length;

You need to be sure that the featurelayer is fully loaded before calling this line though

0 Kudos
JohnAdams
New Contributor III

Thanks. But when I try that is always returns 0, even though I know there should be either 2 or 5, depending on one of my inputs. Got the other one to work using those same inputs.

I'll play around with it and see if I can get it to work.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   You have to ensure that the layer is done loading and has the definition query applied:

on.once(FeatureLayer, "update-end", function(){
   var len = FeatureLayer.graphics.length;
});
0 Kudos
JohnAdams
New Contributor III

I presume that means that merely having already done map.AddLayer(myLayer) isn't sufficient.

0 Kudos
JohnAdams
New Contributor III

Now that I've actually tried it that way, if I understand what I'm seeing correctly, it only "grabs" that number after the entire map has refreshed. Unfortunately I'll need that number to load into a chart that will be shown on the same web map, so I'll need to get that number before the web map reloads, in which case it looks like I'll have to use the query method after all.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   It does not need the entire map to load but it does not that specific layer to be completely loaded.

0 Kudos
JohnAdams
New Contributor III

OK, follow-up to everything here. I'm using the query method - is there any way to get it to return a value? I want to do something like:

var query = new Query();
query.where = "Zip = '" + SelectedZip + "'";
featLayer.queryCount(query, function(count) {
}, newfunction(count));

Then later in my code ...

function newfunction(number)

{

   return number;

}

... so that I have access to whatever "count" was. Or maybe:

var query = new Query();
query.where = "Zip = '" + SelectedZip + "'";
featLayer.queryCount(query, function(count) {

var num = count;
}, newfunction(num));

... and then again, same "newfunction" as above. I've also tried something like:

var query = new Query();
query.where = "Zip = '" + SelectedZip + "'";
var num = featLayer.queryCount(query, function(count) {

return count;
});

So that, once again, I can access "count" elsewhere. Whenever I try to assign a variable within the queryCount function, it disappears into thin air after the function is over and done with, even if I declared that variable globally.

Hope that made sense.

0 Kudos