get the count of features for a layer-definition applied layer?

2662
2
Jump to solution
12-09-2016 10:19 AM
Alexwang
Occasional Contributor II

Hello All, I set a layer definition for one of layers. How to get the count of that layer after the layer definition? I can use query task to query the layer and then get the count, but just wonder if there is an quicker way to do it like layer.count or something like that

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

   If the layer is a ArcGISDynamicMapServiceLayer then no you will have to go to the server and query.

If it is a FeatureLayer then you can get the length of the graphics property:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>FeatureLayer</title>

<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/css/esri.css">
<script src="https://js.arcgis.com/3.18/"></script>

<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>

<script>
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "dojo/on",
    "dojo/domReady!"
  ],
  function(
    Map,
    FeatureLayer,
    on
  ) {

    var map = new Map("map", {
      basemap: "hybrid",
      center: [-82.44109, 35.6122],
      zoom: 17
    });

    // Carbon storage of trees in Warren Wilson College.
    var featureLayer = new FeatureLayer("https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0", {
      "outFields": ["*"]
    });
    featureLayer.setDefinitionExpression("Tree_Age > 5");
    map.addLayer(featureLayer);
    on.once(featureLayer, "update-end", function() {
      console.info(featureLayer.graphics.length);
    })
  });
</script>
</head>

<body>
  <div id="map"></div>
</body>

</html>

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

   If the layer is a ArcGISDynamicMapServiceLayer then no you will have to go to the server and query.

If it is a FeatureLayer then you can get the length of the graphics property:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>FeatureLayer</title>

<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/css/esri.css">
<script src="https://js.arcgis.com/3.18/"></script>

<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>

<script>
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "dojo/on",
    "dojo/domReady!"
  ],
  function(
    Map,
    FeatureLayer,
    on
  ) {

    var map = new Map("map", {
      basemap: "hybrid",
      center: [-82.44109, 35.6122],
      zoom: 17
    });

    // Carbon storage of trees in Warren Wilson College.
    var featureLayer = new FeatureLayer("https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0", {
      "outFields": ["*"]
    });
    featureLayer.setDefinitionExpression("Tree_Age > 5");
    map.addLayer(featureLayer);
    on.once(featureLayer, "update-end", function() {
      console.info(featureLayer.graphics.length);
    })
  });
</script>
</head>

<body>
  <div id="map"></div>
</body>

</html>
0 Kudos
Alexwang
Occasional Contributor II

Thanks Robert. the layer I am setting layer definition is a dynamic service layer. I didn't see it has a graphics property. However, inspired from the codes you posted, I was able to convert the dynamic service layer to a feature layer (see below) and then get the count. Works perfect!

0 Kudos