QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsvectorfieldsymbollayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorfieldsymbollayer.cpp
3 -----------------------------
4 begin : Octorer 25, 2011
5 copyright : (C) 2011 by Marco Hugentobler
6 email : marco dot hugentobler at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgslinesymbol.h"
21#include "qgssldexportcontext.h"
22#include "qgssymbollayerutils.h"
23#include "qgsunittypes.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
33
35
37{
39 mDistanceUnit = unit;
40 if ( mLineSymbol )
41 mLineSymbol->setOutputUnit( unit );
42}
43
45{
46 if ( QgsMarkerSymbolLayer::outputUnit() == mDistanceUnit )
47 {
48 return mDistanceUnit;
49 }
51}
52
58
60{
61 if ( QgsMarkerSymbolLayer::mapUnitScale() == mDistanceMapUnitScale )
62 {
63 return mDistanceMapUnitScale;
64 }
65 return QgsMapUnitScale();
66}
67
69{
71 if ( properties.contains( u"x_attribute"_s ) )
72 {
73 symbolLayer->setXAttribute( properties[u"x_attribute"_s].toString() );
74 }
75 if ( properties.contains( u"y_attribute"_s ) )
76 {
77 symbolLayer->setYAttribute( properties[u"y_attribute"_s].toString() );
78 }
79 if ( properties.contains( u"distance_unit"_s ) )
80 {
81 symbolLayer->setDistanceUnit( QgsUnitTypes::decodeRenderUnit( properties[u"distance_unit"_s].toString() ) );
82 }
83 if ( properties.contains( u"distance_map_unit_scale"_s ) )
84 {
85 symbolLayer->setDistanceMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( properties[u"distance_map_unit_scale"_s].toString() ) );
86 }
87 if ( properties.contains( u"scale"_s ) )
88 {
89 symbolLayer->setScale( properties[u"scale"_s].toDouble() );
90 }
91 if ( properties.contains( u"vector_field_type"_s ) )
92 {
93 symbolLayer->setVectorFieldType( static_cast< VectorFieldType >( properties[u"vector_field_type"_s].toInt() ) );
94 }
95 if ( properties.contains( u"angle_orientation"_s ) )
96 {
97 symbolLayer->setAngleOrientation( static_cast< AngleOrientation >( properties[u"angle_orientation"_s].toInt() ) );
98 }
99 if ( properties.contains( u"angle_units"_s ) )
100 {
101 symbolLayer->setAngleUnits( static_cast< AngleUnits >( properties[u"angle_units"_s].toInt() ) );
102 }
103 if ( properties.contains( u"size"_s ) )
104 {
105 symbolLayer->setSize( properties[u"size"_s].toDouble() );
106 }
107 if ( properties.contains( u"size_unit"_s ) )
108 {
109 symbolLayer->setSizeUnit( QgsUnitTypes::decodeRenderUnit( properties[u"size_unit"_s].toString() ) );
110 }
111 if ( properties.contains( u"size_map_unit_scale"_s ) )
112 {
113 symbolLayer->setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( properties[u"size_map_unit_scale"_s].toString() ) );
114 }
115 if ( properties.contains( u"offset"_s ) )
116 {
117 symbolLayer->setOffset( QgsSymbolLayerUtils::decodePoint( properties[u"offset"_s].toString() ) );
118 }
119 if ( properties.contains( u"offset_unit"_s ) )
120 {
121 symbolLayer->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( properties[u"offset_unit"_s].toString() ) );
122 }
123 if ( properties.contains( u"offset_map_unit_scale"_s ) )
124 {
125 symbolLayer->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( properties[u"offset_map_unit_scale"_s].toString() ) );
126 }
127 return symbolLayer;
128}
129
131{
132 if ( symbol->type() == Qgis::SymbolType::Line )
133 {
134 mLineSymbol.reset( static_cast<QgsLineSymbol *>( symbol ) );
135 return true;
136 }
137 return false;
138}
139
141{
142 return mLineSymbol.get();
143}
144
146{
147 if ( !mLineSymbol )
148 {
149 return;
150 }
151
152 const QgsRenderContext &ctx = context.renderContext();
153
154 const bool prevIsSubsymbol = context.renderContext().flags() & Qgis::RenderContextFlag::RenderingSubSymbol;
156
157 if ( !context.feature() )
158 {
159 //preview
160 QPolygonF line;
161 line << QPointF( 0, 50 );
162 line << QPointF( 100, 50 );
163 mLineSymbol->renderPolyline( line, nullptr, context.renderContext() );
165 return;
166 }
167
168 const QgsFeature f( *context.feature() );
169
170 double xComponent = 0;
171 double yComponent = 0;
172
173 double xVal = 0;
174 if ( mXIndex != -1 )
175 {
176 xVal = f.attribute( mXIndex ).toDouble();
177 }
178 double yVal = 0;
179 if ( mYIndex != -1 )
180 {
181 yVal = f.attribute( mYIndex ).toDouble();
182 }
183
184 const QgsMapToPixel &m2p = ctx.mapToPixel();
185 const double mapRotation = m2p.mapRotation();
186
187 QPolygonF line;
188 line << point;
189
190 QPointF destPoint;
191 switch ( mVectorFieldType )
192 {
193 case Cartesian:
194 {
195 destPoint = QPointF( point.x() + mScale * ctx.convertToPainterUnits( xVal, mDistanceUnit, mDistanceMapUnitScale ),
196 point.y() - mScale * ctx.convertToPainterUnits( yVal, mDistanceUnit, mDistanceMapUnitScale ) );
197 break;
198 }
199
200 case Polar:
201 {
202 convertPolarToCartesian( xVal, yVal, xComponent, yComponent );
203 destPoint = QPointF( point.x() + mScale * ctx.convertToPainterUnits( xComponent, mDistanceUnit, mDistanceMapUnitScale ),
204 point.y() - mScale * ctx.convertToPainterUnits( yComponent, mDistanceUnit, mDistanceMapUnitScale ) );
205 break;
206 }
207
208 case Height:
209 {
210 destPoint = QPointF( point.x(), point.y() - ( mScale * ctx.convertToPainterUnits( yVal, mDistanceUnit, mDistanceMapUnitScale ) ) );
211 break;
212 }
213 }
214
215 if ( !qgsDoubleNear( mapRotation, 0.0 ) && mVectorFieldType != Height )
216 {
217 const double radians = mapRotation * M_PI / 180.0;
218 destPoint = QPointF( cos( radians ) * ( destPoint.x() - point.x() ) - sin( radians ) * ( destPoint.y() - point.y() ) + point.x(),
219 sin( radians ) * ( destPoint.x() - point.x() ) + cos( radians ) * ( destPoint.y() - point.y() ) + point.y() );
220 }
221
222 line << destPoint;
223
224 mLineSymbol->renderPolyline( line, &f, context.renderContext() );
226}
227
229{
230 if ( mLineSymbol )
231 {
232 mLineSymbol->setRenderHints( mLineSymbol->renderHints() | Qgis::SymbolRenderHint::IsSymbolLayerSubSymbol );
233 mLineSymbol->startRender( context.renderContext(), context.fields() );
234 }
235
236 const QgsFields fields = context.fields();
237 if ( !fields.isEmpty() )
238 {
239 mXIndex = fields.lookupField( mXAttribute );
240 mYIndex = fields.lookupField( mYAttribute );
241 }
242 else
243 {
244 mXIndex = -1;
245 mYIndex = -1;
246 }
247}
248
250{
251 if ( mLineSymbol )
252 {
253 mLineSymbol->stopRender( context.renderContext() );
254 }
255}
256
258{
260 if ( mLineSymbol )
261 {
262 clonedLayer->setSubSymbol( mLineSymbol->clone() );
263 }
264 return static_cast< QgsVectorFieldSymbolLayer * >( clonedLayer );
265}
266
268{
269 QVariantMap properties;
270 properties[u"x_attribute"_s] = mXAttribute;
271 properties[u"y_attribute"_s] = mYAttribute;
272 properties[u"distance_unit"_s] = QgsUnitTypes::encodeUnit( mDistanceUnit );
273 properties[u"distance_map_unit_scale"_s] = QgsSymbolLayerUtils::encodeMapUnitScale( mDistanceMapUnitScale );
274 properties[u"scale"_s] = QString::number( mScale );
275 properties[u"vector_field_type"_s] = QString::number( mVectorFieldType );
276 properties[u"angle_orientation"_s] = QString::number( mAngleOrientation );
277 properties[u"angle_units"_s] = QString::number( mAngleUnits );
278 properties[u"size"_s] = QString::number( mSize );
279 properties[u"size_unit"_s] = QgsUnitTypes::encodeUnit( mSizeUnit );
282 properties[u"offset_unit"_s] = QgsUnitTypes::encodeUnit( mOffsetUnit );
284 return properties;
285}
286
293
294void QgsVectorFieldSymbolLayer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
295{
296 QgsSldExportContext context;
297 context.setExtraProperties( props );
298 toSld( doc, element, context );
299}
300
301bool QgsVectorFieldSymbolLayer::toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const
302{
303 context.pushError( QObject::tr( "Vector field symbol layers cannot be converted to SLD" ) );
304 mLineSymbol->toSld( doc, element, context );
305 return false;
306}
307
309{
310 Q_UNUSED( element )
311 return nullptr;
312}
313
315{
316 if ( mLineSymbol )
317 {
318 mLineSymbol->drawPreviewIcon( context.renderContext().painter(), size );
319 }
320}
321
323{
324 QSet<QString> attributes = QgsMarkerSymbolLayer::usedAttributes( context );
325 if ( !mXAttribute.isEmpty() )
326 {
327 attributes.insert( mXAttribute );
328 }
329 if ( !mYAttribute.isEmpty() )
330 {
331 attributes.insert( mYAttribute );
332 }
333 if ( mLineSymbol )
334 {
335 attributes.unite( mLineSymbol->usedAttributes( context ) );
336 }
337 return attributes;
338}
339
341{
343 return true;
344 if ( mLineSymbol && mLineSymbol->hasDataDefinedProperties() )
345 return true;
346 return false;
347}
348
349void QgsVectorFieldSymbolLayer::convertPolarToCartesian( double length, double angle, double &x, double &y ) const
350{
351 //convert angle to degree and to north orientation
352 if ( mAngleOrientation == CounterclockwiseFromEast )
353 {
354 if ( angle <= 90 )
355 {
356 angle = 90 - angle;
357 }
358 else
359 {
360 angle = 360 - angle + 90;
361 }
362 }
363
364 if ( mAngleUnits == Degrees )
365 {
366 angle = angle * M_PI / 180.0;
367 }
368
369 x = length * std::sin( angle );
370 y = length * std::cos( angle );
371}
372
374{
375 if ( mLineSymbol )
376 mLineSymbol->setColor( color );
377
378 mColor = color;
379}
380
382{
383 return mLineSymbol ? mLineSymbol->color() : mColor;
384}
385
386
@ IsSymbolLayerSubSymbol
Symbol is being rendered as a sub-symbol of a QgsSymbolLayer.
Definition qgis.h:790
RenderUnit
Rendering size units.
Definition qgis.h:5279
@ Unknown
Mixed or unknown units.
Definition qgis.h:5286
@ MapUnits
Map units.
Definition qgis.h:5281
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5287
@ RenderingSubSymbol
Set whenever a sub-symbol of a parent symbol is currently being rendered. Can be used during symbol a...
Definition qgis.h:2823
@ Line
Line symbol.
Definition qgis.h:631
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Container of fields for a vector layer.
Definition qgsfields.h:46
bool isEmpty
Definition qgsfields.h:49
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A line symbol type, for rendering LineString and MultiLineString geometries.
Perform transforms between map coordinates and device coordinates.
double mapRotation() const
Returns the current map rotation in degrees (clockwise).
Struct for storing maximum and minimum scales for measurements in map units.
double mSize
Marker size.
virtual void setSize(double size)
Sets the symbol size.
Qgis::RenderUnit mOffsetUnit
Offset units.
void setOffsetUnit(Qgis::RenderUnit unit)
Sets the units for the symbol's offset.
Qgis::RenderUnit outputUnit() const override
Returns the units to use for sizes and widths within the symbol layer.
QPointF mOffset
Marker offset.
QgsMapUnitScale mapUnitScale() const override
void setOffset(QPointF offset)
Sets the marker's offset, which is the horizontal and vertical displacement which the rendered marker...
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol's size.
double size() const
Returns the symbol size.
QgsMapUnitScale mOffsetMapUnitScale
Offset map unit scale.
QgsMapUnitScale mSizeMapUnitScale
Marker size map unit scale.
Qgis::RenderUnit mSizeUnit
Marker size unit.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the symbol's size.
void setOffsetMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol's offset.
double angle() const
Returns the rotation angle for the marker, in degrees clockwise from north.
void setMapUnitScale(const QgsMapUnitScale &scale) override
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
Qgis::RenderContextFlags flags() const
Returns combination of flags used for rendering.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QString encodePoint(QPointF point)
Encodes a QPointF to a string.
static QPointF decodePoint(const QString &string)
Decodes a QSizeF from a string.
virtual bool setSubSymbol(QgsSymbol *symbol)
Sets layer's subsymbol. takes ownership of the passed symbol.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const
Returns the set of attributes referenced by the layer.
virtual bool hasDataDefinedProperties() const
Returns true if the symbol layer (or any of its sub-symbols) contains data defined properties.
QgsSymbolLayer(const QgsSymbolLayer &other)
Encapsulates the context in which a symbol is being rendered.
const QgsFeature * feature() const
Returns the current feature being rendered.
QgsFields fields() const
Fields of the layer.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
Qgis::SymbolType type() const
Returns the symbol's type.
Definition qgssymbol.h:294
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns the set of attributes referenced by the layer.
bool setSubSymbol(QgsSymbol *symbol) override
Sets layer's subsymbol. takes ownership of the passed symbol.
QColor color() const override
Returns the "representative" color of the symbol layer.
QgsSymbol * subSymbol() override
Returns the symbol's sub symbol, if present.
void setMapUnitScale(const QgsMapUnitScale &scale) override
void setYAttribute(const QString &attribute)
void setAngleUnits(AngleUnits units)
void setVectorFieldType(VectorFieldType type)
void setDistanceUnit(Qgis::RenderUnit unit)
Sets the units for the distance.
~QgsVectorFieldSymbolLayer() override
bool hasDataDefinedProperties() const override
Returns true if the symbol layer (or any of its sub-symbols) contains data defined properties.
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
void setAngleOrientation(AngleOrientation orientation)
Qgis::RenderUnit outputUnit() const override
Returns the units to use for sizes and widths within the symbol layer.
QVariantMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
void stopRender(QgsSymbolRenderContext &context) override
Called after a set of rendering operations has finished on the supplied render context.
QgsVectorFieldSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates the symbol layer.
QgsMapUnitScale mapUnitScale() const override
void setXAttribute(const QString &attribute)
void setDistanceMapUnitScale(const QgsMapUnitScale &scale)
bool usesMapUnits() const override
Returns true if the symbol layer has any components which use map unit based sizes.
static QgsSymbolLayer * createFromSld(QDomElement &element)
void setColor(const QColor &color) override
Sets the "representative" color for the symbol layer.
Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props) const override
Saves the symbol layer as SLD.
void renderPoint(QPointF point, QgsSymbolRenderContext &context) override
Renders a marker at the specified point.
void drawPreviewIcon(QgsSymbolRenderContext &context, QSize size) override
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6924