Article directory
- Preface
- Single symbol rendering
-
- Simple Marker
-
- QGis
- Code
- SVG marker
-
- QGis
- Code
- Summarize
Foreword
- The previous tutorial gave an overall introduction to vector layer symbolization, and took point layers as an example to introduce the renderers that can be used:
- No symbols
- Single symbol
- Category (Categorized)
- Graduated
- Rule-based
- Point displacement
- Point cluster
- Heatmap
- This article introduces how to use a single symbol
- Note: The sample codes in the article are all from the open source project qgis_cpp_api_apps
Single symbol rendering
- QGIS uses a single symbol and Simple Marker for rendering by default. Taking places_33S.shp as an example, after adding the places_33S layer, the default display is as shown below
- In the layer properties of the point feature layer, select the “Symbology” tab and you can see the default display, as shown below
- In addition to Simple Marker, there are many other options as shown below
Simple Marker
Simple marker symbol layer, consisting of a rendered shape with solid fill color and an stroke.
- Simple markers refer to using some simple geometric shapes as markers, and can set their fill color and brush color
QGis
- Set the Symbol layer type to Simple marker Layer and set the properties as shown below.
Code implementation
- The
QgsSimpleMarkerSymbolLayer
class is a simple marker layer (Simple marker Layer). The class diagram is as follows
- Its constructor is as follows, the parameters of the constructor are its corresponding parameters
QgsSimpleMarkerSymbolLayer (Qgis::MarkerShape shape=Qgis::MarkerShape::Circle, double size=DEFAULT_SIMPLEMARKER_SIZE, double angle=DEFAULT_SIMPLEMARKER_ANGLE, Qgis::ScaleMethod scaleMethod=DEFAULT_SCALE_METHOD, const QColor & amp;color=DEFAULT_SIMPLEMARK ER_COLOR, const QColor & amp ;strokeColor=DEFAULT_SIMPLEMARKER_BORDERCOLOR, Qt::PenJoinStyle penJoinStyle=DEFAULT_SIMPLEMARKER_JOINSTYLE)
- The steps to set the point symbol to a simple mark are as follows:
- First get the renderer from the layer and convert it to a Single Symbol Renderer code as follows
QgsFeatureRenderer * layerRenderer= layer->renderer(); QgsSingleSymbolRenderer *singleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
- Construct a Simple Marker SymbolLayer, the code is as follows
auto markerSymbolLayer = new QgsSimpleMarkerSymbolLayer(Qgis::MarkerShape::Heart,4.0); QgsSymbolLayerList layerList; layerList << markerSymbolLayer;
- Construct Marker Symbol and set symbol for renderer. The code is as follows
auto markerSymbol = new QgsMarkerSymbol(layerList); singleRenderer->setSymbol(markerSymbol); layer->setRenderer(singleRenderer);
SVG marker
provides you with images from your SVG paths to render as marker symbol.
Each SVG file colors and stroke can also be adapted.
- svg tag refers to using svg pictures as tags, and you can set the fill color and brush color of svg
QGis
- Set the Symbol layer type to SVG marker (SVG marker). See the following figure for the properties.
Code implementation
- The
QgsSvgMarkerSymbolLayer
class is an SVG marker layer. The class diagram is as follows
- Its constructor is as follows, the parameters of the constructor are its corresponding parameters
QgsSvgMarkerSymbolLayer (const QString & amp;path, double size=DEFAULT_SVGMARKER_SIZE, double angle=DEFAULT_SVGMARKER_ANGLE, Qgis::ScaleMethod scaleMethod=DEFAULT_SCALE_METHOD)
- The steps to set the point symbol as an svg tag are as follows:
- First get the renderer from the layer and convert it to a Single Symbol Renderer code as follows
QgsFeatureRenderer * layerRenderer= layer->renderer(); QgsSingleSymbolRenderer *singleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
- Construct the Svg Marker SymbolLayer, the code is as follows
//Construct Svg Marker SymbolLayer method one //As an example, two parameters, path and size, are used. Other parameters are used similarly. QString path = QStringLiteral("resources/plane.svg"); auto markerSymbolLayer = new QgsSvgMarkerSymbolLayer(path,4.0);
- Construct Marker Symbol and set symbol for renderer. The code is as follows
//QgsSymbolLayerList needs to be passed in to the QgsMarkerSymbol constructor //Multiple Symbol Layers form a Symbol QgsSymbolLayerList layerList; layerList << markerSymbolLayer; auto markerSymbol = new QgsMarkerSymbol(layerList); singleRenderer->setSymbol(markerSymbol); layer->setRenderer(singleRenderer);
Summary
- The methods of using a single symbol for the point layer and setting it as a simple mark and svg mark are introduced respectively.