33 #include <QDomDocument>
34 #include <QDomElement>
49 : mLowerValue( lowerValue )
50 , mUpperValue( upperValue )
58 : mLowerValue( range.mLowerValue )
59 , mUpperValue( range.mUpperValue )
60 , mSymbol( range.mSymbol.data() ? range.mSymbol->clone() : NULL )
61 , mLabel( range.mLabel )
62 , mRender( range.mRender )
149 QString attrName = props[
"attribute" ];
171 mSymbol->toSld( doc, ruleElem, props );
180 mFormat(
" %1 - %2 " ),
182 mTrimTrailingZeroes( false ),
185 mReTrailingZeroes(
"[.,]?0*$" ),
186 mReNegativeZero(
"^\\-0(?:[.,]0*)?$" )
191 mReTrailingZeroes(
"[.,]?0*$" ),
192 mReNegativeZero(
"^\\-0(?:[.,]0*)?$" )
210 return !( *
this == other );
220 while ( precision < 0 )
241 valueStr = valueStr.
mid( 1 );
247 if ( valueStr ==
"-0" )
249 if ( valueStr !=
"0" )
261 return legend.
replace(
"%1", lowerStr ).
replace(
"%2", upperStr );
267 element.
attribute(
"prefix",
" " ) +
"%1" +
268 element.
attribute(
"separator",
" - " ) +
"%2" +
286 , mAttrName( attrName )
289 , mInvertedColorRamp( false )
291 , mGraduatedMethod( GraduatedColor )
308 if ( it->lowerValue() <= value && it->upperValue() >= value )
323 if ( symbol == NULL )
341 markerSymbol->
setSize( sizeScale * static_cast<QgsMarkerSymbolV2*>( symbol )->size() );
347 lineSymbol->
setWidth( sizeScale * static_cast<QgsLineSymbolV2*>( symbol )->width() );
357 if ( mAttrNum < 0 || mAttrNum >= attrs.
count() )
393 it->symbol()->startRender( context, &fields );
415 it->symbol()->stopRender( context );
422 it2.
value()->stopRender( context );
454 return attributes.
toList();
459 if ( rangeIndex < 0 || rangeIndex >=
mRanges.
size() )
461 mRanges[rangeIndex].setSymbol( symbol );
467 if ( rangeIndex < 0 || rangeIndex >=
mRanges.
size() )
469 mRanges[rangeIndex].setLabel( label );
475 if ( rangeIndex < 0 || rangeIndex >=
mRanges.
size() )
486 if ( rangeIndex < 0 || rangeIndex >=
mRanges.
size() )
497 if ( rangeIndex < 0 || rangeIndex >=
mRanges.
size() )
499 mRanges[rangeIndex].setRenderState( value );
536 props[
"angle" ] =
mRotation->expression();
544 it->toSld( doc, element, catProps );
566 double step = ( maximum - minimum ) / classes;
569 double value = minimum;
571 for (
int i = 0; i < classes; i++ )
579 breaks[classes-1] = maximum;
600 if ( !values.
count() )
603 int n = values.
count();
604 double Xq = n > 0 ? values[0] : 0.0;
607 for (
int i = 1; i < classes; i++ )
611 double q = i / ( double ) classes;
612 double a = q * ( n - 1 );
613 int aa = ( int )( a );
616 Xq = ( 1 - r ) * values[aa] + r * values[aa+1];
621 breaks.
append( values[ n-1 ] );
637 if ( !values.
count() )
642 int n = values.
count();
643 double minimum = values[0];
644 double maximum = values[0];
646 for (
int i = 0; i < n; i++ )
649 minimum = qMin( values[i], minimum );
650 maximum = qMax( values[i], maximum );
652 mean = mean / ( double ) n;
655 for (
int i = 0; i < n; i++ )
657 sd = values[i] - mean;
660 stdDev = sqrt( stdDev / n );
663 for (
int i = 0; i < breaks.
count(); i++ )
665 labels.
append( breaks[i] );
666 breaks[i] = ( breaks[i] * stdDev ) + mean;
673 double minimum,
double maximum,
674 int maximumSize = 1000 )
686 if ( !values.
count() )
694 if ( classes >= values.
size() )
702 if ( values.
size() > maximumSize )
708 sample.
resize( qMax( maximumSize, values.
size() / 10 ) );
713 sample[ 0 ] = minimum;
714 sample[ 1 ] = maximum;
715 for (
int i = 2; i < sample.
size(); i++ )
719 int j = floor( r / RAND_MAX * ( values.
size() - 1 ) );
720 sample[ i ] = values[ j ];
728 int n = sample.
size();
736 for (
int i = 0; i <= n; i++ )
738 matrixOne[i].
resize( classes + 1 );
739 matrixTwo[i].
resize( classes + 1 );
742 for (
int i = 1; i <= classes; i++ )
746 matrixTwo[0][i] = 0.0;
747 for (
int j = 2; j <= n; j++ )
753 for (
int l = 2; l <= n; l++ )
761 for (
int m = 1; m <= l; m++ )
765 double val = sample[ i3 - 1 ];
771 v = s2 - ( s1 * s1 ) / (
double ) w;
775 for (
int j = 2; j <= classes; j++ )
777 if ( matrixTwo[l][j] >= v + matrixTwo[i4][j - 1] )
779 matrixOne[l][j] = i4;
780 matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
790 breaks[classes-1] = sample[n-1];
792 for (
int j = classes, k = n; j >= 2; j-- )
794 int id = matrixOne[k][j] - 1;
795 breaks[j - 2] = sample[id];
796 k = matrixOne[k][j] - 1;
845 bool valuesLoaded =
false;
859 minimum = values.
first();
860 maximum = values.
last();
876 else if ( mode ==
Pretty )
893 else if ( mode ==
Jenks )
897 else if ( mode ==
StdDev )
907 double lower, upper = minimum;
926 else if ( i == labels.
count() - 1 )
948 if ( symbolsElem.
isNull() )
952 if ( rangesElem.
isNull() )
959 while ( !rangeElem.
isNull() )
961 if ( rangeElem.
tagName() ==
"range" )
967 bool render = rangeElem.
attribute(
"render",
"true" ) !=
"false";
968 if ( symbolMap.
contains( symbolName ) )
982 if ( attrMethod.
length() )
996 if ( !sourceSymbolElem.
isNull() )
999 if ( sourceSymbolMap.
contains(
"0" ) )
1008 if ( !sourceColorRampElem.
isNull() && sourceColorRampElem.
attribute(
"name" ) ==
"[source]" )
1012 if ( !invertedColorRampElem.
isNull() )
1018 if ( !modeElem.
isNull() )
1021 if ( modeString ==
"equal" )
1023 else if ( modeString ==
"quantile" )
1025 else if ( modeString ==
"jenks" )
1027 else if ( modeString ==
"stddev" )
1029 else if ( modeString ==
"pretty" )
1064 if ( ! labelFormatElem.
isNull() )
1077 rendererElem.
setAttribute(
"type",
"graduatedSymbol" );
1132 modeString =
"equal";
1134 modeString =
"quantile";
1136 modeString =
"jenks";
1138 modeString =
"stddev";
1140 modeString =
"pretty";
1166 return rendererElem;
1174 for (
int i = 0; i < count; i++ )
1178 lst << qMakePair( range.
label(), pix );
1219 s->setSize( exp.size( v ) );
1235 Q_UNUSED( scaleDenominator );
1242 lst << qMakePair( range.
label(), range.
symbol() );
1269 double min = DBL_MAX;
1274 sz = static_cast< QgsMarkerSymbolV2 * >(
mRanges[i].symbol() )->size();
1276 sz = static_cast< QgsLineSymbolV2 * >(
mRanges[i].symbol() )->width();
1277 min = qMin( sz, min );
1284 double max = DBL_MIN;
1289 sz = static_cast< QgsMarkerSymbolV2 * >(
mRanges[i].symbol() )->size();
1291 sz = static_cast< QgsLineSymbolV2 * >(
mRanges[i].symbol() )->width();
1292 max = qMax( sz, max );
1303 ? minSize + i * ( maxSize - minSize ) / (
mRanges.
count() - 1 )
1304 : .5 * ( maxSize + minSize );
1306 static_cast< QgsMarkerSymbolV2 * >( symbol.
data() )->setSize( size );
1308 static_cast< QgsLineSymbolV2 * >( symbol.
data() )->setWidth( size );
1359 static_cast<QgsMarkerSymbolV2 *>( symbol.data() )->setSize(
1360 static_cast<QgsMarkerSymbolV2 *>( range.
symbol() )->size() );
1362 static_cast<QgsLineSymbolV2 *>( symbol.data() )->setWidth(
1363 static_cast<QgsLineSymbolV2 *>( range.
symbol() )->width() );
1423 return mRanges[ index ].renderState();
1476 if ( updateSymbols )
1521 double minClassRange = 0.0;
1524 double range = it->upperValue() - it->lowerValue();
1527 if ( minClassRange == 0.0 || range < minClassRange )
1528 minClassRange = range;
1530 if ( minClassRange <= 0.0 )
1537 double nextDpMinRange = 0.0000000099;
1538 while ( ndp > 0 && nextDpMinRange < minClassRange )
1541 nextDpMinRange *= 10.0;
1567 if ( order == Qt::AscendingOrder )
1583 if ( it == sortedRanges.
constEnd() )
1586 if (( *it ).upperValue() < ( *it ).lowerValue() )
1589 double prevMax = ( *it ).upperValue();
1592 for ( ; it != sortedRanges.
constEnd(); ++it )
1594 if (( *it ).upperValue() < ( *it ).lowerValue() )
1597 if (( *it ).lowerValue() < prevMax )
1600 prevMax = ( *it ).upperValue();
1611 if ( it == sortedRanges.
constEnd() )
1614 double prevMax = ( *it ).upperValue();
1617 for ( ; it != sortedRanges.
constEnd(); ++it )
1622 prevMax = ( *it ).upperValue();
1639 if ( order == Qt::AscendingOrder )
1651 if ( renderer->
type() ==
"graduatedSymbol" )
1655 if ( renderer->
type() ==
"pointDisplacement" )
1658 if ( pointDisplacementRenderer )
1661 if ( renderer->
type() ==
"invertedPolygonRenderer" )
1664 if ( invertedPolygonRenderer )
1674 if ( symbols.
size() > 0 )
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, const QString &tagName, QDomDocument &doc)
Class for parsing and evaluation of expressions (formerly called "search strings").
QList< QgsRendererRangeV2 > QgsRangeList
static QgsSymbolV2Map loadSymbols(QDomElement &element)
static QList< double > _calcJenksBreaks(QList< double > values, int classes, double minimum, double maximum, int maximumSize=1000)
#define RENDERER_TAG_NAME
virtual void toSld(QDomDocument &doc, QDomElement &element) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
QString & append(QChar ch)
void setLowerValue(double lowerValue)
QgsGraduatedSymbolRendererV2(const QString &attrName=QString(), const QgsRangeList &ranges=QgsRangeList())
void setSymbolSizes(double minSize, double maxSize)
set varying symbol size for classes
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
void setInvertedColorRamp(bool inverted)
QStringList referencedColumns() const
Get list of columns referenced by the expression.
void addBreak(double breakValue, bool updateSymbols=true)
Add a breakpoint by splitting existing classes so that the specified value becomes a break between tw...
void setDataDefinedAngle(const QgsDataDefined &dd)
Set data defined angle for whole symbol (including all symbol layers).
A container class for data source field mapping or expression.
void setLabelFormat(const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges=false)
Set the label format used to generate default classification labels.
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about 'classes' equally spaced round values which cover the range of values fr...
bool labelLessThan(const QgsRendererRangeV2 &r1, const QgsRendererRangeV2 &r2)
bool contains(const Key &key) const
static QgsGraduatedSymbolRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsGraduatedSymbolRendererV2 from an existing renderer.
static QgsVectorColorRampV2 * loadColorRamp(QDomElement &element)
int localeAwareCompare(const QString &other) const
void updateClasses(QgsVectorLayer *vlayer, Mode mode, int nclasses)
Recalculate classes for a layer.
static QList< double > _calcStdDevBreaks(QList< double > values, int classes, QList< double > &labels)
virtual QString dump() const override
for debugging
QDomNode appendChild(const QDomNode &newChild)
QScopedPointer< QgsVectorColorRampV2 > mSourceColorRamp
QVariant maximumValue(int index)
Returns maximum value for an attribute column or invalid variant in case of error.
QSet< QString > usedAttributes() const
QString field() const
Get the field which this QgsDataDefined represents.
QString attribute(const QString &name, const QString &defValue) const
void updateSymbols(QgsSymbolV2 *sym)
Update all the symbols but leave breaks and colors.
void setDataDefinedSize(const QgsDataDefined &dd)
Set data defined size for whole symbol (including all symbol layers).
static QDomElement saveColorRamp(const QString &name, QgsVectorColorRampV2 *ramp, QDomDocument &doc)
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
double rendererScale() const
virtual QgsSymbolV2 * clone() const =0
Class storing parameters of a scale expression, which is a subclass of QgsExpression for expressions ...
virtual QgsFeatureRendererV2 * clone() const override
QgsDataDefined dataDefinedSize() const
Returns data defined size for whole symbol (including all symbol layers).
static QgsSymbolV2::ScaleMethod decodeScaleMethod(const QString &str)
virtual bool legendSymbolItemChecked(const QString &key) override
item in symbology was checked
bool updateRangeRenderState(int rangeIndex, bool render)
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
To be overridden.
const T & at(int i) const
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
void setSizeScaleField(const QString &fieldOrExpression)
QDomElement nextSiblingElement(const QString &tagName) const
double minSymbolSize() const
return the min symbol size when graduated by size
Container of fields for a vector layer.
QString expressionString() const
Returns the expression string of this QgsDataDefined.
void move(int from, int to)
QVector< T > toVector() const
virtual ~QgsGraduatedSymbolRendererV2()
QHash< QgsSymbolV2 *, QgsSymbolV2 * > mTempSymbols
temporary symbols, used for data-defined rotation and scaling
void calculateLabelPrecision(bool updateRanges=true)
Reset the label decimal places to a numberbased on the minimum class interval.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule=QString()) override
return a list of item text / symbol
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
double toDouble(bool *ok) const
QVariant minimumValue(int index)
Returns minimum value for an attribute column or invalid variant in case of error.
void setValue(const T &value) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
static const char * graduatedMethodStr(GraduatedMethod method)
QgsVectorColorRampV2 * sourceColorRamp()
QgsPaintEffect * mPaintEffect
void setWidth(double width)
void setRenderState(bool render)
Q_DECL_DEPRECATED QList< double > getDataValues(QgsVectorLayer *vlayer)
Evaluates the data expression and returns the list of values from the layer.
QScopedPointer< QgsSymbolV2 > mSymbol
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
virtual void stopRender(QgsRenderContext &context) override
GraduatedMethod mGraduatedMethod
double upperValue() const
QScopedPointer< QgsExpression > mExpression
void setColor(const QColor &color)
bool rangesHaveGaps() const
Tests whether classes assigned to the renderer have gaps between the ranges.
virtual QgsFeatureRendererV2 * clone() const =0
QString number(int n, int base)
int count(const T &value) const
static QgsGraduatedSymbolRendererV2 * createRenderer(QgsVectorLayer *vlayer, const QString &attrName, int classes, Mode mode, QgsSymbolV2 *symbol, QgsVectorColorRampV2 *ramp, bool inverted=false, const QgsRendererRangeV2LabelFormat &legendFormat=QgsRendererRangeV2LabelFormat())
void setGraduatedMethod(GraduatedMethod method)
set the method used for graduation (either size or color)
void append(const T &value)
QgsSymbolV2::ScaleMethod scaleMethod() const
void sortByLabel(Qt::SortOrder order=Qt::AscendingOrder)
static QList< double > _calcQuantileBreaks(QList< double > values, int classes)
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted...
void startRender(QgsRenderContext &context, const QgsFields *fields=0)
QgsAttributes attributes() const
Returns the feature's attributes.
void setAttribute(const QString &name, const QString &value)
#define DEFAULT_SCALE_METHOD
virtual QgsVectorColorRampV2 * clone() const =0
void swap(QgsRendererRangeV2 &other)
int toInt(bool *ok, int base) const
QgsSymbolV2 * sourceSymbol()
Q_DECL_DEPRECATED QString rotationField() const override
return rotation field name (or empty string if not set or not supported by renderer) ...
QgsSymbolV2 * symbol() const
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
void setAngle(double angle)
void setSourceSymbol(QgsSymbolV2 *sym)
bool updateRangeLowerValue(int rangeIndex, double value)
void setSize(double size)
void toSld(QDomDocument &doc, QDomElement &element, QgsStringMap props) const
QgsRendererRangeV2LabelFormat mLabelFormat
QGis::GeometryType geometryType() const
Returns point, line or polygon.
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
for symbol levels
void setUpperValue(double upperValue)
virtual QDomElement save(QDomDocument &doc) override
store renderer info to XML element
bool operator<(const QgsRendererRangeV2 &other) const
QgsSymbolV2 * symbolForValue(double value)
static void convertSymbolSizeScale(QgsSymbolV2 *symbol, QgsSymbolV2::ScaleMethod method, const QString &field)
bool labelGreaterThan(const QgsRendererRangeV2 &r1, const QgsRendererRangeV2 &r2)
QgsFeatureRendererV2 * embeddedRenderer() const
bool useExpression() const
Returns if the field or the expression part is active.
QDomText createTextNode(const QString &value)
bool valueLessThan(const QgsRendererRangeV2 &r1, const QgsRendererRangeV2 &r2)
const T value(const Key &key) const
QList< double > getDoubleValues(const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, int *nullCount=0)
Fetches all double values from a specified field name or expression.
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
void sortByValue(Qt::SortOrder order=Qt::AscendingOrder)
QgsExpressionContext & expressionContext()
Gets the expression context.
A renderer that automatically displaces points with the same position.
virtual bool legendSymbolItemsCheckable() const override
items of symbology items in legend should be checkable
void setUsingSymbolLevels(bool usingSymbolLevels)
QString & replace(int position, int n, QChar after)
bool rangesOverlap() const
Tests whether classes assigned to the renderer have ranges which overlap.
static QList< double > _calcEqualIntervalBreaks(double minimum, double maximum, int classes)
const T & at(int i) const
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
int ANALYSIS_EXPORT lower(int n, int i)
Lower function.
void moveClass(int from, int to)
Moves the category at index position from to index position to.
Contains information about the context of a rendering operation.
const QgsFeatureRendererV2 * embeddedRenderer() const
int mAttrNum
attribute index (derived from attribute name in startRender)
QList< T > toList() const
QgsDataDefined dataDefinedAngle() const
Returns data defined angle for whole symbol (including all symbol layers).
QString mid(int position, int n) const
void copyPaintEffect(QgsFeatureRendererV2 *destRenderer) const
Copies paint effect of this renderer to another renderer.
static bool createFunctionElement(QDomDocument &doc, QDomElement &element, const QString &function)
QSet< T > & unite(const QSet< T > &other)
static QgsSymbolV2 * defaultSymbol(QGis::GeometryType geomType)
return new default symbol for specified geometry type
static QgsExpression * fieldOrExpressionToExpression(const QString &fieldOrExpression)
Return a new valid expression instance for given field or expression string.
Q_DECL_DEPRECATED void setRotationField(const QString &fieldOrExpression) override
sets rotation field of renderer (if supported by the renderer)
void insert(int i, const T &value)
double lowerValue() const
double maxSymbolSize() const
return the max symbol size when graduated by size
const QgsRangeList & ranges() const
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const
Return a list of symbology items for the legend.
QString sizeScaleField() const
static QString encodeScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
QDomElement firstChildElement(const QString &tagName) const
static QString fieldOrExpressionFromExpression(QgsExpression *expression)
Return a field name if the whole expression is just a name of the field .
bool usingSymbolLevels() const
void setScaleMethodToSymbol(QgsSymbolV2 *symbol, int scaleMethod)
void setSymbol(QgsSymbolV2 *s)
int count(const T &value) const
bool updateRangeSymbol(int rangeIndex, QgsSymbolV2 *symbol)
virtual void checkLegendSymbolItem(const QString &key, bool state=true) override
item in symbology was checked
void setSourceColorRamp(QgsVectorColorRampV2 *ramp)
Sets the source color ramp.
QScopedPointer< QgsSymbolV2 > mSourceSymbol
QList< T > toList() const
void setRenderHints(int hints)
static void clearSymbolMap(QgsSymbolV2Map &symbols)
bool updateRangeUpperValue(int rangeIndex, double value)
QScopedPointer< QgsExpression > mRotation
double toDouble(bool *ok) const
virtual QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
Return symbol for feature.
iterator insert(const Key &key, const T &value)
QgsSymbolV2::ScaleMethod mScaleMethod
static QgsFeatureRendererV2 * create(QDomElement &element)
create renderer from XML element
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize) override
return a list of symbology items for the legend
void setLabel(const QString &label)
bool hasDefaultValues() const
Returns whether the data defined container is set to all the default values, ie, disabled, with empty expression and no assigned field.
virtual QList< QString > usedAttributes() override
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
const_iterator constEnd() const
QScopedPointer< QgsExpression > mSizeScale
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
QgsRendererRangeV2 & operator=(QgsRendererRangeV2 range)
static QPixmap symbolPreviewPixmap(QgsSymbolV2 *symbol, QSize size, QgsRenderContext *customContext=0)
void insert(const T &value)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
void deleteClass(int idx)
bool exactMatch(const QString &str) const
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
QgsLegendSymbolListV2 legendSymbolItemsV2() const override
void updateColorRamp(QgsVectorColorRampV2 *ramp=0, bool inverted=false)
Update the color ramp used.
void addClass(QgsSymbolV2 *symbol)
bool valueGreaterThan(const QgsRendererRangeV2 &r1, const QgsRendererRangeV2 &r2)
const T value(const Key &key) const
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
Needs to be called when a new render cycle is started.
static void convertSymbolRotation(QgsSymbolV2 *symbol, const QString &field)
bool updateRangeLabel(int rangeIndex, const QString &label)