How to get Legend Info from web map services

11187
25
02-17-2017 06:59 AM
DavidZhong2
New Contributor II

I am creating a custom TOC which needs to have both layer toggle and legend item (image data) for the web map. But the web map service does not have Legend Info. Does anybody know how to get legend info from the web map services. Any help would be very appreciated.

25 Replies
ChrisSmith7
Frequent Contributor

If you have created a legend, it should be available right on the map service, something like:

https://raster.nationalmap.gov/arcgis/rest/services/SampleWorldCities/MapServer/legend?f=pjson 

You can also create the legend using something like:

Legend | ArcGIS API for JavaScript 3.19 

DavidZhong2
New Contributor II

Thanks Chris for the response. The service I have does not have legend Info. Please see following screenshot. I am creating a TOC having both layers toggles and legend image. I was able to create a one for the map service having the Legend Info. But for the ArcGIS Online Map, I couldn't find a way to get the Legend Info.

0 Kudos
JordanBaumgardner
Occasional Contributor III

You may find this useful once you get your JSON back from the legend url.  [Ripped off from the old 3.x\digit]

/// Key Line

/// src = "data:image/png;base64," +
this.rootLayerTOC._legendResponse.layers[this.rootLayer.layerId].legend[0].imageData;
///

_getLegendIconUrl: function(legend) {
 var src = legend.url;
 if (src == null) {
 src = "data:image/png;base64," +
 this.rootLayerTOC._legendResponse.layers[this.rootLayer.layerId].legend[0].imageData;
 } else if (src != null && src.indexOf('data') == -1) {
 // in some cases NULL value may cause #legend != #of renderer entry.
 if (!has('ie') && legend.imageData && legend.imageData.length > 0) {
 src = "data:image/png;base64," + legend.imageData;
 } else {
 if (src.indexOf('http') !== 0) {
 // resolve relative url
 src = this.rootLayer.url + '/' + this.serviceLayer.id + '/images/' + src;
 }
 if (this.rootLayer.credential && this.rootLayer.credential.token) {
 src = src + "?token=" + this.rootLayer.credential.token;
 } else if (esriConfig.defaults.io.alwaysUseProxy) {
 src = esriConfig.defaults.io.proxyUrl + "?" + src;
 }
 }
 }
 return src;
0 Kudos
DavidZhong2
New Contributor II

Thanks Jordan for the information. But the service I have is a WebMap service which does not have Legend Info JSON.  The following is the screenshot of the service. You can see it only has ALL Layers and Tables link, no Legend link

FC_Basson
MVP Regular Contributor

It's a Feature Service, that's why you are not seeing the legend option, but if you view the properties of each layer you should see the default render in the Drawing Info.  You can also see it in the JSON of each layer.

JasonRainwater
New Contributor II

Hi I work with david trying to figure this out.  For ArcGIS server maps we are querying the legend information and populating a custom widget with the image data that is returned.

We are looking for something similar for webmaps.  I know the renderer is there but how do you just dynamically create something that renders the renderer?  First attempts required knowledge and specific code for all possible renderer types.

Do you know of a way to write one set of code that does not require switches based on every possible renderer type?

0 Kudos
JordanBaumgardner
Occasional Contributor III

I think you simply need to RegEx your URL before sending, replacing FeatureServer with MapServer. Each Feature service will (must) have a matching MapServer.

But - to use your renderer... The 3.x util had a method that would return an SVG suitable for inserting into the browser dom. I was about to port when I realized I could hit the MapServer and get the same results, so I never pursued it. It ran through the JSON and created the image with js. Kinda brute force.

0 Kudos
JasonRainwater
New Contributor II

I should clarify that we are on the 4.x sdk currently on 4.2.  Do you know of anything in there that can help?

From what I understand a webmap (or portal map) doesn't have a "map server".  The webmap is like a wrapper around feature layers but it in itself is not a map server so I don't think we can get the same info as we do in a normal map server.

0 Kudos
JordanBaumgardner
Occasional Contributor III

Well, that's kinda the crux of the issue. They have not ported it yet. 

0 Kudos