Wednesday, 28 December 2011

Use ColorBrewer for coloring maps rendered with GeoTools

ColorBrewer is an excellent library for creating dynamic color palettes that can be used to apply colors to a map based on the spread of data values. The ColorBrewer web tool allows you to select a color scheme based on the nature of the data (sequential, diverging, qualitative) and  the number of data classes present in the data. You can opt to select a color scheme that is color blind safe and works well on a photocopier.
Based on your requirements, the web tool will display the name of the color scheme that works best for the entered combination. For example, in the screenshot below, the color scheme chosen by the tool is BuGn.

ColorBrewer 2.0

ColorBrewer comes bundled with GeoTools. To include the correct library,use the following Maven snippet.


To instantiate the required palette within your Java application,
   String paletteName = "BuGn";
  ColorBrewer brewer = null;
  try {
   brewer = ColorBrewer.instance(ColorBrewer.DIVERGING);
  } catch (IOException e) {
   //Log Error
                // Instantiate the color palette 
                 palette = brewer.getPalette(paletteName);
                // Get the number of colors your need. In this case, 3 colors
                // which are a variant of Green
Now, you have an array of colors which you can apply to your GIS features.

No comments: