polygon input as geometry to a query/queryTask

914
2
04-11-2014 12:31 PM
TracySchloss
Frequent Contributor
I have a basic county boundary service that I'm using as input to a FindTask, based on a county name pick list.  Later, the user should have the option to use the geometry of that found polygon as the input geometry of a querytask which searches on other county polygon layers.  My basic county service is in UTM, but my project is wkid 102100 and so is the layer I'm trying to search in the queryTask. I have the output spatialReference of my findParameters set to that same wkid, which I assume should make the geometry from the find be in the right coordinates.

If the use clicks the "find neighbors" button, it executes the findAdjacentCounties function, which runs a query/QueryTask using Query.SPATIAL_REL_TOUCHES.  The query executes without error, but it never returns any features, I assume because the inputGeometry I'm providing doesn't actually touch any of other features.  The coordinates of the countyGeometry variable says it's wkid 102100. 

//This functions searches the countyLayer, defined earlier to zoom to a county and saves the geometry
   function zoomCounty () {
    var findTask = new FindTask(countyLayer.url);  //layer defined earlier
     var findParams = new FindParameters();
     findParams.returnGeometry = true;
     findParams.layerIds = [0];
     findParams.searchFields = ["COUNTYNAME"]; 
    findParams.outSpatialReference = spatialReference;    
     var countyName = registry.byId("countySelect").value;
     findParams.searchText = countyName;
     findTask.execute(findParams, function (results) {
        map.setExtent(results[0].feature.geometry.getExtent());
        currentExtent = map.extent;
        currentCenter = results[0].feature.geometry.getCentroid();
        countyGeometry = results[0].feature.geometry;  // polygon geometry getting stored
    });
} 
//this executes when the user selects the 'find neighbor' button
  function findAdjacentCounty() {
    var currentVis = ncdmLayer.visibleLayers;//layer defined earlier
    var queryTask = new QueryTask(ncdmLayer.url+"/" +currentVis);
    var query = new Query();
    query.geometry = countyGeometry;
    query.outSpatialReference = spatialReference;
    query.spatialRelationship = Query.SPATIAL_REL_TOUCHES;
    query.outFields=["NAME", "NAME2", "RATE"];
  //  query.returnGeometry = true;
    queryTask.on('error', taskErrorHandler);
    queryTask.execute(query, adjacentResultHandler);
  }
//this function executes, but results is always empty!
     function adjacentResultHandler(results) {
       countyList.length = 0; //arrays defined earlier
       countyRateList.length = 0;
      arrayUtil.forEach(results.features, function(feature){
        countyList.push(feature.attributes.NAME);
        countyRateList.push(feature.attributes.RATE);     
         });
       console.log(countyList);
       console.log(countyRateList);

    }   


I made a basic sample before I started, but it was using all the same layer, both for the find and query tasks.  Now I need to use two different ones.
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Tracy,

This should work if your outSpatialReference is set to the same as the map.  How did you define the 'spatialReference' variable?  i.e.

var spatialReference = new SpatialReference(102100);
0 Kudos
TracySchloss
Frequent Contributor
I do.  These are some pretty old map services made a few years ago that I thought were in WGS 1984 Aux Sphere (102100).  When I studied them more closely, I realized they were in just WGS 1984, which is something like wkid 4326.   I ended up going back and fixing my original MXD files and getting them into the projection I really meant for them to be in (102100).  All that and it still wasn't working!!!

I know it should have worked, but my end around was to create an array of adjacent county names from the original county service.  Then I generated a rather elaborate where clause from that array and used that as input to my 2nd layer (both had county name fields) and got at the information that way.

function findAdjacentCounties(geometry) {
          var queryTask = new QueryTask(countyLayer.url+"/0");
          var query = new Query();
          query.geometry = geometry;
          query.spatialRelationship = Query.SPATIAL_REL_TOUCHES;
          query.outFields=["*"];
          queryTask.on('error', taskErrorHandler);
          queryTask.execute(query, findAdjResultHandler);
        }
        
        function findAdjResultHandler(results) {
           countyList.length = 0;
           var fs = results.features;
           countyList.push(registry.byId("countySelect").value.toUpperCase());//to get data for the current county too
           arrayUtil.forEach(results.features, function(feature){
            countyList.push (feature.attributes.COUNTYNAME.toUpperCase());
             });
           clause = createWhere(countyList);
          }
        
        function createWhere(inputArray){//creates a query where clause based on the adjacent county names
          var s = "";
          arrayUtil.forEach(inputArray, function(item, i){
           if (i < inputArray.length - 1){
             s = s + '"NAME2" = ' + "'" + inputArray + "'" + ' OR ';
           }else {
             s = s + '"NAME2" = ' + "'" + inputArray + "'";
           }    
          });
       //   console.log("where clause is " + s);
          return s;
        }
        function queryAdjacentCounties(){
          var currentVis = ncdmLayer.visibleLayers;
          var queryTask = new QueryTask(ncdmLayer.url+"/" +currentVis);
          var query = new Query();
          query.outFields=["NAME", "NAME2", "RATE"];
          query.where = clause;
          query.returnGeometry = false;
          queryTask.on('error', taskErrorHandler);
          queryTask.on('complete', queryAdjResultHandler);
          queryTask.execute(query);
        }
0 Kudos