Creating a Map Legend
This page is in early editing stage only ... |
Collection of discussions, tools and documents on how to generate map legends / keys
Discussions
- 2013 discussion on the osm-dev list
- 2011 discussion in the Mapnik Wiki
- https://github.com/gravitystorm/openstreetmap-carto/issues/1928
Tools
Unfortunately none of these tools seem to be actively maintained
Wishlist for a legend tool:
- able to process XML styles with mininal modifications
- generate both HTML and printable formats
- can be defined by simple spec file
- optionally: exclude legend entries for objects not present in given map area at all
- optionally: in addition to simple points, lines, rectangles: allow simple OSM XML "minimaps" for visualization of objects in context
RenderLegend
Last changed: 10 years ago
Language: Python 2 (but can easily be converted to Python 3)
Takes a legend specification file, produces small OSM XML format snippet files, and tries to render these using the original Mapnik style file.
As styles usually expect to read data from a PostGIS database though, RenderLegend tries to rewrite SQL queries in the original style into Mapnik filter expressions.
While this may have worked out ok ten years ago, its SQL "parser" does fail on most current styles, e.g. not being able to understand SQL UNIONs.
If this tool was to be reviced, it should probably rather store legend data to render in a dummy PostGIS instance, and render using the unmodified original style, instead of generating OSM XML, and trying to rewrite styles for that.
The general idea of its spec file is to organize a legend in pages and lines, with multiple elements per line. For each element only the legend object type and OSM tag values are given in the element definition, which <style> to use for rendering each element is fully determined by the given Mapnik style only.
Possible legend element types are: point, line, square, rectangle, pointtext, linetext, lineshield, squaretext, rectangletext, squarepoint, smallline
Hartmut Holzgraefe is currently working on a re-implementation, using a dummy PostGIS instance instead of OSM files for Mapnik input, to avoid most of the style file rewriting, see https://github.com/hholzgra/render-legend-nt |
mtbmap make_legend.py
Mapnik-Legendary
Last Change: 5 years ago
Language: Ruby
This also uses a legend spec file, and an actual Mapnik style, to create a legend, somewhat similar to what RenderLegend is doing.
It depends on Ruby-Mapnik, which has been unmaintained for five years already, too.
Hartmut Holzgraefe failed to even build Ruby-Mapnik to begin with, or to find a binary package of it, so I don't even know yet how well it might work to begin with ...
User:Nakaner is currently working on a re-implementation using Python instead of Ruby, see: https://github.com/Nakaner/mapnik-legendary-py |
render_legend() method in the Mapnik Python Bindings
TopOSM legend.py
had a legend.py script that's no longer used with the current site though, and apparently went missing in action ...
Active implementations
- OpenStreetMap Americana demonstrates how to build a legend dynamically based on currently visible features in Mapbox GL JS or MapLibre GL JS. [1]