Friday, September 24, 2010

Dynamic Legend Generation From An Image Map Services

You can see the national weather warnings that NOAA publishes here. If you look at the page, you will notice that _all_ the legend swatches and text are presented under the map and you are left to your own means of locating the associated colors between the map and the legend grid to figure out what is on the map.
It would be nice if the legend grid could be dynamically generated to list only the swatch colors and their descriptions based on what is on the map. This is what this post tries to solve.
So NOAA publishes the national weather service maps as Esri ArcGIS REST endpoints. Using an instance of an ArcGISDynamicMapServiceLayer subclass, I can intercept the reponse image. Once I have the image bitmap data, I can get a histogram of the color distribution. Now using NOAA color map, I can associate a color with a description and populate a legend UI component. Check out the application in action here. The legend located on the top right corner is not fully transparent, roll the mouse over it to make it fully opaque. And like usual, you can download the source code from here.
Update: Looks like my usage and processing of the histogram does not lead me to a unique set of colors. So I reverted back to good old fashion looping through the pixels and creating a unique bucket of colors. Works pretty fast and more importantly correctly :-) A colleague at Esri was wondering if you can click on the map and popup an info window containing the label of the weather at that location, so I updated the code to handle this request - Thanks Mehul.

9 comments:

mapBaker said...

Any idea when this will be core API ie. the javascript API?

thunderhead said...

I know that in AGS 10 SP1 there is a new REST endpoint to generate legend swatches - not sure if it will be as "smart" as this thought - but could be :-)

Drew said...

Excellent app. Thank you

Susan Mordy said...

This is pretty amazing. One of the best legend capabilities I have seen for Flex. Do you know how I can incorporate your code with the ESRI flex viewer (v 1.3) and the live maps widget? I really like this legend I would love to get it to work in the flex viewer (1.3).

thunderhead said...

We have update the FlexViewer to 2.0 - in addition, AGS 10 SP1 will have a new REST endpoint to assist in legend generation - so....expect a post to take advantage of these new features.

Susan Mordy said...

Thanks for your comment..I am not sure what you mean when you say we have update the Flex Viewer to 2.0. Do you mean you have incorporated your dynamic legend code into the SFV 2.0?

thunderhead said...

Sorry for the confusion - what I meant to say - is that with the release of FV2 - no "effort" is expanded on FV1 - we _will_ be releasing a new legend widget when the new ags rest endpoint supports legend swatch generation - which will be very soon - so stay tuned.

Susan Mordy said...

Ok thanks. Good to know. I would like to upgrade to 2.0 but I understand that Querybuilder.swc is not available in 2.0. Our application uses the query builder widget available in 1.3 viewer. Thanks again.

thunderhead said...

u r not the first person to ask for this - subject of a good blog post