how to get total count of zoomed feature

2026
2
05-01-2016 10:24 PM
veenahosur2
New Contributor

I have school layer for state.I am able to zoom to selected district from combo box.now i wanted to display total number of schools in that selected district.Can any one tell me how to do it.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
  <title></title>


  <link rel="stylesheet" href="https://js.arcgis.com/3.16/dijit/themes/claro/claro.css">  
  <link rel="stylesheet" href="https://js.arcgis.com/3.16/esri/css/esri.css">
  <style>
    html, body, #map {
      padding:0;
      margin:0;
      height:100%;
    }
  #HomeButton {
      position: absolute;
      top: 95px;
      left: 20px;
      z-index: 50;
    }
   #search {
      display: block;
      position: absolute;
      z-index: 2;
      top: 20px;
      left: 74px;
    }

  </style>

  <script src="https://js.arcgis.com/3.16/"></script>
  <script>


    var map;
    require([
      "esri/map", "esri/dijit/BasemapGallery","esri/dijit/HomeButton","esri/dijit/Search",
       "esri/layers/ArcGISDynamicMapServiceLayer","esri/layers/FeatureLayer", "dojo/parser","esri/dijit/Attribution",
    "dijit/form/ComboBox", "dojo/data/ItemFileReadStore", "dojo/_base/array", "dijit/registry", "esri/tasks/query",
    "esri/symbols/SimpleLineSymbol","esri/Color",  "esri/InfoTemplate",   "esri/symbols/SimpleFillSymbol",
    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/TitlePane",
      "dojo/domReady!"
    ], function(
      Map, BasemapGallery,HomeButton,Search,ArcGISDynamicMapServiceLayer,FeatureLayer,
      parser,Attribution, ComboBox, ItemFileReadStore, array, registry,Query,SimpleLineSymbol,Color,InfoTemplate,SimpleFillSymbol
    ) {

      parser.parse();


      map = new Map("map", {
        basemap: "topo",
         center: [77.2, 14],
         zoom: 7
      });

  var operationalLayer = new ArcGISDynamicMapServiceLayer("http://117.247.176.60:6080/arcgis/rest/services/BC_SH/BC_SH/MapServer");
   var school = new FeatureLayer("http://117.247.176.60:6080/arcgis/rest/services/BC_SH/BC_SH/MapServer/0", {
     mode: FeatureLayer.MODE_SELECTION,
   outFields: ["*"]
  });
  map.addLayers([operationalLayer,school]);

  var search = new Search({
                     map: map
                 }, "search");
                 search.startup();

  var home = new HomeButton({
        map: map
      }, "HomeButton");
      home.startup();

      //add the basemap gallery, in this case we'll display maps from ArcGIS.com including bing maps
      var basemapGallery = new BasemapGallery({
        showArcGISBasemaps: true,
        map: map
      }, "basemapGallery");
basemapGallery.on('load',function(){
basemapGallery.remove('basemap_0');
basemapGallery.remove('basemap_1');    //remove unwanted base map from base map gallery
basemapGallery.remove('basemap_2');
basemapGallery.remove('basemap_3');
basemapGallery.remove('basemap_4');
basemapGallery.remove('basemap_5');
basemapGallery.remove('basemap_8');
});
      basemapGallery.startup();
    
      basemapGallery.on("error", function(msg) {
        console.log("basemap gallery error:  ", msg);
      });


      var populateList = function(results) {
            //Populate the ComboBox with unique values
            var zone;
            var values = [];
            var testVals = {};


            //Loop through the QueryTask results and populate an array
            //with the unique values
            var features = results.features;
            array.forEach(features, function(feature) {
                zone = feature.attributes.DistrictName;
                if (zone) {
                    if (!testVals[zone]) {
                        testVals[zone] = true;
                        values.push({
                            name: zone
                        });
                    }
                }
            });
       
            //Create a ItemFileReadStore and use it for the
            //ComboBox's data source
            var dataItems = {
                identifier: 'name',
                label: 'name',
                items: values
            };
            var store = new ItemFileReadStore({
                data: dataItems
            });
            registry.byId("mySelect").set('store', store);
        }


        queryTask = new esri.tasks.QueryTask("http://117.247.176.60:6080/arcgis/rest/services/BC_SH/BC_SH/MapServer/0");
  var queryTaskTouches = new esri.tasks.QueryTask("http://117.247.176.60:6080/arcgis/rest/services/BC_SH/BC_SH/MapServer/0");
        var query = new esri.tasks.Query();
        query.returnGeometry = false;
        query.outFields = ["DistrictName"];
        query.where = "DistrictName<> ''";
        queryTask.execute(query, populateList);

  app = {
        zoomRow: function(id){ 
  var symbol = new SimpleLineSymbol(
  SimpleLineSymbol.STYLE_SOLID,
  new Color([255,0,0]), 1);


         school.clearSelection();
          var query = new Query();
          query.where = "DistrictName='" + id.toString() + "'";
    query.outFields = ["DistrictName"];
    query.outSpatialReference = { wkid: 32643 };
          query.returnGeometry = true;
          school.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (features) {
                var extent = esri.graphicsExtent(features);


                if (!extent && features.length == 1) {


                    var point = features[0];
                    extent = new esri.geometry.Extent(point.x - 1, point.y - 1, point.x + 1, point.y + 1, point.SpatialReference);
                }


                if (extent) {
                    // assumes the esri map object is stored in the globally-scoped variable 'map'
                    map.setExtent(extent)
                }
          });
        }
      };

});






  </script>
