Developer corner: taming a massive style and get a performance speedup in the process
recently a customer contacted us with an interesting problem: how do I display a large dataset fast, with the requirement that we want every feature with a different color (unique color approach)?
I cannot show you the original dataset, but let’s consider Natural Earth level 1 administrative subdivision: all provinces in the world. And let’s say we want to give each of them a unique color, considering the are 3791 of them.
The common SLD approach would make for a Rule with filter, and symbolizer, for every single one of them. Assuming 15 lines of SLD per rule, that would make for a SLD with roughly 57000 lines of XML. Ugh! So large that nothing will allow the upload of it… double ugh!
As icing on the cake, for each feature on average 1900 rules would have to be evaluated in order to find the right color. Are you ready to way minutes for you map to be drawn?
Enter SE 1.1 and the recode function. Recode allows a compact representation of a style in which one attribute value is to be associated to a particular color, or width, in a very compact form.
Here is a portion of the SLD for the example at hand:
The full SLD can be downloaded here. It’s around 7600 lines, still quite big, but remember it condenses in a compact form over 3700 rules.
Now, the function surely makes the SLD smaller, but was still quite slow. So we spent some time optimizing it for the common case where all the values and colors are static values, getting a tremendous speedup.
How fast is it now? Brace yourself: by the naked eye, it’s as fast as the plain style that assigns the same color to all the polygons, the style that has just a single rule.
A larger version of the same map is available here. This large version took half a second to generate.
We hope that similar optimizations can be applied, in the future, to other SE useful functions, such as Categorize and Interpolate. Interested in the topic? Let us know!
the GeoSolutions Team.