I am trying to build my first HTML legend with MapServer, but I can't find any full example.

As I understand:

mylayer.map (just after the classes END):

    TEMPLATE "../path/mylegend.html"  ### HTML template file




[leg_class_html opt_flag=15]
   <img src=[leg_icon]> [leg_class_name]<br>
  • There seems to be a HTML legend demo at demo.mapserver.org/itasca_legend, maybe it helps.
    – user30184
    Commented May 16, 2022 at 19:14
  • Thank you, it is hepful! I am not sure where the legend code should go? In the mylegend.html file or an adjacent mylegend.map file ?
    – marieneige
    Commented May 16, 2022 at 19:32
  • Yes, I had created and updated the HTML Legend demo earlier today for you (and replied to you on the -users list, but didn't realize that you posted this question in multiple locations), hope it helps! Let me know if it needs enhancements. Thanks again! (you can find all of the demos listed at demo.mapserver.org ) Thanks for mapserv-ing! -jeff
    – mapserving
    Commented May 16, 2022 at 19:53

1 Answer 1


I created and maintain the official HTML Legend demo for MapServer, and you can find all of the examples at https://demo.mapserver.org/itasca_legend/ (I had updated it all this morning, when I responded to your question on the mapserver-users list)

I should also mention that the full HTML Legend HowTo document lives at: https://mapserver.org/output/html_legend.html

At the bottom of each example page in the HTML Legend demo (https://demo.mapserver.org/itasca_legend/), you will see the template code, which is the contents of the TEMPLATE file referenced in the LEGEND section of the mapfile.

In your case, mylegend.html would contain only the following text:

[leg_class_html opt_flag=15]
   <img src=[leg_icon]> [leg_class_name]<br>

And your mylayer.map would point to that template, within the LEGEND object, such as:

# LEGEND object
  KEYSIZE 18 12
  # LABEL object
    COLOR 0 0 89
  TEMPLATE "mylegend.html"  ### HTML template file

(you don't need a file named mylegend.map)

You can actually test this by using MapServer's legend commandline utility (or legend.exe if you are using MS4W/Windows). Call the command to generate your legend image, such as:

setenv.bat (for MS4W users)
legend.exe mylayer.map mylegend.png

enter image description here

The full working mapfile and HTML legend files for this demo will soon be included for all MS4W installations, as well (I'm working on the big MS4W 5.0 release, so this makes sense to include).

  • Thank you very much, it is very helpful!
    – marieneige
    Commented May 17, 2022 at 20:31
  • I tried and nothing works for me. I only have a white rectangle with size 35px, 5px. Do other templates have to exist in the *.map file besides the one in the legend?
    – Przemek
    Commented Feb 3, 2023 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.