Get properties of layers in a map service

2217
2
02-03-2016 12:01 PM
ChenLiang
New Contributor II

My app loads one of map services containing vector and raster layers. When identifying on a map, users expect to see query results from all visible vector layers in a data grid. In my case, all raster layers should be ignored because of no attribute data.

So when I looped through all visible layer IDs and run query tasks on each of them, I didn't see any API function or class gave info about the layer properties (such as Geometry Type or Fields, basically whatever returns from hitting a layer url in a browser). Such properties appear only with a query result. Surely a simple xhr request would return all layer info. But that's another series of another roundtrips to the server. Or maybe I missed something hidden deep in API docs.

I don't think my use case of identifying all layers is unique or peculiar. What's Esri's arugment for API not exposing or packing the complete layer properites when loading them into the Map instance? However, it already exposes some of properties such as Definition at the service level as an array, and Default Visibility and Max/Min Scale in individual LayerInfos. A bit strange, isn't it?

0 Kudos
2 Replies
TyroneBiggums
Occasional Contributor III

I've been working on the same thing. There is a similar thread in the ArcGIS API for JavaScript place, authored by me (yup, I am to lazy to search and link here at the moment).

map.getLayer(yourLayer).geometryType gives you your geometry type... is the problem that you have a service, not the layer id that map.getLayer expects? map.getLayer(yourLayer).renderer.symbol or map.getLayer(yourLayer).renderer._symbols gives you the symbol information you'd want.

You can get the fields/attributes from the identify task. It returns results, and you'd want thoseResults.feature.attributes.

KenBuja
MVP Esteemed Contributor

While this sample shows how to get information about the fields of a layer in a service, you can query the type of layer by adding response.type in this function

function requestSucceeded(response, io){
  var fieldInfo, pad;
  pad = dojoString.pad;

  //toJson converts the given JavaScript object
  //and its properties and values into simple text 
  dojoJson.toJsonIndentStr = "  ";
  console.log("response as text:\n", dojoJson.toJson(response,true));
  dom.byId("status").innerHTML = "";
  dom.byId("content").value = response.type;

  //show field names and aliases
  if ( response.hasOwnProperty("fields") ) {
    console.log("got some fields");
    fieldInfo = array.map(response.fields, function(f) {
      return pad("Field:", 8, " ", true) + pad(f.name, 25, " ", true) + 
        pad("Alias:", 8, " ", true) + pad(f.alias, 25, " ", true) + 
        pad("Type:", 8, " ", true) + pad(f.type, 25, " ", true);
    });
    dom.byId("content").value = response.type + "\n" + fieldInfo.join("\n");
  } else {
    dom.byId("content").value = response.type + "\n" + "No field info found. Please double-check the URL.";
  }

}