Geometry transformations in SLD

SLD symbolizers may contain an optional <Geometry> element, which allows specifying which geometry attribute is to be rendered. In the common case of a featuretype with a single geometry attribute this element is usually omitted, but it is useful when a featuretype has multiple geometry-valued attributes.

SLD 1.0 requires the <Geometry> content to be a <ogc:PropertyName>. GeoServer extends this to allow a general SLD expression to be used. The expression can contain filter functions that manipulate geometries by transforming them into something different. This facility is called SLD geometry transformations.

GeoServer provides a number of filter functions that can transform geometry. A full list is available in the Filter Function Reference. They can be used to do things such as extracting line vertices or endpoints, offsetting polygons, or buffering geometries.

Geometry transformations are computed in the geometry’s original coordinate reference system, before any reprojection and rescaling to the output map is performed. For this reason, transformation parameters must be expressed in the units of the geometry CRS. This must be taken into account when using geometry transformations at different screen scales, since the parameters will not change with scale.

Examples

Let’s look at some examples.

Extracting vertices

Here is an example that allows one to extract all the vertices of a geometry, and make them visible in a map, using the vertices function:

 1    <PointSymbolizer>
 2      <Geometry>
 3        <ogc:Function name="vertices">
 4           <ogc:PropertyName>the_geom</ogc:PropertyName>
 5        </ogc:Function>
 6      </Geometry>
 7      <Graphic>
 8        <Mark>
 9          <WellKnownName>square</WellKnownName>
10          <Fill>
11            <CssParameter name="fill">#FF0000</CssParameter>
12          </Fill>
13        </Mark>
14        <Size>6</Size>
15      </Graphic>
16   </PointSymbolizer>

View the full "Vertices" SLD

Applied to the sample tasmania_roads layer this will result in:

../../../_images/vertices.png

Extracting and showing the vertices out of a geometry

Start and end point

The startPoint and endPoint functions can be used to extract the start and end point of a line.

 1 <PointSymbolizer>
 2   <Geometry>
 3     <ogc:Function name="startPoint">
 4       <ogc:PropertyName>the_geom</ogc:PropertyName>
 5     </ogc:Function>
 6   </Geometry>
 7   <Graphic>
 8     <Mark>
 9       <WellKnownName>square</WellKnownName>
10       <Stroke>
11         <CssParameter name="stroke">0x00FF00</CssParameter>
12         <CssParameter name="stroke-width">1.5</CssParameter>
13       </Stroke>
14     </Mark>
15     <Size>8</Size>
16   </Graphic>
17  </PointSymbolizer>
18  <PointSymbolizer>
19    <Geometry>
20      <ogc:Function name="endPoint">
21        <ogc:PropertyName>the_geom</ogc:PropertyName>
22      </ogc:Function>
23    </Geometry>
24    <Graphic>
25      <Mark>
26        <WellKnownName>circle</WellKnownName>
27        <Fill>
28           <CssParameter name="fill">0xFF0000</CssParameter>
29        </Fill>
30      </Mark>
31      <Size>4</Size>
32    </Graphic>
33  </PointSymbolizer>

View the full "StartEnd" SLD

Applied to the sample tasmania_roads layer this will result in:

../../../_images/startend.png

Extracting start and end point of a line

Drop shadow

The offset function can be used to create drop shadow effects below polygons. Notice that the offset values reflect the fact that the data used in the example is in a geographic coordinate system.

 1   <PolygonSymbolizer>
 2     <Geometry>
 3        <ogc:Function name="offset">
 4           <ogc:PropertyName>the_geom</ogc:PropertyName>
 5           <ogc:Literal>0.00004</ogc:Literal>
 6           <ogc:Literal>-0.00004</ogc:Literal>
 7        </ogc:Function>
 8     </Geometry>
 9     <Fill>
10       <CssParameter name="fill">#555555</CssParameter>
11     </Fill>
12   </PolygonSymbolizer>

View the full "Shadow" SLD

Applied to the sample tasmania_roads layer this will result in:

../../../_images/shadow.png

Dropping building shadows

Performance tips

GeoServer’s filter functions contain a number of set-related or constructive geometric functions, such as buffer, intersection, difference and others. These can be used as geometry transformations, but they be can quite heavy in terms of CPU consumption so it is advisable to use them with care. One strategy is to activate them only at higher zoom levels, so that fewer features are processed.

Buffering can often be visually approximated by using very large strokes together with round line joins and line caps. This avoids incurring the performance cost of a true geometric buffer transformation.

Adding new transformations

Additional filter functions can be developed in Java and then deployed in a JAR file as a GeoServer plugin. A guide is not available at this time, but see the GeoTools main module for examples.