Developer’s Corner: Bridging CSS and SLD unit of measure treatment in GeoServer

Hi,
in this blog we are introducing some work with did to bridge the unit of measure treatment in SLD and CSS. The beginning of the blog is a bit technical, so if you want to just get the news, skip towards the map screenshots.

Unit of measure treatment in CSS is rather straightforward, every size is in pixel, unless otherwise specified by appending a unit of measure to it. In GeoServer geospatial CSS design there are three unit of measure, meter (m), feet (ft) and pixels (px), for example, say we want to have a circular mark, whose size is 2 meters, but whose outline is always 1 pixel, regardless of the zoom:

* {
  mark: symbol(“circle”);
  mark-size: 2m;
}

:mark {
  stroke: black;
  stroke-width: 1px;
  fill: red;
}

Generally speaking, every property is free to have a different unit of measure. That’s nice, too bad it does not work in the current CSS support, more on this later.

How does this compare with the SLD unit of measure model? Well, in SLD 1.0 there is no official support for unit of measure (although GeoServer support the same syntax as SLD 1.1 as an extension).
In SLD 1.1 the unit of measure can be specified only at the whole symbolizer level, with a twist: if one property inside that symbolizer needs to be expressed in pixels, then it’s valid to append the px suffix to it.
So the above CSS style could be roughly translated to:


  uom=”http://www.opengeospatial.org/se/units/metre”>
  MyPointSymbolizer
 
   
      circle
     
        #FF0000
     
     
        #000000
        1px
     
   
    2
 

Now, why can’t we do that? Because the pixel unit of measure override is not really supported in the current GeoTools/GeoServer code.
So we could have added it, and in case we had three properties to be expressed in meters, feet and pixels, theoretically the translation from CSS could turn feet into meters, set the global unit to meters, and keep the pixels one with the px suffix.
However, we decided not to go that way for a few reasons:
  • It makes the CSS translation un-necessarily complex
  • Being able to overload the current unit of measure requires a some code to make it just for pixels, and just a little more to make it available also for meters and feets
What we did instead, was to add support for m, ft and px in SLD/SE, so that every property can have its own local unit of measure just like in CSS: the global amount of effort is reduced, and the result is nicer and more symmetric than standard SE/SLD 1.1 (and also more readable, if only local unit of measure are used).
So, the CSS now translates to:
>
  MyPointSymbolizer
 
   
      circle
     
        #FF0000
     
     
        #000000
        1px
     
   
    2m
 

Here are some example outputs using the usual bugsites demo layer:

First map
Zooming in
And zooming in more!

Ok, so, long story short: it’s now possible to specify per property unit of measure in both CSS and SLD by adding the “m”, “ft” and “px” suffixes!

This new feature is now available in the development series nightly builds.

If you’d like to know more about what you could achieve with us, do not hesitate and get in touch with usIf you need professional services to get started with Open Source software make sure to have a look at our GeoSolutions Enterprise Services.


The GeoSolutions team,