I'm experimenting with choropleth maps using geotools.

Is there an easy way to add a color scale to the map (using geotools itself or some other os library)? By a color scale I mean a bar that shows how the feature attribute values map to color, with labeled ticks either automatically generated, or at break points of my choosing.

Ideally, I'd like to generate a separate color scale that could be rendered independently of the choropleth map, so that I can use a single color with layouts containing multiple maps.

I'm not absolutely wedded to geotools, and would consider other open source libraries, though I'd much prefer to stick with JVM languages (Clojure, Groovy, Java, ...).

  • can you clarify if you need help with styling the map or with just drawing the legend for a map you have already styled? – Ian Turton Apr 6 '12 at 8:32
  • drawing the legend – John McDonald Apr 6 '12 at 20:45
  • Did you find your way out in visualizing the legend for the choropleth map? – pradeepburla Aug 31 '16 at 10:35

Assuming that you know what colours you want and how many classes there are, (i.e. you can parse the SLD file) this is a simple case of looping through the list of colours and rule titles and drawing a small square of the right colour and printing the title next to it on a BufferedImage or suchlike. So something like the following could work:

BufferImage img = new ....;
Graphics2D g = img.getGraphics2D();
for(int i=0;i<nClasses;i++){

If you want to do anything more complex then have a look at how GeoServer does it in BufferedImageLegendGraphicBuilder

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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