Stay Connected with the Boundless Blog

CSS for GeoServer Styling – An Alternative to SLD

OpenGeo is excited to share the GeoServer CSS module, an alternative to SLD for thematic map styling in the OpenGeo Suite. Compared to SLD, GeoServer CSS brings many advantages for web cartographers.

GeoServer CSS is customized for use in GeoServer and provides access to spatial utilities such as powerful filter expressions that are useful not only for choosing which features to render but also for transforming geometries and other attributes on-the-fly during the rendering process. Thanks to its custom syntax (which builds on OGC CQL the same way that SLD builds on OGC Filter Encoding XML) it is much more compact, allowing authors to see more of the style without scrolling and to make edits without worrying about details such as closing nested XML tags in the proper order. GeoServer CSS is also simpler because it eschews some non-styling aspects of SLD such as embedded geometries.

As an example, let’s say we are styling polygons representing the habitats of several bird species. We would like to distinguish species by color (orioles have an orange area, bluebirds have a blue one, etc.), but all polygons should have a consistent opacity to help us identify places where habitats overlap. A style in CSS notation might look like this:

[species="bluebird"] {
    fill: blue;
    fill-opacity: 70%;
}

[species="oriole"] {
    fill: orange;
    fill-opacity: 70%;
}

[species="robin"] {
    fill: brown;
    fill-opacity: 70%;
}

Whereas in SLD, we might write the following much more verbose style:

<?xml version="1.0" encoding="UTF-8"?>
<sld:UserStyle xmlns="http://www.opengis.net/sld" xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">
    <sld:FeatureTypeStyle>
        <sld:Name>name</sld:Name>
        <sld:Rule>
            <ogc:Filter>
                <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>species</ogc:PropertyName>
                    <ogc:PropertyName>oriole</ogc:PropertyName>
                </ogc:PropertyIsEqualTo>
            </ogc:Filter>
            <sld:PolygonSymbolizer>
                <sld:Fill>
                    <sld:CssParameter name="fill">#ffa500</sld:CssParameter>
                    <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                </sld:Fill>
            </sld:PolygonSymbolizer>
        </sld:Rule>
        <sld:Rule>
            <ogc:Filter>
                <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>species</ogc:PropertyName>
                    <ogc:PropertyName>robin</ogc:PropertyName>
                </ogc:PropertyIsEqualTo>
            </ogc:Filter>
            <sld:PolygonSymbolizer>
                <sld:Fill>
                    <sld:CssParameter name="fill">#ff000</sld:CssParameter>
                    <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                </sld:Fill>
            </sld:PolygonSymbolizer>
        </sld:Rule>
        <sld:Rule>
            <ogc:Filter>
                <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>species</ogc:PropertyName>
                    <ogc:PropertyName>bluebird</ogc:PropertyName>
                </ogc:PropertyIsEqualTo>
            </ogc:Filter>
            <sld:PolygonSymbolizer>
                <sld:Fill>
                    <sld:CssParameter name="fill">#0000ff</sld:CssParameter>
                    <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                </sld:Fill>
            </sld:PolygonSymbolizer>
        </sld:Rule>
    </sld:FeatureTypeStyle>
</sld:UserStyle>

Where SLD would require all style properties to be listed for each rule in the style, the “cascading” nature of CSS allows styles to specify common attributes in one place. This allows styles to be written in a less repetitive way, reducing the amount of work needed to create styles and making it easier to ensure consistency between multiple rules. The repeated property “fill-opacity: 70%” in the example above presents a problem when updating the style—it’s extra work for a web cartographer to ensure that the fill is applied consistently across all styles. While it doesn’t save us much typing (although compared to SLD we are already way ahead!) we can change opacities across the entire style by editing in just one place. For example, we could “factor out” the opacity to a catch-all rule:

* {
    fill-opacity: 70%;
}

[species="bluebird"] {
    fill: blue;
}

[species="oriole"] {
    fill: orange;
}

[species="robin"] {
    fill: brown;
}

GeoServer CSS is inspired by the web—and by great tools like CartoCascadenik, and MapCSS. Like these tools it provides a concise styling syntax modeled on CSS but differs in that it’s goal is to improve the experience relative to SLD while preserving CSS concepts like cascading. While we ultimately aspire to a common CSS-like styling language for web cartography, we hope that GeoServer CSS is much easier than SLD and will be familiar enough for web designers to use.

We’re excited about GeoServer CSS and think it makes building maps easier, We’d like you to try it out and tell us what you think. If it’s well-received, we may include it in a future release of the OpenGeo Suite. We encourage you to download and install the plugin, and if you have any questions, be sure to read the documentation or look for dwins in the #geoserver IRC channel on Freenode.

Boundless Geo

Boundless has over 14 years of experience in the open GIS technology industry; serving as the leading open mapping software and solutions provider to government and commercial sector clients. As an active member of the open source community, including OSGeo and LocationTech, Boundless contributes to over 140 projects repositories. Boundless resides in the USA and has over 80 employees spread across the globe.