How to return a symbol from a queryTask

1551
8
05-09-2012 07:34 PM
StephenLead
Regular Contributor III
I'm trying to obtain the symbol which applies to a result returned by a queryTask. Is this possible?

A query allows you to specify the fields to be returned, and whether the geometry should be returned, but it doesn't seem to return the symbol associated with a feature.

The result of the queryTask is a featureSet, comprising the features which satisfy the query. Although feature.symbol is available, I'm finding that it's blank.

To illustrate, put a breakpoint on line 36 of this Query sample and note that results.features[0].symbol is null, even though the source layer does have a renderer.

A workaround is presumably to obtain the renderer associated with the query.URL and build up the symbol this way - any better ideas?

Thanks,
Steve
0 Kudos
8 Replies
ShreyasVakil
Occasional Contributor II
What i think is, you cannot get symbology in QueryTask from the server. If you want symbology, you may want to use the FeatureLayer. The FeatureLayer will, by default, use the drawing information (symbology and domains) from the server (requires ArcGIS Server 10 or above)FeatureLayer.

If you want to apply symbol to the features you get from QueryTask refer this document.

It has all the steps to apply symbols to the Features.
[INDENT]   1. Initialize a Map and add a Layer.
   2. Initialize a QueryTask layer. Note that the URL includes the layer ID. In this example, the layer ID is "0".
   3.Initialize the Query. You should specify everything except the actual filter. In this example, a list of output fields is included in outFields. When you specify the output fields, you should limit the fields only to those you expect to use in the query or the results. The fewer fields you include, the faster the response will be. You must use the actual field names rather than the alias names, but you can use the alias names later when you display the results.
   4. Initialize InfoTemplate. If you plan to use an InfoWindow to display the results, this is a good time to construct the InfoTemplate. The syntax ${fieldName} is substituted at runtime with the actual attribute value. The first value is the title. The rest of the values make up the text of the InfoWindow. You can use any valid HTML.
   5. Initialize the Symbol you want to use for displaying the highlighted features on the map.
[/INDENT]


Initialize Map, Layer and QueryTask :

function init() {
    //create map and add layer
    map = new esri.Map("mapDiv");
    var layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");
    map.addLayer(layer);

    //initialize query task
    queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");

    //initialize query
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["CITY_NAME", "STATE_NAME", "POP1990"];

    //initialize InfoTemplate
    infoTemplate = new esri.InfoTemplate("${CITY_NAME}", "Name : ${CITY_NAME}<br/> State : ${STATE_NAME}<br />Population : ${POP1990}");

    //create symbol for selected features
    symbol = new esri.symbol.SimpleMarkerSymbol();
    symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
    symbol.setSize(10);
    symbol.setColor(new dojo.Color([255,255,0,0.5]));

  }


Create a function to show the results:
function showResults(featureSet) {
    //remove all graphics on the maps graphics layer
    map.graphics.clear();

    //Performance enhancer - assign featureSet array to a single variable.
    var resultFeatures = featureSet.features;

    //Loop through each feature returned
    for (var i=0, il=resultFeatures.length; i<il; i++) {
      //Get the current feature from the featureSet.
      //Feature is a graphic
      var graphic = resultFeatures;
      graphic.setSymbol(symbol);

      //Set the infoTemplate.
      graphic.setInfoTemplate(infoTemplate);

      //Add graphic to the map graphics layer.
      map.graphics.add(graphic);
    }
  }
0 Kudos
StephenLead
Regular Contributor III
What i think is, you cannot get symbology in QueryTask from the server.


Thanks Shreyas, I think you're right.

If you want symbology, you may want to use the FeatureLayer. The FeatureLayer will, by default, use the drawing information (symbology and domains) from the server


I'd normally do this, but in this particular case I want to obtain the features which would have been returned by the feature layer, without actually drawing them - hence the use of a query task based on the extent of the map.

5. Initialize the Symbol you want to use for displaying the highlighted features on the map.


I'm trying to do this automatically based on the current queryTask parameters, since my map is dynamic and I won't know exactly which symbol(s) to use at run-time.

I'll keep playing around with it and will report back if I come up with a workaround.

Thanks,
Steve
0 Kudos
ShreyasVakil
Occasional Contributor II
Sure, no problem Steve !

If you get a workaround, please try to post here.

Thanks.
0 Kudos
AdrianMarsden
Regular Contributor II

OK - old topic, but doe the same apply for the idtask ?  Symbolgy isn't returned, just the geometry?  has anything changed over the years?

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Adrain,

  No nothing has changed. The symbol is not something that is returned from the Identify task. You will have to make a esriRequest call to the service to get the renderer json info and in code create the renderer from that json and then use the getSymbol method on the renderer to get the symbol that that specific graphic should have or apply the renderer to the layer/graphics you create from the IdentTask results.

0 Kudos
AdrianMarsden
Regular Contributor II

Cheers - one for a day when I've not much on.  My ID task already has the ability to take the geometry and create a simply highlighted feature to help the users pick out a feature where there are scores overlapping, but we want that added graphic to have similar styling to the layer, so by switching the layer off you get left with just the single added graphic for the required feature.  Mainly for printout reasons

0 Kudos
JohnGrayson
Esri Regular Contributor

Check out getSymbol (3.x) and getDisplayedSymbol (4.x) as they might provide a way to use the renderer of the source layer and the graphic from the returned query/identify to retrieve a specific symbol.  In 4.x I normally create a FeatureLayer (using client-side graphics) with the same render as the source layer and then add the query/identify results to it.  Critical to both of these approaches is that the returned graphic must have all the necessary attributes and values so the renderer can figure out the correct symbol to use or return.

AdrianMarsden
Regular Contributor II

Thanks for that - in the end I went about it totally differently.

I have all the results of the ID task appear in a table - one table per layer - just like they used to in ArcIMS

At the end of each row (feature) I now have a button - this button takes the OBJECTID of that feature and the feature number and applies a filter to that layer. So the result is exactly what the users want - in those areas where there a 100s of features, they can ID the one they want, hit the button, and only that one is left - they then can print out just that one feature, without the others messing with the map.

Another button clears all these Isolations

The only pre-req is the existence of OBJECTID - which isn't a given - so much of our data are from other Esrti sites, that when they are imported the old OBJECTID is ignored and a new OBJECTID_1 is set up.  But that can be resolved.

0 Kudos