@@ -55,6 +55,17 @@ bool QgsPropertyTransformer::writeXml( QDomElement& transformerElem, QDomDocumen
55
55
return true ;
56
56
}
57
57
58
+ QgsPropertyTransformer* QgsPropertyTransformer::fromExpression ( const QString& expression, QString& baseExpression, QString& fieldName )
59
+ {
60
+ baseExpression.clear ();
61
+ fieldName.clear ();
62
+
63
+ if ( QgsPropertyTransformer* sizeScale = QgsSizeScaleTransformer::fromExpression ( expression, baseExpression, fieldName ) )
64
+ return sizeScale;
65
+ else
66
+ return nullptr ;
67
+ }
68
+
58
69
bool QgsPropertyTransformer::readXml ( const QDomElement &transformerElem, const QDomDocument &doc )
59
70
{
60
71
Q_UNUSED ( doc );
@@ -195,6 +206,88 @@ QString QgsSizeScaleTransformer::toExpression( const QString& baseExpression ) c
195
206
return QString ();
196
207
}
197
208
209
+ QgsSizeScaleTransformer* QgsSizeScaleTransformer::fromExpression ( const QString& expression, QString& baseExpression, QString& fieldName )
210
+ {
211
+ bool ok = false ;
212
+
213
+ ScaleType type = Linear;
214
+ double nullSize = 0.0 ;
215
+ double exponent = 1.0 ;
216
+
217
+ baseExpression.clear ();
218
+ fieldName.clear ();
219
+
220
+ QgsExpression e ( expression );
221
+
222
+ if ( !e.rootNode () )
223
+ return nullptr ;
224
+
225
+ const QgsExpression::NodeFunction * f = dynamic_cast <const QgsExpression::NodeFunction*>( e.rootNode () );
226
+ if ( !f )
227
+ return nullptr ;
228
+
229
+ QList<QgsExpression::Node*> args = f->args ()->list ();
230
+
231
+ // the scale function may be enclosed in a coalesce(expr, 0) to avoid NULL value
232
+ // to be drawn with the default size
233
+ if ( " coalesce" == QgsExpression::Functions ()[f->fnIndex ()]->name () )
234
+ {
235
+ f = dynamic_cast <const QgsExpression::NodeFunction*>( args[0 ] );
236
+ if ( !f )
237
+ return nullptr ;
238
+ nullSize = QgsExpression ( args[1 ]->dump () ).evaluate ().toDouble ( &ok );
239
+ if ( ! ok )
240
+ return nullptr ;
241
+ args = f->args ()->list ();
242
+ }
243
+
244
+ if ( " scale_linear" == QgsExpression::Functions ()[f->fnIndex ()]->name () )
245
+ {
246
+ type = Linear;
247
+ }
248
+ else if ( " scale_exp" == QgsExpression::Functions ()[f->fnIndex ()]->name () )
249
+ {
250
+ exponent = QgsExpression ( args[5 ]->dump () ).evaluate ().toDouble ( &ok );
251
+ if ( ! ok )
252
+ return nullptr ;
253
+ if ( qgsDoubleNear ( exponent, 0.57 , 0.001 ) )
254
+ type = Flannery;
255
+ else if ( qgsDoubleNear ( exponent, 0.5 , 0.001 ) )
256
+ type = Area;
257
+ else
258
+ type = Exponential;
259
+ }
260
+ else
261
+ {
262
+ return nullptr ;
263
+ }
264
+
265
+ bool expOk = true ;
266
+ double minValue = QgsExpression ( args[1 ]->dump () ).evaluate ().toDouble ( &ok );
267
+ expOk &= ok;
268
+ double maxValue = QgsExpression ( args[2 ]->dump () ).evaluate ().toDouble ( &ok );
269
+ expOk &= ok;
270
+ double minSize = QgsExpression ( args[3 ]->dump () ).evaluate ().toDouble ( &ok );
271
+ expOk &= ok;
272
+ double maxSize = QgsExpression ( args[4 ]->dump () ).evaluate ().toDouble ( &ok );
273
+ expOk &= ok;
274
+
275
+ if ( !expOk )
276
+ {
277
+ return nullptr ;
278
+ }
279
+
280
+ if ( args[0 ]->nodeType () == QgsExpression::ntColumnRef )
281
+ {
282
+ fieldName = static_cast < QgsExpression::NodeColumnRef* >( args[0 ] )->name ();
283
+ }
284
+ else
285
+ {
286
+ baseExpression = args[0 ]->dump ();
287
+ }
288
+ return new QgsSizeScaleTransformer ( type, minValue, maxValue, minSize, maxSize, nullSize, exponent );
289
+ }
290
+
198
291
199
292
//
200
293
// QgsColorRampTransformer
0 commit comments