Select to view content in your preferred language

On complete query task event contains many query tasks

1364
4
Jump to solution
09-26-2017 02:49 AM
SaraEL_MALKI
Frequent Contributor

Hi guys,

I am struggling with same problem since I started using the API, 

my concept is: I wanna make a query which has some results, for each result I wanna do another query, and for each result of that last query I wanna do some instructions so the idea is that the hole process is based on the first query, whenever I finish my instructions I wanna display the object id of the FIRST QUERY which lead me to the last one, 

When I try this in the API, I notice that it doesn't happen like that instead they run all the queries, then they go to the second then to the third as if they's not inside each other, I'll give you the code which I am using to draw a dojo chart, that you can understand my problem clearly,

In the code below I wanna for Each Collection circuit, Search if it has Sections then If it does, Search for each Section if it has bags, then for each bag look if it has a "C" or "NC" in a column then calculate the SUM of the bags having "C" and those having "NC" in the COLLECTION CIRCUIT,

so I need an ARRAY containing the name of the circuit and the sum of C and NC bags, 

the code below is giving me the sum of C and NC bags for each SECTION  not for each Collection Circuits,

please HELP ME and clarify what's the error that I'm doing !

Thomas Solow‌ John Zhang  Robert Scheitlin, GISP Rebecca Strauch, GISP 

function do_the_magic_1(){

array_id_troncon=[];

var queryCircuit_6 = new Query();

var QTask_Collecte = new QueryTask("..../MapServer/1"); //Collection Circuits
var queryTaskCircuit_6 = new QueryTask("..../MapServer/11"); //SECTIONS
var queryTaskBac_6 = new QueryTask(".../MapServer/6"); //BAGS

queryCircuit_6.where = "direction IS NULL AND renfort_repassage = 0 AND arrondissement='MGHOGHA'"
queryCircuit_6.outSpatialReference = {wkid:102100};
queryCircuit_6.returnGeometry = false;
queryCircuit_6.outFields = ["*"];
QTask_Collecte.execute(queryCircuit_6); // We have found all the "Collection Circuits"

QTask_Collecte.on("complete",function(eventCircuitCol){

var fsetCircuitC = eventCircuitCol.featureSet;
var FeaturesCC = fsetCircuitC.features;

for (var i = 0, len = FeaturesCC.length; i < len; i++) { // for each Circuit => Search if it has a Section

var FeatureCC = FeaturesCC[i];
var queryCircuit_6 = new Query();
queryCircuit_6.where = "id_circuit_collecte='"+FeatureCC.attributes['objectid']+"'"; //Sections of the circuit
queryCircuit_6.outSpatialReference = {wkid:102100};
queryCircuit_6.returnGeometry = false;
queryCircuit_6.outFields = ["*"];
queryTaskCircuit_6.execute(queryCircuit_6); //We have found all the sections of that circuit 
}
});

queryTaskCircuit_6.on("complete",function(eventTroncon){

var fsetTroncon = eventTroncon.featureSet;
var FeaturesTr = fsetTroncon.features

for (var ii = 0, leni = FeaturesTr.length; ii < leni; ii++) { //for each Section => Search if it has Bags !
var featureTr = FeaturesTr[ii];
var queryBac_6 = new Query();
queryBac_6.where = "id_troncon='"+featureTr.attributes['id_troncon']+"'"; //BAGS that belongs to the section
queryBac_6.outSpatialReference = {wkid:102100};
queryBac_6.returnGeometry = false;
queryBac_6.outFields = ["*"];

queryTaskBac_6.execute(queryBac_6,function(fset){
conforme_oui_1=0; // I wanna initialise these too variables whenever I change the Collection Circuit
conforme_non_1=0;
for(i=0;i<fset.features.length;i++){ // For each bag => look if it has a "C" or "NC" in a specific column 
var featureBac=fset.features[i];

if(featureBac.attributes['positionnement']=='C'){
conforme_oui_1=conforme_oui_1+1;
}
else if(featureBac.attributes['positionnement']=='NC'){
conforme_non_1=conforme_non_1+1;
}
 
}
Conforme_oui_Array.push(conforme_oui_1);
Conforme_non_Array.push(conforme_non_1);

//Rendering
chartPos_ParCircuit.addSeries("Series A", Conforme_oui_Array, {color:"green"} ).addSeries("Series B", Conforme_non_Array,{color:"red"}).render();
});

}

}); //end on complete

} //end function //
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Sara,

   So it sounds like you want to complete the second, third and fourth queries before you proceed to the next circuit. If that is the case then you want to modify your workflow to not do the next circuit OID result query until all the subsequent queries are complete. Try this (it is untested and my needs some work still):

