4

I have to highlight district boundary and corresponding point. So I am trying to do like below code. But I don't know how to pass district polygon geometry to query.geometry.

var queryTask = new QueryTask(".../arcgisserver/rest/services/CRD/CRD2/MapServer/0");

var query = new Query();

query.geometry = event.mapPoint; //your district polygon geometry    
query.outFields = ["*"];    
query.returnGeometry = true;

//Execute task    
queryTask.execute(query, showResults);

function showResults (results) {

    var resultItems = [];
    var resultCount = results.features.length;

    for (var i = 0; i < resultCount; i++) {
        var featureAttributes = results.features[i].attributes;
    }

}

Is it possible to pass polygon geometry into query task. If so how can do it.

2 Answers 2

3

Yes you can passe a Polygon Geometry , in your case I think , when you are clicking in the map you want first to get the District polygon , and after you'll perform the query on the CRD/CRD2/MapServer/0 by passing the distric poly if i've understand !

it's quite easy , I dont know if you're creating your own widget or a you write a legacy code,

So , create first a FeatureLayer(*1) for your distrcit Layer : it has a queryfeatures method that helps to get geometry of the clicked point ( if it has ) , declare it as class varibale if possible , that can be accessible in the class functions

var districtFLayer = new FeatureLayer("URL_TO_YOUR_DITRICT_MAPSERVER_LAYER");
//".../arcgisserver/rest/services/District/MapServer/1" by exemple

then create a methode that return the geometry District by seraching by District Name Text

(here by exemple I'm using the attribute search Name for the district layer the query would be

query.where ="Name ='" + dsitrictName+"'";)learn more about query.where

//AMD declaration Style 
makeQuery: function(dsitrictName){
   var query = new Query();
   query.where ="Name ='" + dsitrictName+"'";
   //query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;  //no for this if it's not query with geometry 
   // point that intersect with the District polygon  spatial relation ship
   query.returnGeometry = true;
   query.outFields = ["*"]; // if you want all attribute being returned
   var geometry = null;
   districtFLayer.queryFeatures(query, function (featureSet){
       //check existance of a Distric at that point
       if(featureSet.features.length>0)
           geometry = featureSet.features[0].geometry;
   });
   return geometry;
}

// leagacy declaration
function makeQuery(mapPoint){
   var query = new Query();
   query.where ="Name ='" + dsitrictName+"'"; // name of the district
   //query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS; //no for this if it's not query with geometry 
   // point that intersect with the District polygon  spatial relation ship
   query.returnGeometry = true;
   query.outFields = ["*"]; // if you want all attribute being returned
   var geometry = null;
   districtFLayer.queryFeatures(query, function (featureSet){
       //check existance of a Distric at that point
       if(featureSet.features.length>0)
           geometry = featureSet.features[0].geometry;
   });
   return geometry;
}

After in your above code use the features-query-complete event to ensure that the request (ajax ) was done and there is a returned geometry :

...
var pointClick = event.mapPoint;
var geometry = makeQuery(pointClick ); // this.makeQuery(pointClick ) in AMD 
districtFLayer.on("features-query-complete",function(result){

    if(geometry != null){ // if there is a returned geometry
    // **** HERE your above code ***** //
       var queryTask = new QueryTask(".../arcgisserver/rest/services/CRD/CRD2/MapServer/0");
       var query = new Query();
       query.geometry = geometry ; //the returned district polygon geometry    
       query.outFields = ["*"];    
       query.returnGeometry = true;

       //Execute task    
       queryTask.execute(query, showResults);
       .... // rest of code 
    }
});

Info: if you want to highlight the returned geometries use Graphics and add them to a GraphicLayer

(*1) module "esri/layers/FeatureLayer"

8
  • If you provide the whole class code , I can help you to highliight the results Feb 2, 2016 at 9:51
  • exactly same thing I wanted to do but values will come from drop down. that thing I am getting Feb 2, 2016 at 10:22
  • You mean that event.mapPoint come form DropDown menu ? if so, is it a point ? Feb 2, 2016 at 10:24
  • no its district name so its polygon Feb 2, 2016 at 10:34
  • so event.mapPoint is a text or a geometry ! if you can share the code , i can't figure now your probleme ! Feb 2, 2016 at 10:39
2

you can pass any valid geometry object (including esri/geometry/Polygon) to query.geometry as soon as you have a reference to it.

1
  • i need to do two queries one to get district boundary extent and that extent i need to pass in above code Feb 2, 2016 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.