[GIS] Label styling applied in Geoserver are not being applied to WMS layer in Openlayers

geoserveropenlayers-2sld

I am trying to label a layer of line vectors using SLD in Geoserver. It is simple enough, and it's pretty much copied from the SLD cookbook: http://docs.geoserver.org/latest/en/user/styling/sld-cookbook/lines.html

The thing that's getting me is that when I view the Openlayers preview in Geoserver, everything seems to render correctly. Each road is only labeled once and it's not extremely busy looking. However, when I import the layer via WMS to my Openlayers map, it doesn't seem to recognize this line:

<VendorOption name="repeat">400</VendorOption> 

Which is supposed to keep it from repeating within 400 pixels.

I've also tried:

<VendorOption name="group">yes</VendorOption> 

Which should only display a single label on a map per unique text.

Any idea what is going on here? I need the labels to look like the Geoserver preview.

Map displayed in openlayers after importing layer via WMS:
WMS Served Map

Map displayed in Geoserver Openlayers preview:
Openlayers preview

<?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>Roads</Name>
<UserStyle>
<FeatureTypeStyle>
<Rule>
<Title>Roads</Title>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">
<ogc:Literal>#000000</ogc:Literal>
</CssParameter>
<CssParameter name="stroke-width">
<ogc:Literal>1</ogc:Literal>
</CssParameter>
</Stroke>
</LineSymbolizer>
<TextSymbolizer>
<Label>
<ogc:PropertyName>name</ogc:PropertyName>
</Label>
<LabelPlacement>
<LinePlacement>
<PerpendicularOffset>
10
</PerpendicularOffset>
</LinePlacement>
</LabelPlacement>
<Fill>
<CssParameter name="fill">#000000</CssParameter>
</Fill>
<VendorOption name="repeat">400</VendorOption>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

Best Answer

This is probably an artifact due to tiling. The layer preview map defaults to a single tile, but I'm guessing your openlayers map uses tiling.

Geoserver insists on labeling every tile. GeoServer doesn't know that each request is for a tile in a larger map, so it treats a tile as a complete map and dutifully applies a label to it. So basically, if a road stretches across three tiles, it will be labelled three times.

Unfortunately, there's not much you can do about it. I was advised to enable meta-tiling. This does allow GeoServer to think of requests as tile components to some extent, and this can minimise the label repeating. However, I believe it's on by default in recent versions.