function do_the_magic_1() {

  array_id_troncon = [];

  var queryCircuit_6 = new Query();
  var id_circuit_array = [];
  circuitIndex = 0;

  var QTask_Collecte = new QueryTask("..../MapServer/1"); //Collection Circuits
  var queryTaskCircuit_6 = new QueryTask("..../MapServer/11"); //SECTIONS
  var queryTaskBac_6 = new QueryTask(".../MapServer/6"); //BAGS

  queryCircuit_6.where = "direction IS NULL AND renfort_repassage = 0 AND arrondissement='MGHOGHA'";
  queryCircuit_6.outSpatialReference = {
    wkid: 102100
  };
  queryCircuit_6.returnGeometry = false;
  queryCircuit_6.outFields = ["*"];
  QTask_Collecte.execute(queryCircuit_6, function(eventCircuitCol) {
    var fsetCircuitC = eventCircuitCol.featureSet;
    var FeaturesCC = fsetCircuitC.features;
    for(var i = 0, len = FeaturesCC.length; i < len; i++) { // for each Circuit => Search if it has a Section
      id_circuit_array.push(FeatureCC.attributes['objectid']);
    }
    Query2(id_circuit_array[circuitIndex]);
  });

  function Query2(oid) {
    conforme_oui_1 = 0; // I wanna initialise these too variables whenever I change the Collection Circuit
    conforme_non_1 = 0;
    queryCircuit_6.where = "id_circuit_collecte='" + oid + "'"; //Sections of the circuit
    queryCircuit_6.outSpatialReference = {
      wkid: 102100
    };
    queryCircuit_6.returnGeometry = false;
    queryCircuit_6.outFields = ["*"];
    queryTaskCircuit_6.execute(queryCircuit_6,function(eventTroncon){
      ar fsetTroncon = eventTroncon.featureSet;
      var FeaturesTr = fsetTroncon.features; 

      for (var ii = 0, leni = FeaturesTr.length; ii < leni; ii++) { //for each Section => Search if it has Bags !
        var featureTr = FeaturesTr[ii];
        Query3(featureTr.attributes['id_troncon']);
      }
    }); //We have found all the sections of that circuit
  }

  function Query3(id_troncon) {
    var queryBac_6 = new Query();
    queryBac_6.where = "id_troncon='" + id_troncon + "'"; //BAGS that belongs to the section
    queryBac_6.outSpatialReference = {
      wkid: 102100
    };
    queryBac_6.returnGeometry = false;
    queryBac_6.outFields = ["*"];
    queryTaskBac_6.execute(queryBac_6, function(fset) {
      for(i = 0; i < fset.features.length; i++) { // For each bag => look if it has a "C" or "NC" in a specific column 
        var featureBac = fset.features[i];
        if(featureBac.attributes['positionnement'] == 'C') {
          conforme_oui_1 = conforme_oui_1 + 1;
        } else if(featureBac.attributes['positionnement'] == 'NC') {
          conforme_non_1 = conforme_non_1 + 1;
        }
      }
      Conforme_oui_Array.push(conforme_oui_1);
      Conforme_non_Array.push(conforme_non_1);
      circuitIndex++
      if(circuitIndex < id_circuit_array.length - 1){
        Query2(id_circuit_array[circuitIndex]);
      }else{
        //Rendering
        chartPos_ParCircuit.addSeries("Series A", Conforme_oui_Array, {
          color: "green"
        }).addSeries("Series B", Conforme_non_Array, {
          color: "red"
        }).render();
      }
    });
  }
} //end function //

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Sara,

   I am not sure I completely follow but what happen if you nest all the queries like this:

