Skip to content

Commit 5297df7

Browse files
aaimerldhont
authored andcommitted
Export map level scale based dependencies in most vector symbology
1 parent 760036a commit 5297df7

25 files changed

+2910
-67
lines changed

python/core/qgsvectorlayer.sip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,15 @@ class QgsVectorLayer : QgsMapLayer
733733
*/
734734
bool writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
735735

736-
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
736+
/** Write just the style information for the layer into the document
737+
* @param node the node that will have the style element added to it.
738+
* @param doc the document that will have the QDomNode added.
739+
* @param errorMessage reference to string that will be updated with any error messages
740+
* @return true in case of success.
741+
*/
742+
bool writeStyle( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
743+
744+
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap& props ) const;
737745
bool readSld( const QDomNode& node, QString& errorMessage );
738746

739747
/**
@@ -1820,4 +1828,3 @@ class QgsVectorLayer : QgsMapLayer
18201828

18211829

18221830
};
1823-

python/core/symbology-ng/qgssymbollayerv2utils.sip

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class QgsSymbolLayerV2Utils
204204
static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString& geomFunc );
205205
static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
206206

207+
static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString& function );
207208
static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString& function );
208209
static bool functionFromSldElement( QDomElement &element, QString &function );
209210

@@ -412,4 +413,34 @@ class QgsSymbolLayerV2Utils
412413
*/
413414
static QList<double> prettyBreaks( double minimum, double maximum, int classes );
414415

416+
/** Rescales the given size based on the uomScale found in the props, if any is found, otherwise
417+
* returns the value un-modified
418+
* @note added in 3.0
419+
*/
420+
static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap& props );
421+
422+
/** Rescales the given point based on the uomScale found in the props, if any is found, otherwise
423+
* returns a copy of the original point
424+
* @note added in 3.0
425+
*/
426+
static QPointF rescaleUom( const QPointF& point, QgsUnitTypes::RenderUnit unit, const QgsStringMap& props ) /PyName=rescalePointUom/;
427+
428+
/** Rescales the given array based on the uomScale found in the props, if any is found, otherwise
429+
* returns a copy of the original point
430+
* @note added in 3.0
431+
*/
432+
static QVector<qreal> rescaleUom( const QVector<qreal>& array, QgsUnitTypes::RenderUnit unit, const QgsStringMap& props ) /PyName=rescaleArrayUom/;
433+
434+
/**
435+
* Checks if the properties contain scaleMinDenom and scaleMaxDenom, if available, they are added into the SE Rule element
436+
* @note added in 3.0
437+
*/
438+
static void applyScaleDependency( QDomDocument& doc, QDomElement& ruleElem, QgsStringMap& props );
439+
440+
/**
441+
* Merges the local scale limits, if any, with the ones already in the map, if any
442+
* @note added in 3.0
443+
*/
444+
static void mergeScaleDependencies( int mScaleMinDenom, int mScaleMaxDenom, QgsStringMap& props );
445+
415446
};

src/core/qgsmaplayer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,13 @@ void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg )
14711471
return;
14721472
}
14731473

1474-
if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg ) )
1474+
QgsStringMap props;
1475+
if ( hasScaleBasedVisibility() )
1476+
{
1477+
props[ "scaleMinDenom" ] = QString::number( mMinScale );
1478+
props[ "scaleMaxDenom" ] = QString::number( mMaxScale );
1479+
}
1480+
if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg, props ) )
14751481
{
14761482
errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
14771483
return;

src/core/qgsvectorlayer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,8 +2107,12 @@ bool QgsVectorLayer::readSld( const QDomNode& node, QString& errorMessage )
21072107
return true;
21082108
}
21092109

