4

When creating labels using centroids of a polygon layer, I'm facing the problem that some of them are missing. Eg:

Missing labels

Ok, I can make it reappear by using "maxDisplacement" vendor option, but, using this option labels appear displaced. Some to the right, some to the left.

Displaced labels

How can I render all the labels propperly in the centroid of the polygons?

Here is the SLD code I'm using:

<?xml version="1.0" encoding="UTF-8"?>
<sld:UserStyle 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">
  <sld:Title/>
  <FeatureTypeStyle>
     <Rule>
       <PolygonSymbolizer>
         <Fill>
           <CssParameter name="fill">#40FF40</CssParameter>
         </Fill>
         <Stroke>
           <CssParameter name="stroke">#FFFFFF</CssParameter>
           <CssParameter name="stroke-width">2</CssParameter>
         </Stroke>
       </PolygonSymbolizer>
       <TextSymbolizer>
        <sld:Geometry>
          <ogc:Function name="centroid">
            <ogc:PropertyName>geom</ogc:PropertyName>
          </ogc:Function>
        </sld:Geometry>
         <Label>
           <ogc:PropertyName>cod_25000</ogc:PropertyName>
         </Label>
         <Font>
           <CssParameter name="font-family">Arial</CssParameter>
           <CssParameter name="font-size">11</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">100</VendorOption>
         <VendorOption name="maxDisplacement">50</VendorOption>
         <VendorOption name="repeat">1</VendorOption>
       </TextSymbolizer>
     </Rule>
   </FeatureTypeStyle>
</sld:UserStyle>
2
  • 2
    turn off tiling for your labels layer.
    – Ian Turton
    Commented May 25, 2015 at 13:16
  • Thanks iant! I've changed in openlayers the TileWMS layer to ImageWMS and now displays correcly. This layer is not complex and renders fast, but in case I have a complex layer and I need tiling to improve performance, wich should be the proper way to display the labels? Maybe creating a point layer using polygon centroids? (Maybe I should edit my question...) Commented May 27, 2015 at 7:29

1 Answer 1

1

You can use the following style.

<se:Geometry>
    <ogc:Function name="centroid">
         <ogc:PropertyName>geom</ogc:PropertyName>
    </ogc:Function>
</se:Geometry>

enter image description here

So the rendering of labels will be done on the centroid of the polygon.

3
  • Please add the code-like text as text, not picture. Commented Apr 14, 2021 at 20:50
  • <se:Geometry> <ogc:Function name="centroid"> <ogc:PropertyName>geom</ogc:PropertyName> </ogc:Function> </se:Geometry>
    – ZhefengJin
    Commented Apr 15, 2021 at 1:30
  • fyi if you are using the css sld extension you can use the following line for the same effect label-geometry: [centroid(geom)]; Commented May 1 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.