I have set up GeoServer with PostgreSQL/PostGIS as database.
Imported a country boundary shapefile into the database and published as WMS.
The data has got the Country name attribute in the "label" column.
I setup a map client using Openlayers, and it shows so many labels when zoomed in, one for each tile.
Can I publish the wms map as a Single Image, (without multiple image tiles) to avoid duplicate country name labels?
Or is there any other successful methods to do this in SLD?
I have read many answers and tried vendor options to correct this, nothing worked.
Some similar questions were seen as removed as duplicates, but sincerely, I got no solution for this duplicate label issue in GeoServer.
More Info:
I am using GeoServer 2.7.0
Using openlayers javascript from :
And the Openlayers code is:
<script type="text/javascript"> var map; function init() { map = new OpenLayers.Map({ div: "map", projection: new OpenLayers.Projection("EPSG:900913"), controls: [], numZoomLevels: 8, maxZoomLevel: 16 }); var osm = new OpenLayers.Layer.OSM("Open Street"); var gmap = new OpenLayers.Layer.Google("Google Streets");
Layer1 = new OpenLayers.Layer.WMS("1648", "http://localhost:8080/geoserver/wms",
{ layers: 'my_wor:Layer1', transparent: true },
{ isBaseLayer: false });
Layer2 = new OpenLayers.Layer.WMS("2015", "http://localhost:8080/geoserver/wms",
{ layers: 'my_wor:Layer2', transparent: true },
{ isBaseLayer: false });
map.addLayers([gmap, osm, Layer1, Layer2]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.ScaleLine());
map.addControl(new OpenLayers.Control.OverviewMap());
map.addControl(new OpenLayers.Control.Navigation());
map.addControl(new OpenLayers.Control.PanZoomBar());
</script>
See the SLD below, with the vendor options "group" is given true.
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>Polygon with styled label</Name>
<UserStyle>
<Title>SLD Cook Book: Polygon with styled label</Title>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#CCCCCC</CssParameter>
<CssParameter name="stroke-width">1</CssParameter>
</Stroke>
</PolygonSymbolizer>
<TextSymbolizer>
<Label>
<ogc:PropertyName>label</ogc:PropertyName>
</Label>
<Font>
<CssParameter name="font-family">Arial</CssParameter>
<CssParameter name="font-size">12</CssParameter>
<CssParameter name="font-style">normal</CssParameter>
<CssParameter name="font-weight">bold</CssParameter>
</Font>
<LabelPlacement>
<PointPlacement>
<AnchorPoint>
<AnchorPointX>0.5</AnchorPointX>
<AnchorPointY>0.5</AnchorPointY>
</AnchorPoint>
</PointPlacement>
</LabelPlacement>
<Fill>
<CssParameter name="fill">#000000</CssParameter>
</Fill>
<VendorOption name="autoWrap">160</VendorOption>
<VendorOption name="repeat">0</VendorOption>
<VendorOption name="group">true</VendorOption>
<VendorOption name="labelAllGroup">false</VendorOption>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Best Answer
I will try to be as clear as possible, but I am not sure I understood everything that your are asking. You might want to edit your post with additional information or a concrete example and the version of Geoserver you are using.
Problem 1: you have duplicate labels due to the tiling of your WMS request by OL
-> solution: in Open Layer, request a single WMS image (see example here http://openlayers.org/en/v3.1.1/examples/wms-image.html)
Problem 2: you have duplicate labels due to multi geometries
-> solution: use the vendor option
group
in Geoserver SLD(see here http://docs.geoserver.org/latest/en/user/styling/sld-reference/labeling.html)
These are not miracle solutions and you might still need to tweek your SLD to get exactly what you want.
EDIT – regarding Problem 2
A possible trick I used on a small dataset: manually delete the label value for the smaller entities of my multi-geometries. However, I would not consider this good practice.
Another trick would be to create a additional column in your database and to assign 1 (=need a label) and 0 (=does not need a label) and to use it in a conditional filter in SLD to tell Geoserver whether to draw a label or not
EDIT 2
(after the comment from @user30184 & @OpenMapperz)
This is the same example, but for OL3 here http://openlayers.org/en/master/examples/wms-image.html?q=wms
If you want to find the relevant syntax, look at the OL3 documentation. The relevant point here is to use the object
ol.layer.Image
withol.layer.ImageWMS
.