I reckon that GeoServer layer label style has excellent feature of TextSymbolizer where text label can be positioned relatively on centroid of VISIBLE PART of polygon, as oppose to a geographically fixed position. The GeoServer website states that <sld:Graphic>
can be specified in a <sld:TextSymbolizer>
.
With this kind of placement i also need to visualise piechart. This is where i found difficulties.
Here is my SLD, within the <sld:TextSymbolizer>
there's a piechart <sld:ExternalGraphic>
and a <sld:Label>
.
<?xml version="1.0" encoding="UTF-8"?>
<sld:StyledLayerDescriptor 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"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.0.0">
<sld:NamedLayer>
<sld:Name>test</sld:Name>
<sld:UserStyle>
<sld:Name>test</sld:Name>
<sld:Title/>
<sld:FeatureTypeStyle>
<sld:Rule>
<sld:TextSymbolizer>
<sld:Graphic>
<sld:ExternalGraphic>
<sld:OnlineResource
xlink:href="http://chart?cht=p&chd=t:${100 * revenue_percentage},${100 * (1 - revenue_percentage)}&chf=bg,s,FFFFFF00" />
<sld:Format>application/chart</sld:Format>
</sld:ExternalGraphic>
<sld:Size>
<ogc:Add>
<ogc:Literal>20</ogc:Literal>
<ogc:Mul>
<ogc:Div>
<ogc:PropertyName>revenue_target</ogc:PropertyName>
<ogc:Literal>2e13</ogc:Literal>
</ogc:Div>
<ogc:Literal>60</ogc:Literal>
</ogc:Mul>
</ogc:Add>
</sld:Size>
</sld:Graphic>
<sld:Label>
<ogc:Literal>PIECHART GRAPHIC</ogc:Literal>
</sld:Label>
<sld:Font>
<sld:CssParameter name="font-family">SansSerif</sld:CssParameter>
<sld:CssParameter name="font-size">14</sld:CssParameter>
<sld:CssParameter name="font-style">normal</sld:CssParameter>
<sld:CssParameter name="font-weight">bold</sld:CssParameter>
</sld:Font>
<sld:LabelPlacement>
<sld:Fill>
<CssParameter name="fill-opacity">1</CssParameter>
</sld:Fill>
<sld:PointPlacement>
<sld:AnchorPoint>
<sld:AnchorPointX>0.5</sld:AnchorPointX>
<sld:AnchorPointY>0.5</sld:AnchorPointY>
</sld:AnchorPoint>
</sld:PointPlacement>
</sld:LabelPlacement>
</sld:TextSymbolizer>
</sld:Rule>
</sld:FeatureTypeStyle>
</sld:UserStyle>
</sld:NamedLayer>
</sld:StyledLayerDescriptor>
I found that:
- In order a graphic to show,
<sld:Label>
text must be present in a<sld:TextSymbolizer>
. And the<sld:Label>
must not be empty. If<sld:Label>
is not wanted then the only getaway is to hide it with<CssParameter name="fill-opacity">0</CssParameter>
- If
<sld:Label>
is not wanted it is good to put a shortest possible string in the<sld:Label><ogc:Literal>
(for example a point ".") to make sure that<sld:Graphic>
element of the same<sld:Rule>
are drawn. - With
<sld:Label>
in<sld:TextSymbolizer>
, GeoServer GUI complaints when i click the "Validate" button in Style Editor. The Style Editor says while i believe<Priority>
and<VendorOption>
are optional. However, the style can be submitted and can be use with a WMS Layer without error. - The piechart
<sld:ExternalGraphic>
never showed up in WMS, and not even an error message from GeoServer (this is observed using FireBug, the request is fine and nothing is wrong).By the way the expected piechart is below
- If I replace the piechart in
<sld:ExternalGraphic>
with a PNG image then the PNG Images are drawn nicely in WMS response, off course with<sld:Label>
in the<sld:TextSymbolizer>
. The expected placement works. The<sld:Size>
also works, depending on value of a field. Screenshot below is when i panned slightly to the west. - If I replace the
<sld:TextSymbolizer>
with<sld:PointSymbolizer>
, the piechart shows. Surely if i panned the map, the piechart stay at the same geographic location. Unfortunately the extent in the above images do not cover centroid of polygon, hence the below screenshot uses different extent. Here's the screenshot:
My questions:
- How to make the piechart
<sld:ExternalGraphic>
in<sld:TextSymbolizer>
drawn in WMS response? - Is this because
<sld:TextSymbolizer>
doesn't like piechart<sld:ExternalGraphic>
?
My environment:
- GeoServer 2.5 (+Chart Extension) on Apache Tomcat 6.0.35
- OpenLayers 2.12
- PostgreSQL 9.2.4-1 + PostGIS 2.0.3-2
- Windows Server 2003 32-bit