Centroid fill 是一种用于地理信息系统 (GIS) 中填充面状地物的方法。在计算每个多边形的质心后,将该点与邻近的多边形连接,然后填充整个区域。该方法通常用于将遥感图像转换为矢量格式,或者在地图制作中创建特定类型的地图,例如土壤类型地图或植被覆盖地图。
QgsCentroidFillSymbolLayer
是仅渲染中心点(Centroid fill),详情见文档,类图如下QgsCentroidFillSymbolLayer
需要设置子符号为Marker符号QgsCentroidFillSymbolLayer
构造函数中默认创建了一个Simple Marker的符号,代码如下QgsCentroidFillSymbolLayer::QgsCentroidFillSymbolLayer()
{
setSubSymbol( new QgsMarkerSymbol() );
}
create
//从图层获取渲染器
QgsFeatureRenderer * layerRenderer= layer->renderer();
QgsSingleSymbolRenderer *singleSymbolRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
auto centroidFillSymbolLayer = new QgsCentroidFillSymbolLayer();
centroidFillSymbolLayer->setPointOnSurface(false);
centroidFillSymbolLayer->setPointOnAllParts(true);
centroidFillSymbolLayer->setClipPoints(false);
centroidFillSymbolLayer->setClipOnCurrentPartOnly(false);
//多个Symbol Layer构成一个Symbol
QgsSymbolLayerList layerList;
layerList << centroidFillSymbolLayer;
//构造QgsFillSymbol并设置renderer
auto fillSymbol = new QgsFillSymbol(layerList);
singleSymbolRenderer->setSymbol(fillSymbol);
layer->setRenderer(singleSymbolRenderer);
create()
成员函数,颜色方案同样有两种选择 QVariantMap mp;
mp["point_on_surface"] = QStringLiteral("0");
mp["point_on_all_parts"] = QStringLiteral("1");
mp["clip_points"] = QStringLiteral("0");
mp["clip_on_current_part_only"] = QStringLiteral("0");
auto centroidFillSymbolLayer = QgsCentroidFillSymbolLayer::create(mp);
void MainWindow::polygonCentroidSlot()
{
//添加一个线图层
QgsVectorLayer* layer = addTestShape(QStringLiteral("maps/shapefile/water.shp"));
//从图层获取渲染器
QgsFeatureRenderer * layerRenderer= layer->renderer();
QgsSingleSymbolRenderer *singleSymbolRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
#if 0
auto centroidFillSymbolLayer = new QgsCentroidFillSymbolLayer();
centroidFillSymbolLayer->setPointOnSurface(false);
centroidFillSymbolLayer->setPointOnAllParts(true);
centroidFillSymbolLayer->setClipPoints(false);
centroidFillSymbolLayer->setClipOnCurrentPartOnly(false);
#else
QVariantMap mp;
mp["point_on_surface"] = QStringLiteral("0");
mp["point_on_all_parts"] = QStringLiteral("1");
mp["clip_points"] = QStringLiteral("0");
mp["clip_on_current_part_only"] = QStringLiteral("0");
auto centroidFillSymbolLayer = QgsCentroidFillSymbolLayer::create(mp);
#endif
//多个Symbol Layer构成一个Symbol
QgsSymbolLayerList layerList;
layerList << centroidFillSymbolLayer;
//构造QgsFillSymbol并设置renderer
auto fillSymbol = new QgsFillSymbol(layerList);
singleSymbolRenderer->setSymbol(fillSymbol);
layer->setRenderer(singleSymbolRenderer);
}