function do_the_magic_1() {

  array_id_troncon = [];

  var queryCircuit_6 = new Query();

  var QTask_Collecte = new QueryTask("..../MapServer/1"); //Collection Circuits
  var queryTaskCircuit_6 = new QueryTask("..../MapServer/11"); //SECTIONS
  var queryTaskBac_6 = new QueryTask(".../MapServer/6"); //BAGS

  queryCircuit_6.where = "direction IS NULL AND renfort_repassage = 0 AND arrondissement='MGHOGHA'";
  queryCircuit_6.outSpatialReference = {
    wkid: 102100
  };
  queryCircuit_6.returnGeometry = false;
  queryCircuit_6.outFields = ["*"];
  QTask_Collecte.execute(queryCircuit_6, function(eventCircuitFS){
    var FeaturesCC = eventCircuitFS.features;
    for(var i = 0, len = FeaturesCC.length; i < len; i++) { // for each Circuit => Search if it has a Section
      var FeatureCC = FeaturesCC[i];
      var queryCircuit_6 = new Query();
      queryCircuit_6.where = "id_circuit_collecte='" + FeatureCC.attributes['objectid'] + "'"; //Sections of the circuit
      queryCircuit_6.outSpatialReference = {
        wkid: 102100
      };
      queryCircuit_6.returnGeometry = false;
      queryCircuit_6.outFields = ["*"];
      queryTaskCircuit_6.execute(queryCircuit_6, function(fsetTroncon){
        var FeaturesTr = fsetTroncon.features;
    
        for(var ii = 0, leni = FeaturesTr.length; ii < leni; ii++) { //for each Section => Search if it has Bags !
          var featureTr = FeaturesTr[ii];
          var queryBac_6 = new Query();
          queryBac_6.where = "id_troncon='" + featureTr.attributes['id_troncon'] + "'"; //BAGS that belongs to the section
          queryBac_6.outSpatialReference = {
            wkid: 102100
          };
          queryBac_6.returnGeometry = false;
          queryBac_6.outFields = ["*"];
    
          queryTaskBac_6.execute(queryBac_6, function(fset) {
            conforme_oui_1 = 0; // I wanna initialise these too variables whenever I change the Collection Circuit
            conforme_non_1 = 0;
            for(i = 0; i < fset.features.length; i++) { // For each bag => look if it has a "C" or "NC" in a specific column
              var featureBac = fset.features[i];
              if(featureBac.attributes['positionnement'] == 'C') {
                conforme_oui_1 = conforme_oui_1 + 1;
              } else if(featureBac.attributes['positionnement'] == 'NC') {
                conforme_non_1 = conforme_non_1 + 1;
              }
            }
            Conforme_oui_Array.push(conforme_oui_1);
            Conforme_non_Array.push(conforme_non_1);
    
            //Rendering
            chartPos_ParCircuit.addSeries("Series A", Conforme_oui_Array, {
              color: "green"
            }).addSeries("Series B", Conforme_non_Array, {
              color: "red"
            }).render();
          });
        }
      }); //We have found all the sections of that circuit
    }
  }); // We have found all the "Collection Circuits"
} //end function //
0 Kudos
SaraEL_MALKI
Frequent Contributor

No actually that was my first idea but it didn't work at all,

to understand why I've added "alert" like above, I get 10 alerts consecutively giving the Circuit Id (I have 10 Circuits matching the condition) , and after them I get MANY alerts of the sections, 

What I want is having One alert of the Id circuit then many alerts of the section then, another alert of the circuit then many alerts of the section  ...

function do_the_magic_1() {

  array_id_troncon = [];

  var queryCircuit_6 = new Query();

  var QTask_Collecte = new QueryTask("..../MapServer/1"); //Collection Circuits
  var queryTaskCircuit_6 = new QueryTask("..../MapServer/11"); //SECTIONS
  var queryTaskBac_6 = new QueryTask(".../MapServer/6"); //BAGS

  queryCircuit_6.where = "direction IS NULL AND renfort_repassage = 0 AND arrondissement='MGHOGHA'";
  queryCircuit_6.outSpatialReference = {
    wkid: 102100
  };
  queryCircuit_6.returnGeometry = false;
  queryCircuit_6.outFields = ["*"];
  QTask_Collecte.execute(queryCircuit_6, function(eventCircuitFS){
            conforme_oui_1 = 0; // I wanna initialise these too variables whenever I change the Collection Circuit
            conforme_non_1 = 0;
    var FeaturesCC = eventCircuitFS.features;
    for(var i = 0, len = FeaturesCC.length; i < len; i++) { // for each Circuit => Search if it has a Section
      var FeatureCC = FeaturesCC[i];

alert('Circuit ID is'+FeatureCC.attributes['layer']);

      var queryCircuit_6 = new Query();
      queryCircuit_6.where = "id_circuit_collecte='" + FeatureCC.attributes['objectid'] + "'"; //Sections of the circuit
      queryCircuit_6.outSpatialReference = {
        wkid: 102100
      };
      queryCircuit_6.returnGeometry = false;
      queryCircuit_6.outFields = ["*"];
      queryTaskCircuit_6.execute(queryCircuit_6, function(fsetTroncon){
        var FeaturesTr = fsetTroncon.features;

alert('Section is found');

   
        for(var ii = 0, leni = FeaturesTr.length; ii < leni; ii++) { //for each Section => Search if it has Bags !
          var featureTr = FeaturesTr[ii];
          var queryBac_6 = new Query();
          queryBac_6.where = "id_troncon='" + featureTr.attributes['id_troncon'] + "'"; //BAGS that belongs to the section
          queryBac_6.outSpatialReference = {
            wkid: 102100
          };
          queryBac_6.returnGeometry = false;
          queryBac_6.outFields = ["*"];
   
          queryTaskBac_6.execute(queryBac_6, function(fset) {

            for(i = 0; i < fset.features.length; i++) { // For each bag => look if it has a "C" or "NC" in a specific column
              var featureBac = fset.features[i];
              if(featureBac.attributes['positionnement'] == 'C') {
                conforme_oui_1 = conforme_oui_1 + 1;
              } else if(featureBac.attributes['positionnement'] == 'NC') {
                conforme_non_1 = conforme_non_1 + 1;
              }
            }
            Conforme_oui_Array.push(conforme_oui_1);
            Conforme_non_Array.push(conforme_non_1);
   
            //Rendering
            chartPos_ParCircuit.addSeries("Series A", Conforme_oui_Array, {
              color: "green"
            }).addSeries("Series B", Conforme_non_Array, {
              color: "red"
            }).render();
          });
        }
      }); //We have found all the sections of that circuit
    }
  }); // We have found all the "Collection Circuits"
} //end function //
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Sara,

   So it sounds like you want to complete the second, third and fourth queries before you proceed to the next circuit. If that is the case then you want to modify your workflow to not do the next circuit OID result query until all the subsequent queries are complete. Try this (it is untested and my needs some work still):

