How to get Legend Info from web map services

11370
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
JasonRainwater
New Contributor II

Ok gotcha, so there are urls that are used and populated that might not be visible by clicking around the webmap viewer.  I tried digging through there to find what endpoints are available but couldn't find any that surfaced a "map server"

0 Kudos
JordanBaumgardner
Occasional Contributor III

Yes, there are some key URL that everyone just kinda knows.

Try looking in YourServer\arcgis\rest\services   <- that should be the base url to your services.

YourServer:6080\arcgis\manager   

YourServer:6080\arcgis\admin

are also useful.

Rhys-Donoghue
New Contributor III

@jordan - Yes, you are correct, a webmap is a wrapper or collection of layers as JSON, however, a Portal/AGO webmap can also contain other types of layers not supported by (or originating) from ArcGIS Server (e.g. a WMTS layer).  A hosted feature layer does not have a corresponding map service, so navigating the REST Services Directory won't show the map service for a hosted feature layer.  See @Andrew's comment below.

0 Kudos
by Anonymous User
Not applicable

I'm coming to this late (and in the context of getting a legend from Esri services for a Leaflet map), but I think the discrepancy noted between the questioners and the responders is that the "legend" service URL pattern is only available for services coming from ArcGIS Server (Enterprise) instances, but NOT from ArcGIS Online services. It looks like several questioners were referring specifically to AGOL resources...

Also, not every "FeatureServer" hosted in AGOL (at arcgis.com) will have a corresponding "MapServer" REST endpoint, unlike what you see with a standard ArcGIS Server/Enterprise setup.

I'm really hoping that someone else can confirm if this assessment is correct, and that the "legend" service URL pattern is only available for standard ArcGIS Server REST endpoints (for both Feature or Map service layers) and NOT for ArcGIS Online services (of any service type).

If so, and if I still need to support legends for AGOL feature and map resources (not just for ArcGIS Server/Enterprise resources), how do I generate the legend image for use in a web map interface (perhaps using Leaflet, or some other framework)? We're not planning on using any Dojo/JSAPI components...

I the AGOL REST service endpoints, there is a Drawing Info Renderer JSON object with all the necessary ingredients (symbol type, colors, sizes, etc), but it would be nice not to have to roll my own legend generator for AGOL services...

Rhys-Donoghue
New Contributor III

Yes, Andrew is correct.  Feature services from ArcGIS Server web services are created by publishing map services and enabling Feature Access.  So if you are using ArcGIS Server, you can change "FeatureServer" in the REST endpoint for the feature service to "MapServer" and get the legend info, however, as Andrew correctly points out, if the feature service is hosted with ArcGIS Online or Portal for ArcGIS, it does not have a corresponding map service because the service was not created via ArcGIS Server - it was published from Portal/AGO.  To get the legend for the feature service if there is no map service, you need to use the drawingInfo - see here for an example of the drawingInfo object: http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/1?f=pjson. Also the drawingInfo.renderer does not match the renderer for Esri's JS API (4.8 at the time of writing this), so you will need to use jsonUtils | API Reference | ArcGIS API for JavaScript 4.8 to convert it to a renderer you can use with the JS API.  Once you have the drawingInfo, you can convert it to SVG format to render your custom legend - you can use this: symbolPreview | API Reference | ArcGIS API for JavaScript 4.8 .  You can test your legend first using Esri's legend widget: Legend | API Reference | ArcGIS API for JavaScript 4.8 - This will show you the legend works even if there is no corresponding map service for the feature service.

by Anonymous User
Not applicable

I _did_ eventually circle back to this and (with the hints from Rhys) got a working AGOL Feature Service legend image generator for use in a Leaflet map (which also leverages AngularJS). I can now generate legend images for using the drawingInfo.renderer object from the REST endpoint JSON response.

To do this, I had to first use the angular-esri-core library from here: https://github.com/Esri/angular-esri-map 

I actually only needed the smaller angular-esri-core.js component to add just the bits of Dojo/JSAPI that I needed (specifically, the esriLoader object) to generate legend images using the JSAPI 4.x symbolPreview object's renderPreviewHTML method. Our version of AngularJS was just under the "supported" line, but the "core" loader library seems to work fine with our older version.

Using the esriLoader object, I loaded these pieces of Dojo/JSAPI:

  • esri/symbols/support/symbolPreview
  • esri/symbols/support/jsonUtils
  • esri/renderers/support/jsonUtils

Once I had an SVG legend image for the drawingInfo.renderer, I also needed to do some minor surgery on the SVG/XML string, to add an "xmlns" tag to make it valid SVG for rendering as a source URL for an img tag.

Fun times!