[GIS] How to show Labels on Geoserver, only if they fit within the Polygon

geoserverlabelingleafletsld

I'm trying to Set the Style via SLD on a Polygon Layer, such that it shows labels, and the Labels are visible only within the Polygon, and do not overflow outside the Polygon.

I've managed to get the Polygons to show, using the following SLD:

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
  xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/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>locality</Name>
        <UserStyle>
            <Title>locality</Title>
            <FeatureTypeStyle>
                <Rule>
                    <Title>locality</Title>
                    <PolygonSymbolizer>
                        <Stroke>
                            <CssParameter name="stroke">#47B8FF</CssParameter>
                            <CssParameter name="stroke-width">1</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>

                    <TextSymbolizer>
                        <Geometry>
                            <ogc:Function name="centroid">
                                <ogc:PropertyName>geom</ogc:PropertyName>
                            </ogc:Function>
                        </Geometry>
                        <Label>
                            <ogc:PropertyName>locality</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">#47B8FF</CssParameter>
                        </Fill>
                    </TextSymbolizer>
                </Rule>
            </FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

This has managed to show the Labels, but they still overlap adjacent polygons, like this:
enter image description here

How can I update this SLD, so that the Labels do not overlap adjacent polygons?

Best Answer

Add the following vendor option:

<VendorOption name="goodnessOfFit">1</VendorOption>

See also the documentation: http://docs.geoserver.org/stable/en/user/styling/sld/reference/labeling.html#goodnessoffit

Related Question