Styling GeoServer Layers with CSS

GeoServer users have a lot to wrap their heads around. We need to optimize our servlet containers, determine projections for all those broken shapefiles, and remember to fill out layer metadata. One issue in particular that comes up again and again is difficulty with creating SLD files to style maps. It is hardly surprising that map designers fail to take advantage of some of the niftier rendering tricks that Andrea cooks up. Styler, a graphical SLD editing application that OpenGeo has been developing, is one approach to making SLD creation more palatable. But just as experienced web designers often feel limited by WYSIWYG environments, advanced users will always have a use for manual, text-based editing. Unfortunately, the highly structured, verbose nature of XML can make this strenuous.

With this in mind, I’ve been working on an extension to GeoServer allowing styling of map layers with a CSS-type syntax. This work follows in the footsteps of tools like Cartagen and Cascadenik which both apply a CSS framework to map styling.

To see how a CSS map style would look, compare the “simpleRoads” SLD from the data directory that ships with GeoServer:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0"
  xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
  xmlns="http://www.opengis.net/sld"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NamedLayer>
    <Name>Simple Roads</Name>
    <UserStyle>
      <Title>Default Styler for simple road segments</Title>
      <Abstract>Light red line, 2px wide</Abstract>
      <FeatureTypeStyle>
        <Rule>
          <Title>Roads</Title>
          <LineSymbolizer>
            <Stroke>
              <CssParameter name="stroke">
                <ogc:Literal>#AA3333</ogc:Literal>
              </CssParameter>
              <CssParameter name="stroke-width">
                <ogc:Literal>2</ogc:Literal>
              </CssParameter>
            </Stroke>
          </LineSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

with the equivalent CSS style:

/* @title Default styler for simple road segments
 * @abstract Light red line, 2px wide
 */
* {
  stroke: #AA3333;
  stroke-width: 2px;
}

CSS styles are fully converted to SLD before GeoServer uses them for rendering, so the resulting SLDs can be migrated directly to other GeoServer instances, even if they don’t have the CSS extension installed. Filter functions, zoom-based styling rules, and geometry transformations are also available.

You can download the CSS extension from the nightly build server and add it to your existing GeoServer instance (2.0.0 and later only). Installation instructions are available in the CSS tutorial. All the features of the CSS extension are documented in the CSS section of the User Manual. I encourage everyone to check it out!

One Comment

  1. Posted April 9, 2010 at 12:51 pm | Permalink

    Hi – GSS (Cartagen) was designed to be compliant JSON so that it could be parsed by any JSON library instead of needing a custom parser. This made it harder to respect ‘css-like’ selectors, and limited the syntax of selectors in general, since JSON doesn’t allow for many extra characters, unless you enclose in quotations, like:

    “#park .path { lineWidth: 4 }”

    anyways I’d love to converge a bit on stylesheet selectors.

Download GeoServer