Color blending and compositing implemented for GeoServer 2.7
in this post we want to introduce a new mapping ability available in the GeoServer 2.7.x series: color blending and compositing.
The notion of color blending and compositing has been borrowed from the SVG specification, and it’s commonly available in image manipulation programs such as Gimp/Photoshop, and has been available, with different types of limitations, in open source GIS software MapServer, QGis and Mapnik.
GeoServer own implementation is sort of a sum of the above software abilities, in that it provides both Porter-Duff compositing, useful for masking, color blending, great to more naturally blend multiple colored levels, and base compositing layers, very important to get accurate control of how masking and blending are performed, especially in a WMS setting where the client can decide how layers are going to be stacked up.
As an example of masking via Porter-Duff compositing, let’s pick two images, the first is know as the destination, the second as the source (the source is painted on top of the destination):
The two images can be used to mask each other using destination-in and source-in masking:
Color blending is instead quite useful to naturally blend DEM colors with polygon fills above them, for example, given the following source and destination, the multiply color blend results in a nice looking blended map:
Now, both masking and blending results are quite sensitive to the destination being blended onto. Normally a layer will blend with the sum of the previously painted layers, making the result rather sensitive to how the WMS request issued by the client, who is in control of how layers are being stacked up.
To have better control one or more FeatureTypeStyle can be marked as a composite base, that is, a destination on wich the subsequent layers will be blended against, and that will be blended against the layers before it only as the last step in the map drawing.
Let’s see how this can be used to generate interesting map effects. We have the usual states map, and a natural earth raster layer. We would like to get a “line inside polygon” effect for the states, and then combine the result on top of the natural earth raster layer. The first step is easily achieved by creating a black thick stroke which will be used as the mask against the states layer:
Now, what happens if now we stack the natural raster layer, states, and the black mask? The states layer will be painted on top of the raster, and the result will be used as the destination for the masking, resulting in this:
Not exactly what we wanted… to make sure we get the right result, the topp:states layer need to be marked as a compositing-base, this will make sure the compositing happens between states and the mask, and only after that the result will be painted on top of the raster layer, resulting in the following:
You can look at the user guide for a full set of composite/blending examples, and the SLD for the above map.