function do_the_magic_1() {

  array_id_troncon = [];

  var queryCircuit_6 = new Query();
  var id_circuit_array = [];
  circuitIndex = 0;

  var QTask_Collecte = new QueryTask("..../MapServer/1"); //Collection Circuits
  var queryTaskCircuit_6 = new QueryTask("..../MapServer/11"); //SECTIONS
  var queryTaskBac_6 = new QueryTask(".../MapServer/6"); //BAGS

  queryCircuit_6.where = "direction IS NULL AND renfort_repassage = 0 AND arrondissement='MGHOGHA'";
  queryCircuit_6.outSpatialReference = {
    wkid: 102100
  };
  queryCircuit_6.returnGeometry = false;
  queryCircuit_6.outFields = ["*"];
  QTask_Collecte.execute(queryCircuit_6, function(eventCircuitCol) {
    var fsetCircuitC = eventCircuitCol.featureSet;
    var FeaturesCC = fsetCircuitC.features;
    for(var i = 0, len = FeaturesCC.length; i < len; i++) { // for each Circuit => Search if it has a Section
      id_circuit_array.push(FeatureCC.attributes['objectid']);
    }
    Query2(id_circuit_array[circuitIndex]);
  });

  function Query2(oid) {
    conforme_oui_1 = 0; // I wanna initialise these too variables whenever I change the Collection Circuit
    conforme_non_1 = 0;
    queryCircuit_6.where = "id_circuit_collecte='" + oid + "'"; //Sections of the circuit
    queryCircuit_6.outSpatialReference = {
      wkid: 102100
    };
    queryCircuit_6.returnGeometry = false;
    queryCircuit_6.outFields = ["*"];
    queryTaskCircuit_6.execute(queryCircuit_6,function(eventTroncon){
      ar fsetTroncon = eventTroncon.featureSet;
      var FeaturesTr = fsetTroncon.features; 

      for (var ii = 0, leni = FeaturesTr.length; ii < leni; ii++) { //for each Section => Search if it has Bags !
        var featureTr = FeaturesTr[ii];
        Query3(featureTr.attributes['id_troncon']);
      }
    }); //We have found all the sections of that circuit
  }

  function Query3(id_troncon) {
    var queryBac_6 = new Query();
    queryBac_6.where = "id_troncon='" + id_troncon + "'"; //BAGS that belongs to the section
    queryBac_6.outSpatialReference = {
      wkid: 102100
    };
    queryBac_6.returnGeometry = false;
    queryBac_6.outFields = ["*"];
    queryTaskBac_6.execute(queryBac_6, function(fset) {
      for(i = 0; i < fset.features.length; i++) { // For each bag => look if it has a "C" or "NC" in a specific column 
        var featureBac = fset.features[i];
        if(featureBac.attributes['positionnement'] == 'C') {
          conforme_oui_1 = conforme_oui_1 + 1;
        } else if(featureBac.attributes['positionnement'] == 'NC') {
          conforme_non_1 = conforme_non_1 + 1;
        }
      }
      Conforme_oui_Array.push(conforme_oui_1);
      Conforme_non_Array.push(conforme_non_1);
      circuitIndex++
      if(circuitIndex < id_circuit_array.length - 1){
        Query2(id_circuit_array[circuitIndex]);
      }else{
        //Rendering
        chartPos_ParCircuit.addSeries("Series A", Conforme_oui_Array, {
          color: "green"
        }).addSeries("Series B", Conforme_non_Array, {
          color: "red"
        }).render();
      }
    });
  }
} //end function //
SaraEL_MALKI
Frequent Contributor

thank you mister Robert for your time, your question has helped me no doubt in getting what I wanted, many thanks

0 Kudos