</head>


<body class="claro">
<div id="search"></div>
    <div id="map"
         data-dojo-type="dijit/layout/ContentPane"
         data-dojo-props="region:'center'"
         style="padding:0;">


      <div style="position:absolute; right:10px; top:10px; z-Index:999;">
        <div data-dojo-type="dijit/TitlePane"
             data-dojo-props="title:'Switch Basemap', closable:false, open:false">
          <div data-dojo-type="dijit/layout/ContentPane" style="width:200px; height:280px; overflow:auto;">
            <div id="basemapGallery"></div>
          </div>
        </div>


      </div>
  <div style="position:absolute; right:250px; top:10px; z-Index:999;" value="Select District" id="mySelect" data-dojo-type="dijit.form.ComboBox" onchange="app.zoomRow(document.getElementById('mySelect').value);"/>
     <div id="HomeButton"></div>


    </div>
</body>


</html>
Tags (1)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Veena,

  If you are zooming to the features based on a drop down control then you likely already have an array of the graphics. But having no clue what your code looks like it is hard to provide help here. How are you wanting to display this count info? Do you have a html control in your app you will display it in or are you wanting to use an Alert?...

0 Kudos
JoshVan_Kylen
New Contributor III

You can get a count of features in the map's current extents with JS.

var currentExtent;

map.on("extent-change", function (currentMap) {

     currentExtent = map.extent;

     getProjects();

});

getProjects = function () {    

     var theProjectQueryTask = new QueryTask(urlProjects);

     var theProjectQueryParam = new Query();

     theProjectQueryParam.where = "1=1";

     theProjectQueryParam.geometry = currentExtent;

     theProjectQueryParam.returnGeometry = false;

     theProjectQueryTask.execute(theProjectQueryParam, queryProjs, querySearchError);

};

queryProjs = function (results) {

     console.log(results.features.length + " projects");

};

Also, here is a spatial query solution.

select T2.DistrictNumber, T2.SupervisorName, COUNT(t1.OBJECTID) AS CommentCount

from sde.someCOMMENTS t1

inner join sde.SUPERVISORIALDISTRICT t2

on t1.SHAPE.STWithin(t2.SHAPE) = 1

GROUP BY T2.DistrictNumber, T2.SupervisorName

ORDER BY T2.DistrictNumber;