QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
196 = QPointF( point.x() + mScale * ctx.convertToPainterUnits( xVal, mDistanceUnit, mDistanceMapUnitScale ), point.y() - mScale * ctx.convertToPainterUnits( yVal, mDistanceUnit, mDistanceMapUnitScale ) );
197 break;
198 }
199
200 case Polar:
201 {
202 convertPolarToCartesian( xVal, yVal, xComponent, yComponent );
203 destPoint
204 = QPointF( point.x() + mScale * ctx.convertToPainterUnits( xComponent, mDistanceUnit, mDistanceMapUnitScale ), 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(
219 cos( radians ) * ( destPoint.x() - point.x() ) - sin( radians ) * ( destPoint.y() - point.y() ) + point.x(),
220 sin( radians ) * ( destPoint.x() - point.x() ) + cos( radians ) * ( destPoint.y() - point.y() ) + point.y()
221 );
222 }
223
224 line << destPoint;
225
226 mLineSymbol->renderPolyline( line, &f, context.renderContext() );
228}
229
231{
232 if ( mLineSymbol )
233 {
234 mLineSymbol->setRenderHints( mLineSymbol->renderHints() | Qgis::SymbolRenderHint::IsSymbolLayerSubSymbol );
235 mLineSymbol->startRender( context.renderContext(), context.fields() );
236 }
237
238 const QgsFields fields = context.fields();
239 if ( !fields.isEmpty() )
240 {
241 mXIndex = fields.lookupField( mXAttribute );
242 mYIndex = fields.lookupField( mYAttribute );
243 }
244 else
245 {
246 mXIndex = -1;
247 mYIndex = -1;
248 }
249}
250
252{
253 if ( mLineSymbol )
254 {
255 mLineSymbol->stopRender( context.renderContext() );
256 }
257}
258
260{
262 if ( mLineSymbol )
263 {
264 clonedLayer->setSubSymbol( mLineSymbol->clone() );
265 }
266 return static_cast< QgsVectorFieldSymbolLayer * >( clonedLayer );
267}
268
270{
271 QVariantMap properties;
272 properties[u"x_attribute"_s] = mXAttribute;
273 properties[u"y_attribute"_s] = mYAttribute;
274 properties[u"distance_unit"_s] = QgsUnitTypes::encodeUnit( mDistanceUnit );
275 properties[u"distance_map_unit_scale"_s] = QgsSymbolLayerUtils::encodeMapUnitScale( mDistanceMapUnitScale );
276 properties[u"scale"_s] = QString::number( mScale );
277 properties[u"vector_field_type"_s] = QString::number( mVectorFieldType );
278 properties[u"angle_orientation"_s] = QString::number( mAngleOrientation );
279 properties[u"angle_units"_s] = QString::number( mAngleUnits );
280 properties[u"size"_s] = QString::number( mSize );
281 properties[u"size_unit"_s] = QgsUnitTypes::encodeUnit( mSizeUnit );
284 properties[u"offset_unit"_s] = QgsUnitTypes::encodeUnit( mOffsetUnit );
286 return properties;
287}
288
298
299void QgsVectorFieldSymbolLayer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
300{
301 QgsSldExportContext context;
302 context.setExtraProperties( props );
303 toSld( doc, element, context );
304}
305
306bool QgsVectorFieldSymbolLayer::toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext &context ) const
307{
308 context.pushError( QObject::tr( "Vector field symbol layers cannot be converted to SLD" ) );
309 mLineSymbol->toSld( doc, element, context );
310 return false;
311}
312
314{
315 Q_UNUSED( element )
316 return nullptr;
317}
318
320{
321 if ( mLineSymbol )
322 {
323 mLineSymbol->drawPreviewIcon( context.renderContext().painter(), size );
324 }
325}
326
328{
329 QSet<QString> attributes = QgsMarkerSymbolLayer::usedAttributes( context );
330 if ( !mXAttribute.isEmpty() )
331 {
332 attributes.insert( mXAttribute );
333 }
334 if ( !mYAttribute.isEmpty() )
335 {
336 attributes.insert( mYAttribute );
337 }
338 if ( mLineSymbol )
339 {
340 attributes.unite( mLineSymbol->usedAttributes( context ) );
341 }
342 return attributes;
343}
344
346{
348 return true;
349 if ( mLineSymbol && mLineSymbol->hasDataDefinedProperties() )
350 return true;
351 return false;
352}
353
354void QgsVectorFieldSymbolLayer::convertPolarToCartesian( double length, double angle, double &x, double &y ) const
355{
356 //convert angle to degree and to north orientation
357 if ( mAngleOrientation == CounterclockwiseFromEast )
358 {
359 if ( angle <= 90 )
360 {
361 angle = 90 - angle;
362 }
363 else
364 {
365 angle = 360 - angle + 90;
366 }
367 }
368
369 if ( mAngleUnits == Degrees )
370 {
371 angle = angle * M_PI / 180.0;
372 }
373
374 x = length * std::sin( angle );
375 y = length * std::cos( angle );
376}
377
379{
380 if ( mLineSymbol )
381 mLineSymbol->setColor( color );
382
383 mColor = color;
384}
385
387{
388 return mLineSymbol ? mLineSymbol->color() : mColor;
389}
@ IsSymbolLayerSubSymbol
Symbol is being rendered as a sub-symbol of a QgsSymbolLayer.
Definition qgis.h:797
RenderUnit
Rendering size units.
Definition qgis.h:5340
@ Unknown
Mixed or unknown units.
Definition qgis.h:5347
@ MapUnits
Map units.
Definition qgis.h:5342
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5348
@ RenderingSubSymbol
Set whenever a sub-symbol of a parent symbol is currently being rendered. Can be used during symbol a...
Definition qgis.h:2862
@ Line
Line symbol.
Definition qgis.h:638
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:227
Qgis::SymbolType type() const
Returns the symbol's type.
Definition qgssymbol.h:296
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:6975