By David Crowther
Question:
How can I set styles in GeoServer based on a geometry Type?
Answer:
GeoServer uses Style Layer Descriptors (SLDs) to help you style your map features.
The Generic SLD Style file…..
… has RULES within it that allow you to style Raster, Polygon, Line and Point data differently.
However, this will style all POINT Data with one Style (e.g. a RED DOT), all LINE Data with one Style (e.g. Blue Line) and all POLYGON Data with one Style (e.g. Grey Polygon).
However, what if you wish to also use multiple Feature Styles for each different Geometry Type?
For example, you have Grounds Maintenance data, with a series of different FEATURE TYPES (Trees, Grass, Hard Surface, Shrubs)….. and those feature types are represented by multiple geometry types? e.g. a Tree can be represented by a Point (tree location), by a line (a stand of trees) and a polygon (an area of woodland).
Common Problems:
1 – If we Create a WMS that simply uses a POLYGON SMYBOLIZER and RULES for each FEATURE TYPE,.. we will only be rendering the GM features whose Geometry Type is Polygon… and many features which are Points and Lines will not be rendered.
2 – However, if we choose to add a POINT and LINE Symbolizer to each of the above FEATURE TYPE Rules,… this will show all Map features as we are showing Points, Lines and Polygons,.. however, the Point SYMBOLIZER will create extra Points on the map – as it also renders a point at the Centroid of Polygons and the Centroids of Lines! and the Line Symbolizer will create line features around the outside of all Polygons!
Options:
1 - To solve this - you could split your source data into separate GEOMETRY TYPE Tables and then simply create 3 SLD files – POINTS, LINES and POLYGONS – where you include RULES that are style based on the FEATURE TYPES.
2 - But if you don’t want to split your source data table… you can add 2 filters to your GeoServer SLD Rules. The first part will define the Geometry Type and the second will define the FEATURE Type… this is the approach we will use!
Here’s how to do it!
In the example below, the SLD text shown is filtering the Grounds Maintenance data where the GEOMETRY TYPE is POLYGON – which is geometry dimension of 2.
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:Function name="dimension">
<ogc:Function name="geometry"/>
</ogc:Function>
<ogc:Literal>2</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
If we update the SLD we can add in the AND option to introduce the second FILTER, using the FEATURE CATEGORY.
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsEqualTo>
<ogc:Function name="dimension">
<ogc:Function name="geometry"/>
</ogc:Function>
<ogc:Literal>2</ogc:Literal>
</ogc:PropertyIsEqualTo>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>FEATURE_CATEGORY</ogc:PropertyName>
<ogc:Literal>Flowers</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:And>
</ogc:Filter>
The above SLD, therefore Selects the GM features which:
- are POLYGONS,
- and where the Feature Category is Flowers.
By then repeating these RULES and changing the Feature Category Value, you can create an SLD that:
- ONLY shows the POLYGON Geometry
- and Styles those Polygon Features by the FEATURE CATEGORY value for all values
Repeat this for a POINT SLD – where the geometry dimension value is 0
and for a LINE SLD – where the geometry dimension value is 1
… you will now have 3 separate SLDs which you can apply to 3 different WMS Layers,… allowing your users to display all the FEATURE TYPES per Geometry Type.
Each Feature Type where geometry = Point:
Each Feature Type where geometry = Line:
Each Feature Type where geometry = Polygon:
Finally, by combining all the RULES into one SLD,.. you can have a WMS that shows all 3 geometry Types (Points, Lines and Polygons) styled by each individual Feature Type.
Comments (0 comments)