10.2 QGIS标签(labels)的应用与二次开发实现

前言

标注(Labels)

标注是与图层关联的,一般通过图层的属性信息生成,并与矢量要素一一关联。

  • protected_areas.shp为例展示标注的使用,默认效果如下
    在这里插入图片描述

QGis中的标注(Label)

  • 打开图层属性,切换到“Labels”选项卡,如下图所示
    在这里插入图片描述
  • 在最上方的下拉列表中选择“Single labels”,各选项的说明如下:
    • No labels:无标注。
    • Single labels:简单标注。
    • Rule-based labeling:基于规则标注。
    • Blocking:遮挡,用于调整其他图层的标注布局,防止标注被遮挡。将图层设置为其他图层标签的遮挡时,其本身无须呈现任何标签。
  • Value选项中选择需要标注的字段,此处选择"name"
  • 通过文字(Text)、格式化(Formatting)、缓冲区(Buffer)、遮罩(Mask)、背景(Background)、阴影(Shadow)、位置(Placement)、渲染(Rendering)选项卡设置格式,如下图
    在这里插入图片描述
  • 属性设置完成后,图层显示效果如下
    在这里插入图片描述

二次开发实现标注(Label)

  • QgsVectorLayer中成员函数setLabeling(QgsAbstractVectorLayerLabeling *labeling)用于设置标注,函数setLabelsEnabled(true)开启标注,QgsAbstractVectorLayerLabeling类图如下

在这里插入图片描述
-QgsVectorLayerSimpleLabeling是QGis中Single Lables,详情见文档

  • QgsVectorLayerSimpleLabeling构造函数需要传入类QgsPalLayerSettings,该类包含了文字(Text)、格式化(Formatting)、缓冲区(Buffer)、遮罩(Mask)、背景(Background)、阴影(Shadow)、位置(Placement)的设置,详情见文档
  • QgsPalLayerSettings的成员函数setCallout,setPlacementSettings对应QGis Single Labels中Callouts和Placement
  • QgsPalLayerSettings的成员函数setFormat(const QgsTextFormat &format)用于设置Label的格式,其中参数QgsTextFormat代表文本格式,详情见文档
  • QgsTextFormat的函数setBackground(in const QgsTextBackgroundSettings &backgroundSettings)对应文本背景设置
  • QgsTextFormat的函数setBuffer(in const QgsTextBufferSettings &bufferSettings)对应Buffer设置
  • QgsTextFormat的函数setShadow(in const QgsTextShadowSettings &shadowSettings)对应于Shadow设置
  • QgsTextFormat的函数setMask(in const QgsTextMaskSettings &maskSettings)对应于Mask设置
  • 以上几个类的关联图如下
    在这里插入图片描述
  • 完整测试代码如下
void MainWindow::labelSlot()
{
    //添加一个线图层
    QgsVectorLayer* layer = addTestShape(QStringLiteral("maps/shapefile/protected_areas.shp"));
    //
    //QgsTextFormat --> QgsTextBackgroundSettings
    //              --> QgsTextBufferSettings
    //              --> QgsTextMaskSettings
    //              --> QgsTextShadowSettings
    //QgsCallout
    //QgsLabelPlacementSettings
    //QgsPalLayerSettings包含文本设置
    QgsPalLayerSettings settings;
    settings.drawLabels = true;
    settings.fieldName = "name";
    //文字设置
    QgsTextFormat textFormat;
    QgsTextBackgroundSettings backgroundSettings;
    QgsTextBufferSettings bufferSettings;
    QgsTextMaskSettings maskSettings;
    QgsTextShadowSettings shadowSettings;
    //开启背景并设置为白色
    backgroundSettings.setEnabled(true);
    backgroundSettings.setFillColor(QColor("white"));
    textFormat.setBackground(backgroundSettings);
    //开启buffer并设置颜色为黑色
    bufferSettings.setEnabled(true);
    bufferSettings.setColor(QColor("black"));
    textFormat.setBuffer(bufferSettings);
    //开启mask并设置size
    maskSettings.setEnabled(true);
    maskSettings.setSize(1.0);
    textFormat.setMask(maskSettings);
    //开启shadow
    shadowSettings.setEnabled(true);
    shadowSettings.setColor(QColor("red"));
    textFormat.setShadow(shadowSettings);
    settings.setFormat(textFormat);
    //设置callout
    auto callout = new QgsBalloonCallout();
    settings.setCallout(callout);
    //设置位置
    QgsLabelPlacementSettings placementSettings;
    placementSettings.setAllowDegradedPlacement(true);
    settings.setPlacementSettings(placementSettings);

    QgsVectorLayerSimpleLabeling* simpleLabeling = new QgsVectorLayerSimpleLabeling(settings);
    layer->setLabelsEnabled(true);
    layer->setLabeling(simpleLabeling);
}
  • 效果如下图
    在这里插入图片描述
    在这里插入图片描述

总结

  • 介绍了QGis中Label的使用以及二次开发代码的实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雷动软件工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值