-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgssinglesymbolrendererv2.cpp
446 lines (378 loc) · 14 KB
/
qgssinglesymbolrendererv2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/***************************************************************************
qgssinglesymbolrendererv2.cpp
---------------------
begin : November 2009
copyright : (C) 2009 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgssinglesymbolrendererv2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgslogger.h"
#include "qgsfeature.h"
#include "qgsvectorlayer.h"
#include "qgssymbollayerv2.h"
#include "qgsogcutils.h"
#include "qgspointdisplacementrenderer.h"
#include "qgsinvertedpolygonrenderer.h"
#include "qgspainteffect.h"
#include "qgsscaleexpression.h"
#include "qgsdatadefined.h"
#include <QDomDocument>
#include <QDomElement>
QgsSingleSymbolRendererV2::QgsSingleSymbolRendererV2( QgsSymbolV2* symbol )
: QgsFeatureRendererV2( "singleSymbol" )
, mSymbol( symbol )
, mScaleMethod( DEFAULT_SCALE_METHOD )
, mOrigSize( 0.0 )
{
Q_ASSERT( symbol );
}
QgsSingleSymbolRendererV2::~QgsSingleSymbolRendererV2()
{
}
QgsSymbolV2* QgsSingleSymbolRendererV2::symbolForFeature( QgsFeature& feature )
{
if ( !mRotation.data() && !mSizeScale.data() ) return mSymbol.data();
const double rotation = mRotation.data() ? mRotation->evaluate( feature ).toDouble() : 0;
const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( feature ).toDouble() : 1.;
if ( mTempSymbol->type() == QgsSymbolV2::Marker )
{
QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mTempSymbol.data() );
if ( mRotation.data() ) markerSymbol->setAngle( rotation );
markerSymbol->setSize( sizeScale * mOrigSize );
markerSymbol->setScaleMethod( mScaleMethod );
}
else if ( mTempSymbol->type() == QgsSymbolV2::Line )
{
QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( mTempSymbol.data() );
lineSymbol->setWidth( sizeScale * mOrigSize );
}
else if ( mTempSymbol->type() == QgsSymbolV2::Fill )
{
QgsFillSymbolV2* fillSymbol = static_cast<QgsFillSymbolV2*>( mTempSymbol.data() );
if ( mRotation.data() ) fillSymbol->setAngle( rotation );
}
return mTempSymbol.data();
}
QgsSymbolV2* QgsSingleSymbolRendererV2::originalSymbolForFeature( QgsFeature& feature )
{
Q_UNUSED( feature );
return mSymbol.data();
}
void QgsSingleSymbolRendererV2::startRender( QgsRenderContext& context, const QgsFields& fields )
{
if ( !mSymbol.data() ) return;
mSymbol->startRender( context, &fields );
if ( mRotation.data() || mSizeScale.data() )
{
// we are going to need a temporary symbol
mTempSymbol.reset( mSymbol->clone() );
int hints = 0;
if ( mRotation.data() )
hints |= QgsSymbolV2::DataDefinedRotation;
if ( mSizeScale.data() )
hints |= QgsSymbolV2::DataDefinedSizeScale;
mTempSymbol->setRenderHints( hints );
mTempSymbol->startRender( context, &fields );
if ( mSymbol->type() == QgsSymbolV2::Marker )
{
mOrigSize = static_cast<QgsMarkerSymbolV2*>( mSymbol.data() )->size();
}
else if ( mSymbol->type() == QgsSymbolV2::Line )
{
mOrigSize = static_cast<QgsLineSymbolV2*>( mSymbol.data() )->width();
}
else
{
mOrigSize = 0;
}
}
}
void QgsSingleSymbolRendererV2::stopRender( QgsRenderContext& context )
{
if ( !mSymbol.data() ) return;
mSymbol->stopRender( context );
if ( mRotation.data() || mSizeScale.data() )
{
// we are going to need a temporary symbol
mTempSymbol->stopRender( context );
mTempSymbol.reset();
}
}
QList<QString> QgsSingleSymbolRendererV2::usedAttributes()
{
QSet<QString> attributes;
if ( mSymbol.data() ) attributes.unite( mSymbol->usedAttributes() );
if ( mRotation.data() ) attributes.unite( mRotation->referencedColumns().toSet() );
if ( mSizeScale.data() ) attributes.unite( mSizeScale->referencedColumns().toSet() );
return attributes.toList();
}
QgsSymbolV2* QgsSingleSymbolRendererV2::symbol() const
{
return mSymbol.data();
}
void QgsSingleSymbolRendererV2::setSymbol( QgsSymbolV2* s )
{
Q_ASSERT( s );
mSymbol.reset( s );
}
void QgsSingleSymbolRendererV2::setRotationField( QString fieldOrExpression )
{
mRotation.reset( QgsSymbolLayerV2Utils::fieldOrExpressionToExpression( fieldOrExpression ) );
}
QString QgsSingleSymbolRendererV2::rotationField() const
{
return mRotation.data() ? QgsSymbolLayerV2Utils::fieldOrExpressionFromExpression( mRotation.data() ) : QString();
}
void QgsSingleSymbolRendererV2::setSizeScaleField( QString fieldOrExpression )
{
mSizeScale.reset( QgsSymbolLayerV2Utils::fieldOrExpressionToExpression( fieldOrExpression ) );
}
QString QgsSingleSymbolRendererV2::sizeScaleField() const
{
return mSizeScale.data() ? QgsSymbolLayerV2Utils::fieldOrExpressionFromExpression( mSizeScale.data() ) : QString();
}
void QgsSingleSymbolRendererV2::setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod )
{
mScaleMethod = scaleMethod;
setScaleMethodToSymbol( mSymbol.data(), scaleMethod );
}
QString QgsSingleSymbolRendererV2::dump() const
{
return mSymbol.data() ? QString( "SINGLE: %1" ).arg( mSymbol->dump() ) : "";
}
QgsFeatureRendererV2* QgsSingleSymbolRendererV2::clone() const
{
QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( mSymbol->clone() );
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
//r->setScaleMethod( scaleMethod() );
copyPaintEffect( r );
return r;
}
void QgsSingleSymbolRendererV2::toSld( QDomDocument& doc, QDomElement &element ) const
{
QgsStringMap props;
QString errorMsg;
if ( mRotation.data() )
props[ "angle" ] = mRotation->expression();
if ( mSizeScale.data() )
props[ "scale" ] = mSizeScale->expression();
QDomElement ruleElem = doc.createElement( "se:Rule" );
element.appendChild( ruleElem );
QDomElement nameElem = doc.createElement( "se:Name" );
nameElem.appendChild( doc.createTextNode( "Single symbol" ) );
ruleElem.appendChild( nameElem );
if ( mSymbol.data() ) mSymbol->toSld( doc, ruleElem, props );
}
QgsSymbolV2List QgsSingleSymbolRendererV2::symbols()
{
QgsSymbolV2List lst;
lst.append( mSymbol.data() );
return lst;
}
QgsFeatureRendererV2* QgsSingleSymbolRendererV2::create( QDomElement& element )
{
QDomElement symbolsElem = element.firstChildElement( "symbols" );
if ( symbolsElem.isNull() )
return NULL;
QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
if ( !symbolMap.contains( "0" ) )
return NULL;
QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( symbolMap.take( "0" ) );
// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap( symbolMap );
QDomElement rotationElem = element.firstChildElement( "rotation" );
if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
{
convertSymbolRotation( r->mSymbol.data(), rotationElem.attribute( "field" ) );
}
QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
{
convertSymbolSizeScale( r->mSymbol.data(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}
// TODO: symbol levels
return r;
}
QgsFeatureRendererV2* QgsSingleSymbolRendererV2::createFromSld( QDomElement& element, QGis::GeometryType geomType )
{
// XXX this renderer can handle only one Rule!
// get the first Rule element
QDomElement ruleElem = element.firstChildElement( "Rule" );
if ( ruleElem.isNull() )
{
QgsDebugMsg( "no Rule elements found!" );
return NULL;
}
QString label, description;
QgsSymbolLayerV2List layers;
// retrieve the Rule element child nodes
QDomElement childElem = ruleElem.firstChildElement();
while ( !childElem.isNull() )
{
if ( childElem.localName() == "Name" )
{
// <se:Name> tag contains the rule identifier,
// so prefer title tag for the label property value
if ( label.isEmpty() )
label = childElem.firstChild().nodeValue();
}
else if ( childElem.localName() == "Description" )
{
// <se:Description> can contains a title and an abstract
QDomElement titleElem = childElem.firstChildElement( "Title" );
if ( !titleElem.isNull() )
{
label = titleElem.firstChild().nodeValue();
}
QDomElement abstractElem = childElem.firstChildElement( "Abstract" );
if ( !abstractElem.isNull() )
{
description = abstractElem.firstChild().nodeValue();
}
}
else if ( childElem.localName() == "Abstract" )
{
// <sld:Abstract> (v1.0)
description = childElem.firstChild().nodeValue();
}
else if ( childElem.localName() == "Title" )
{
// <sld:Title> (v1.0)
label = childElem.firstChild().nodeValue();
}
else if ( childElem.localName().endsWith( "Symbolizer" ) )
{
// create symbol layers for this symbolizer
QgsSymbolLayerV2Utils::createSymbolLayerV2ListFromSld( childElem, geomType, layers );
}
childElem = childElem.nextSiblingElement();
}
if ( layers.size() == 0 )
return NULL;
// now create the symbol
QgsSymbolV2 *symbol;
switch ( geomType )
{
case QGis::Line:
symbol = new QgsLineSymbolV2( layers );
break;
case QGis::Polygon:
symbol = new QgsFillSymbolV2( layers );
break;
case QGis::Point:
symbol = new QgsMarkerSymbolV2( layers );
break;
default:
QgsDebugMsg( QString( "invalid geometry type: found %1" ).arg( geomType ) );
return NULL;
}
// and finally return the new renderer
return new QgsSingleSymbolRendererV2( symbol );
}
QDomElement QgsSingleSymbolRendererV2::save( QDomDocument& doc )
{
QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
rendererElem.setAttribute( "type", "singleSymbol" );
rendererElem.setAttribute( "symbollevels", ( mUsingSymbolLevels ? "1" : "0" ) );
QgsSymbolV2Map symbols;
symbols["0"] = mSymbol.data();
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( symbols, "symbols", doc );
rendererElem.appendChild( symbolsElem );
QDomElement rotationElem = doc.createElement( "rotation" );
if ( mRotation.data() )
rotationElem.setAttribute( "field", QgsSymbolLayerV2Utils::fieldOrExpressionFromExpression( mRotation.data() ) );
rendererElem.appendChild( rotationElem );
QDomElement sizeScaleElem = doc.createElement( "sizescale" );
if ( mSizeScale.data() )
sizeScaleElem.setAttribute( "field", QgsSymbolLayerV2Utils::fieldOrExpressionFromExpression( mSizeScale.data() ) );
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );
if ( mPaintEffect )
mPaintEffect->saveProperties( doc, rendererElem );
return rendererElem;
}
QgsLegendSymbologyList QgsSingleSymbolRendererV2::legendSymbologyItems( QSize iconSize )
{
QgsLegendSymbologyList lst;
if ( mSymbol.data() )
{
QPixmap pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mSymbol.data(), iconSize );
lst << qMakePair( QString(), pix );
}
return lst;
}
QgsLegendSymbolList QgsSingleSymbolRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
Q_UNUSED( rule );
QgsLegendSymbolList lst;
lst << qMakePair( QString(), mSymbol.data() );
return lst;
}
QgsLegendSymbolListV2 QgsSingleSymbolRendererV2::legendSymbolItemsV2() const
{
QgsLegendSymbolListV2 lst;
if ( mSymbol->type() == QgsSymbolV2::Marker )
{
const QgsMarkerSymbolV2 * symbol = static_cast<const QgsMarkerSymbolV2 *>( mSymbol.data() );
QgsDataDefined sizeDD = symbol->dataDefinedSize();
if ( sizeDD.isActive() && sizeDD.useExpression() )
{
QgsScaleExpression scaleExp( sizeDD.expressionString() );
if ( scaleExp.type() != QgsScaleExpression::Unknown )
{
QgsLegendSymbolItemV2 title( NULL, scaleExp.baseExpression(), 0 );
lst << title;
foreach ( double v, QgsSymbolLayerV2Utils::prettyBreaks( scaleExp.minValue(), scaleExp.maxValue(), 4 ) )
{
QgsLegendSymbolItemV2 si( mSymbol.data(), QString::number( v ), 0 );
QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() );
s->setDataDefinedSize( 0 );
s->setSize( scaleExp.size( v ) );
lst << si;
}
return lst;
}
}
}
lst << QgsLegendSymbolItemV2( mSymbol.data(), QString(), 0 );
return lst;
}
QgsSingleSymbolRendererV2* QgsSingleSymbolRendererV2::convertFromRenderer( const QgsFeatureRendererV2 *renderer )
{
if ( renderer->type() == "singleSymbol" )
{
return dynamic_cast<QgsSingleSymbolRendererV2*>( renderer->clone() );
}
if ( renderer->type() == "pointDisplacement" )
{
const QgsPointDisplacementRenderer* pointDisplacementRenderer = dynamic_cast<const QgsPointDisplacementRenderer*>( renderer );
if ( pointDisplacementRenderer )
return convertFromRenderer( pointDisplacementRenderer->embeddedRenderer() );
}
if ( renderer->type() == "invertedPolygonRenderer" )
{
const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
if ( invertedPolygonRenderer )
return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
}
QgsSymbolV2List symbols = const_cast<QgsFeatureRendererV2 *>( renderer )->symbols();
if ( symbols.size() > 0 )
{
return new QgsSingleSymbolRendererV2( symbols.at( 0 )->clone() );
}
return 0;
}