Select to view content in your preferred language

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

10838
27
12-19-2016 06:19 AM
JohnAdams
Occasional Contributor

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
JohnAdams
Occasional Contributor

I finally figured it out. Thanks, your comment above was helpful. My code is actually a lot more complicated than the examples I gave above so it was really hard to figure out where to put things.

0 Kudos
JohnAdams
Occasional Contributor

Eh, I take that back, it didn't solve the problem. It just moved it somewhere else.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   I am not sure what else I can do. I provided three different simple samples that work for what you requested. Can you take your code and substitute public services and post it all?

0 Kudos
JohnAdams
Occasional Contributor

My issue now is, while your (2nd) example calls a deferred function directly from the event handler, due to the complexity of my program, I need that event handler to first call a RedrawLayer function, which in turn will call one of 13 other functions in a switch statement. In those 13 other functions is where I assign a global variable based on a queryCount (which is used to make a pie chart). I *could* turn those 13 other functions into one giant if-else tree, which would let me make the RedrawLayer function be the deferred function, but that would be an incredible mess. So now, basically I'm trying to figure out where and what to defer.

Interestingly, if I put an alert popup at certain spots, the whole thing works. But if I do nothing more than comment out that alert popup it *doesn't* work and it seems it's not grabbing my global variable and my chart doesn't draw. Ultimately I don't want the alert popup to be there so I have to figure out how to make it work without the popup. It's hard to figure out exactly what is being drawn, and when it is being drawn, in what order.

BTW how do you insert code like you've been doing? Don't see any kind of "insert code" function in the toolbar.

0 Kudos
JohnAdams
Occasional Contributor

Actually, I think I just figured out that it is the redrawing of my pie chart I need to defer. Hopefully that'll make it easier.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus
JohnAdams
Occasional Contributor
#include <iostream.h>
void main(void)
{
    cout << "Thanks!";
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
ImtiazSyed
Occasional Contributor

Using ArcGIS API for Python

from arcgis.gis import GIS

gis = GIS("https://site.maps.arcgis.com", "username", "password")

feature_layer_item = gis.content.search("<item-id>")[0]
flayers = feature_layer_item.layers

flayer = flayers[0]

count = flayer.query(return_count_only=true)
0 Kudos