在配置多边形标注样式的时候,无可避免的会对标注的位置进行微调,我们一般会用到下面这组样式标注:
<LabelPlacement>
<PointPlacement>
<AnchorPoint>
<AnchorPointX>0</AnchorPointX>
<AnchorPointY>0</AnchorPointY>
</AnchorPoint>
<Displacement>
<DisplacementX>0</DisplacementX>
<DisplacementY>0</DisplacementY>
</Displacement>
</PointPlacement>
</LabelPlacement>
那么我们如何调整这4个参数来调整标注位置呢?
首先,我们将这4个参数均设置为0,标注位置如图所示:
由于我配置的是让标注固定到多边形的质心位置,可见标注的左下角是在质心位置的;因此,如果4个参数均不设置的话,标注的左下角会在默认位置。
通过尝试修改AnchorPointX和AnchorPointY的值会发现,它俩的值指的是标注相对自身偏移的倍数,比如将AnchorPointX和AnchorPointY均设置为0.5,即标注xy轴均偏移自身一半,神奇的事情发生了,标注的正中心就会移动到多边形的质心上,如下图所示:
由此可见AnchorPoint配置代表标注相对自身的长宽位移的倍数
通过尝试修改DisplacementX和DisplacementY的值会发现,它俩的值指的是标注在x轴和y轴位移的px值,这个就比较好理解了,如果让标注往右和上各移动1px,设置DisplacementX和DisplacementY均设置为1即可
由此可见Displacement配置代表标注偏移的xy轴px值
以上为自己摸索得出的结论,没有找到官方的解释,不知是否正确,有问题欢迎大家指正
为了将标注的中心放在多边形的质心位置,完整的样式代码如下:
<?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>provincePlolygon</Name>
<UserStyle>
<Title>A yellow polygon style</Title>
<FeatureTypeStyle>
<Rule>
<Title>yellow polygon</Title>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#1f2f47</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#038bc0</CssParameter>
<CssParameter name="stroke-width">0.5</CssParameter>
</Stroke>
</PolygonSymbolizer>
<TextSymbolizer>
<Geometry>
<ogc:Function name="centroid">
<ogc:PropertyName>the_geom</ogc:PropertyName><!-- 多边形图形属性的字段名 -->
</ogc:Function>
</Geometry>
<Label>
<ogc:PropertyName>NAME</ogc:PropertyName><!-- 显示的标注字段名 -->
</Label>
<Font>
<CssParameter name="font-family">微软雅黑</CssParameter>
<CssParameter name="font-weight">Bold</CssParameter>
<CssParameter name="font-size">11</CssParameter>
</Font>
<Fill>
<CssParameter name="fill">#f4e925</CssParameter><!-- 标注字体颜色 -->
</Fill>
<LabelPlacement>
<PointPlacement>
<AnchorPoint>
<AnchorPointX>0.5</AnchorPointX>
<AnchorPointY>0.5</AnchorPointY>
</AnchorPoint>
<Displacement>
<DisplacementX>0</DisplacementX>
<DisplacementY>0</DisplacementY>
</Displacement>
</PointPlacement>
</LabelPlacement>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>