Using two queries with a few different layers

772
5
Jump to solution
08-06-2013 11:18 AM
EvanKarmazin
New Contributor
I have been working on finding samples that would give me an idea of where to go next. Alot of the sample I have found are back in the 1.6 javascript api
[HTML]http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...
The objective I have is that I would like to allow users to search for a polygon location from one layer, and use a second query to determine if a different layer intersects the graphic returned by that first query.

While I realize the code is old, does the code below add the first query's graphic to the map then call the second query?
var firstGraphic = null;         // +++++Listen for QueryTask onComplete event+++++         dojo.connect(queryTask, "onComplete", function(graphics) {           firstGraphic = graphics.features[0];           var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new dojo.Color([100,100,100]), 3), new dojo.Color([255,0,0,0.20]));           firstGraphic.setSymbol(symbol);           firstGraphic.setInfoTemplate(infoTemplate);            map.graphics.add(firstGraphic);           query.geometry = firstGraphic.geometry;           query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_TOUCHES;           queryTaskTouches.execute(query);           dojo.byId('messages').innerHTML = "<b>Executing Polygon Touches Query...</b>";
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
Not clear what you like to accomplish by reading your code. Do you like to query the TaxDistrict layer first, and then using the returned TaxDistrict features to query against Streams layer using spatial touch or spatial intersect?

There are some issues with your code:
1. Since there will be two queries and one will be performed after another, two functions will be needed, one to perform the first query; the other is the callback function of the first query, and will perform the second query.
2. You haven't defined the callback function for queryTaxDistrictsTask.

Here is the workflow I would have if you like to query TaxDistricts first, and the Streams layer the second based on the TaxDistricts query result.
function queryTaxDistrictsFunc() {
    county = document.getElementById("countyField").value;
    taxDistrict = document.getElementById("taxDistrict").value;
          var queryTaxDistrictsTask = new esri.tasks.QueryTask("url of the TaxDistricts layer");
   
    var queryTaxDistricts = new esri.tasks.Query();
    queryTaxDistricts.returnGeometry = true;
    queryTaxDistricts.outFields = ["DNAME", "CNAME"];
    queryTaxDistricts.where = "CNAME = '" + county + "' and DNAME = '" + taxDistrict + "'";
    queryTaxDistrictsTask.execute(queryTaxDistricts, callbackTaxDistrictsQuery);
}

function callbackTaxDistrictsQuery(featureSet) {
          firstGraphic = featureSet.features[0];
          var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new dojo.Color([100,100,100]), 3), new dojo.Color([255,0,0,0.20]));
          firstGraphic.setSymbol(symbol);
          firstGraphic.setInfoTemplate(infoTemplate);

          map.graphics.add(firstGraphic);
var queryStreamsTask = new esri.tasks.QueryTask("url of the Streams layer");
          query.geometry = firstGraphic.geometry;
          query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_TOUCHES;
          queryStreamsTask.execute(query, callbackStreamsQuery);
}

function callbackStreamsQuery(featureSet) {
// do what ever you like to handle the Streams result here
}

View solution in original post

0 Kudos
5 Replies
JasonZou
Occasional Contributor III
The whole idea should work. Since there are some code missing, it's hard to tell if the code works. What went wrong with your code?
0 Kudos
StephenLead
Regular Contributor III
I would like to allow users to search for a polygon location from one layer, and use a second query to determine if a different layer intersects the graphic returned by that first query.

Does the code below add the first query's graphic to the map then call the second query?


I agree with Jason - this code fragment does what you're asking. If it's failing, it's probably because you haven't defined query or queryTaskTouches

Y
ou also haven't verified that there are any results:

firstGraphic = graphics.features[0];

this might be null if there are no returned graphics.
0 Kudos
EvanKarmazin
New Contributor
That is code from the example, I was just trying to make sure I understood it as far as I had gotten, my code is below:

The query does send, and gets a geometry response, I just was thinking what might be the best way to add the geometry to the map. I am not sure that I have finalized the query. I have been working on using a json file to make the input more accurate for the fields using drop downs.
function queryFunction() {
    county = document.getElementById("countyField").value;
    taxDistrict = document.getElementById("taxDistrict").value;
    //streams = document.getElementById("streamField").value;
    
    var queryStreamsTask = new esri.tasks.QueryTask("link");
          var queryTaxDistrictsTask = new esri.tasks.QueryTask("link");
    
    
    var queryTaxDistricts = new esri.tasks.Query();
    queryTaxDistricts.returnGeometry = true;
    queryTaxDistricts.outFields = ["DNAME", "CNAME"];
    queryTaxDistricts.where = "CNAME = '" + county + "' and DNAME = '" + taxDistrict + "'";
    queryTaxDistrictsTask.execute(queryTaxDistricts);
0 Kudos
JasonZou
Occasional Contributor III
Not clear what you like to accomplish by reading your code. Do you like to query the TaxDistrict layer first, and then using the returned TaxDistrict features to query against Streams layer using spatial touch or spatial intersect?

There are some issues with your code:
1. Since there will be two queries and one will be performed after another, two functions will be needed, one to perform the first query; the other is the callback function of the first query, and will perform the second query.
2. You haven't defined the callback function for queryTaxDistrictsTask.

Here is the workflow I would have if you like to query TaxDistricts first, and the Streams layer the second based on the TaxDistricts query result.
function queryTaxDistrictsFunc() {
    county = document.getElementById("countyField").value;
    taxDistrict = document.getElementById("taxDistrict").value;
          var queryTaxDistrictsTask = new esri.tasks.QueryTask("url of the TaxDistricts layer");
   
    var queryTaxDistricts = new esri.tasks.Query();
    queryTaxDistricts.returnGeometry = true;
    queryTaxDistricts.outFields = ["DNAME", "CNAME"];
    queryTaxDistricts.where = "CNAME = '" + county + "' and DNAME = '" + taxDistrict + "'";
    queryTaxDistrictsTask.execute(queryTaxDistricts, callbackTaxDistrictsQuery);
}

function callbackTaxDistrictsQuery(featureSet) {
          firstGraphic = featureSet.features[0];
          var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new dojo.Color([100,100,100]), 3), new dojo.Color([255,0,0,0.20]));
          firstGraphic.setSymbol(symbol);
          firstGraphic.setInfoTemplate(infoTemplate);

          map.graphics.add(firstGraphic);
var queryStreamsTask = new esri.tasks.QueryTask("url of the Streams layer");
          query.geometry = firstGraphic.geometry;
          query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_TOUCHES;
          queryStreamsTask.execute(query, callbackStreamsQuery);
}

function callbackStreamsQuery(featureSet) {
// do what ever you like to handle the Streams result here
}
0 Kudos
EvanKarmazin
New Contributor
well, as to the objective. I would want the user to be able to search for a stream within the tax district. I wrote it that way, because I wanted to make sure the taxquery worked before I tried to write the next query which would find the specified stream  that intersect or are within the geometry of the taxquery geometry. which would use spatial ref intersect or contain I think
0 Kudos