如何在GeoServer CSS中添加条件逻辑实现道路标签智能显示
GeoServer道路标签优化:避免缺失字段时异常显示
在GeoServer中配置道路标签时,需求是将道路编码与名称并排显示(示例:X123-Highway),但使用初始配置时,若道路编码或名称任一缺失,会出现如编码-编码或名称-名称的重复显示问题,需要优化为仅在对应内容存在时显示单项标签。
初始问题代码
[@scale < 8k] { label: [code]"-"[name]; label-anchor: 0.5 0.5; font-fill: black; font-weight: bold; label-follow-line: true; halo-color: #FFFFFF; halo-radius: 2; }
优化后的代码
[@scale < 8k] { label: [code is Not Null][name is Not Null] ? [code]" - "[name] : [code is Not Null] ? [code] : [name]; label-anchor: 0.5 0.5; font-fill: black; font-weight: bold; label-follow-line: true; halo-color: #FFFFFF; halo-radius: 2; }
代码逻辑说明
通过嵌套条件判断实现精准显示:
- 当
code和name均不为空时,显示编码 - 名称格式 - 仅
code不为空时,只显示编码内容 - 仅
name不为空时,只显示名称内容
内容的提问来源于stack exchange,提问作者Priyanka Shakya
相关产品推荐
相关产品推荐

