CSS in GeoServer – How’s it Work?
I make maps with computers. Or rather, I make tools for making maps with computers, tools such as GeoServer and GeoExt. I also write code recreationally from time to time. A while ago I wrote a GeoServer blog post about one side project of mine, a tool to translate from CSS-like syntax to SLD styles (CSS is the Cascading Style Sheet language used for styling things like the blog you’re looking at right now; SLD is the Styled Layer Descriptor language used in GeoServer and other mapping servers to style maps). While the post on the GeoServer blog was intended to help spread user awareness of this tool, I’d like to write a few posts on this blog about the implementation. Who knows, maybe I can get some collaborators?
The CSS module started almost a year ago when I was checking out the Scala programming language. I was interested in Scala because it’s Java-compatible, a key property for working with GeoServer and GeoTools. Scala provides excellent Java interoperability, including natural use of overloaded methods which are a rough spot in the other JVM languages I’ve looked at due to their lack of typed variables (for example, Jython, Rhino, and JRuby). Scala also provides lots of nice language features like function values, function literals, mixins (aka traits in Scala terminology), and pattern matching to name a few.
The best thing to do when learning a new programming language is to write some code in it, and after finishing HelloWorld.scala and a markov text generator, I started to think about a “real” project to work on. I had recently come across Cascadenik, so a CSS map styling tool for GeoServer seemed like a good idea (and for a bonus, a text parsing API is part of the Scala standard library!)
I’ll devote the next few posts on this blog to discussing the actual code in a bit more depth, but here’s the general strategy I took. Rather than reproducing all the work that’s gone into SLD-based rendering (including non-image maps like KML and SVG) in GeoTools, I decided to work on translating CSS files to SLD, using the normal GeoServer configuration and rendering system to actually generate maps. I also decided to try and emulate the cascading model that CSS uses during the translation process, so that styling rules could share properties in a more flexible way than in the painter’s model imposed by SLD. After parsing a CSS file and jumbling its rules around to adapt to the differing models, I produced a GeoTools Style object and could either pass the Style to the rendering system, or use GeoTools serialization methods to produce an XML document.
Part 1 – Parsing CSS in Scala