2110-
21112110
bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const
2111+
{
2112+
return writeSld( node, doc, errorMessage, QgsStringMap() );
2113+
}
2114+
2115+
bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap props ) const
21122116
{
21132117
Q_UNUSED( errorMessage );
21142118

@@ -2117,9 +2121,15 @@ bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& error
21172121
nameNode.appendChild( doc.createTextNode( name() ) );
21182122
node.appendChild( nameNode );
21192123

2124+
QgsStringMap localProps = QgsStringMap( props );
2125+
if ( hasScaleBasedVisibility() )
2126+
{
2127+
QgsSymbolLayerUtils::mergeScaleDependencies( minimumScale(), maximumScale(), localProps );
2128+
}
2129+
21202130
if ( hasGeometryType() )
21212131
{
2122-
node.appendChild( mRendererV2->writeSld( doc, name() ) );
2132+
node.appendChild( mRendererV2->writeSld( doc, name(), localProps ) );
21232133
}
21242134
return true;
21252135
}

src/core/qgsvectorlayer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,17 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
841841
bool writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const override;
842842

843843
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
844+
845+
/**
846+
* Writes the symbology of the layer into the document provided in SLD 1.1 format
847+
* @param node the node that will have the style element added to it.
848+
* @param doc the document that will have the QDomNode added.
849+
* @param errorMessage reference to string that will be updated with any error messages
850+
* @param props a open ended set of properties that can drive/inform the SLD encoding
851+
* @return true in case of success
852+
*/
853+
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap props = QgsStringMap() ) const;
854+
844855
bool readSld( const QDomNode& node, QString& errorMessage ) override;
845856

846857
/**

src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ void QgsRendererCategoryV2::toSld( QDomDocument &doc, QDomElement &element, QgsS
141141
mValue.toString().replace( '\'', "''" ) );
142142
QgsSymbolLayerV2Utils::createFunctionElement( doc, ruleElem, filterFunc );
143143

144+
// add the mix/max scale denoms if we got any from the callers
145+
QgsSymbolLayerUtils::applyScaleDependency( doc, ruleElem, props );
146+
144147
mSymbol->toSld( doc, ruleElem, props );
145148
}
146149

@@ -528,9 +531,8 @@ QgsCategorizedSymbolRendererV2* QgsCategorizedSymbolRendererV2::clone() const
528531
return r;
529532
}
530533

531-
void QgsCategorizedSymbolRendererV2::toSld( QDomDocument &doc, QDomElement &element ) const
534+
void QgsCategorizedSymbolRendererV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
532535
{
533-
QgsStringMap props;
534536
props[ "attribute" ] = mAttrName;
535537
if ( mRotation.data() )
536538
props[ "angle" ] = mRotation->expression();

src/core/symbology-ng/qgscategorizedsymbolrendererv2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
9494

9595
virtual QgsCategorizedSymbolRendererV2* clone() const override;
9696

97-
virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
97+
virtual void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props = QgsStringMap() ) const override;
9898

9999
//! returns bitwise OR-ed capabilities of the renderer
100100
virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,8 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::clone() const
559559
return r;
560560
}
561561

562-
void QgsGraduatedSymbolRendererV2::toSld( QDomDocument& doc, QDomElement &element ) const
562+
void QgsGraduatedSymbolRendererV2::toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props ) const
563563
{
564-
QgsStringMap props;
565564
props[ "attribute" ] = mAttrName;
566565
props[ "method" ] = graduatedMethodStr( mGraduatedMethod );
567566
if ( mRotation.data() )

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
143143

144144
virtual QgsGraduatedSymbolRendererV2* clone() const override;
145145

146-
virtual void toSld( QDomDocument& doc, QDomElement &element ) const override;
146+
virtual void toSld( QDomDocument& doc, QDomElement &element, QgsStringMap props = QgsStringMap() ) const override;
147147

148148
//! returns bitwise OR-ed capabilities of the renderer
149149
virtual int capabilities() override { return SymbolLevels | RotationField | Filter; }

src/core/symbology-ng/qgslinesymbollayerv2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ void QgsMarkerLineSymbolLayerV2::toSld( QDomDocument &doc, QDomElement &element,
14461446
if ( !gap.isEmpty() )
14471447
{
14481448
QDomElement gapElem = doc.createElement( "se:Gap" );
1449-
QgsSymbolLayerV2Utils::createFunctionElement( doc, gapElem, gap );
1449+
QgsSymbolLayerV2Utils::createExpressionElement( doc, gapElem, gap );
14501450
graphicStrokeElem.appendChild( gapElem );
14511451
}
14521452

0 commit comments

Comments
 (0)