AGOL Arcpy count features in a hosted services from REST

580
2
12-09-2019 10:38 AM
JordanMiller4
Occasional Contributor III

Is there a way to get the total count of features in a single service with python?

0 Kudos
2 Replies
RandyBurton
MVP Regular Contributor

See: Query (Feature Service)

The Query operation is performed on a feature service resource. The result of this operation is either a feature set for each layer in the query or a count of features for each layer (if returnCountOnly is set to true) or an array of feature IDs for each layer in the query (if returnIdsOnly is set to true).

I did a quick test, with returnCountOnly set to true and using a where "1=1". It returned a count.

URL = "https://services.arcgis.com/xxxxx/arcgis/rest/services/myFeature/FeatureServer/0/query"

query_dict = {
    "where" : "1=1",
    "returnCountOnly" : "true",
    "returnGeometry" : "false",
    "f": "json", "token": token['token'] }

jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))

response = json.loads(jsonResponse.read())

print response

# prints: {u'count': 486, u'serverGens': {u'serverGen': 461482, u'minServerGen': 65364}}
0 Kudos
RandyBurton
MVP Regular Contributor

If you are using the Python API, see: arcgis.features.FeatureLayer.query

and use  return_count_only=True