解决geoserver多边形标签重复问题 —— 使用centroid

geoserver在生成多边形标签的时候,同一个多边形的标签有可能会出现多次,解决这个问题可以使用多边形的质心功能,将多边形的标签固定到该多边形的质心上,关于质心的官方介绍:

NameArgumentsDescription
centroidgeometry:GeometryReturns the centroid of the geometry. Can be often used as a label point for polygons, though there is no guarantee it will actually lie inside the geometry
<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>
              <LabelPlacement>          	
                  <PointPlacement>
                      <AnchorPoint>
                          <AnchorPointX>0.5</AnchorPointX>
                          <AnchorPointY>0.5</AnchorPointY>
                      </AnchorPoint>
                      <Displacement>
                          <DisplacementX>0</DisplacementX>
                          <DisplacementY>0</DisplacementY>
                      </Displacement>
                  </PointPlacement>
              </LabelPlacement>  
              <Fill>
             		<CssParameter name="fill">#f4e925</CssParameter> <!-- 字体颜色 -->
         		</Fill>
            </TextSymbolizer>
        </Rule>
</FeatureTypeStyle>

关键的代码就是这块:

<Geometry>
    <ogc:Function name="centroid">
        <ogc:PropertyName>the_geom</ogc:PropertyName><!-- 多边形图形字段名 -->
    </ogc:Function>
</Geometry>

效果如图显示:
在这里插入图片描述

注意:
1,如果多边形包含多个闭合的子多边形,则每个子多边形的质心都会有一个标签
在这里插入图片描述
2,如果多边形的质心在多边形外,则标签也会显示到多边形外
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值