Skip to content

Commit 2b9cb32

Browse files
aaimerldhont
authored andcommitted
Export map level scale based dependencies in most vector symbology
1 parent 2f268d4 commit 2b9cb32

25 files changed

+2902
-66
lines changed

python/core/qgsvectorlayer.sip

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class QgsVectorLayer : QgsMapLayer
564564
*/
565565
bool writeStyle( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
566566

567-
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
567+
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap& props ) const;
568568
bool readSld( const QDomNode& node, QString& errorMessage );
569569

570570
/**
@@ -1749,4 +1749,3 @@ class QgsVectorLayer : QgsMapLayer
17491749

17501750

17511751
};
1752-

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

@@ -424,4 +425,34 @@ class QgsSymbolLayerV2Utils
424425
*/
425426
static QList<double> prettyBreaks( double minimum, double maximum, int classes );
426427

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

src/core/qgsmaplayer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,13 @@ void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg )
14811481
return;
14821482
}
14831483

1484-
if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg ) )
1484+
QgsStringMap props;
1485+
if ( hasScaleBasedVisibility() )
1486+
{
1487+
props[ "scaleMinDenom" ] = QString::number( mMinScale );
1488+
props[ "scaleMaxDenom" ] = QString::number( mMaxScale );
1489+
}
1490+
if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg, props ) )
14851491
{
14861492
errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
14871493
return;

src/core/qgsvectorlayer.cpp

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

2326-
23272326
bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const
2327+
{
2328+
return writeSld( node, doc, errorMessage, QgsStringMap() );
2329+
}
2330+
2331+
bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap props ) const
23282332
{
23292333
Q_UNUSED( errorMessage );
23302334

@@ -2333,9 +2337,15 @@ bool QgsVectorLayer::writeSld( QDomNode& node, QDomDocument& doc, QString& error
23332337
nameNode.appendChild( doc.createTextNode( name() ) );
23342338
node.appendChild( nameNode );
23352339

2340+
QgsStringMap localProps = QgsStringMap( props );
2341+
if ( hasScaleBasedVisibility() )
2342+
{
2343+
QgsSymbolLayerUtils::mergeScaleDependencies( minimumScale(), maximumScale(), localProps );
2344+
}
2345+
23362346
if ( hasGeometryType() )
23372347
{
2338-
node.appendChild( mRendererV2->writeSld( doc, name() ) );
2348+
node.appendChild( mRendererV2->writeSld( doc, name(), localProps ) );
23392349
}
23402350
return true;
23412351
}

src/core/qgsvectorlayer.h

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

938938
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
939+
940+
/**
941+
* Writes the symbology of the layer into the document provided in SLD 1.1 format
942+
* @param node the node that will have the style element added to it.
943+
* @param doc the document that will have the QDomNode added.
944+
* @param errorMessage reference to string that will be updated with any error messages
945+
* @param props a open ended set of properties that can drive/inform the SLD encoding
946+
* @return true in case of success
947+
*/
948+
bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage, QgsStringMap props = QgsStringMap() ) const;
949+
939950
bool readSld( const QDomNode& node, QString& errorMessage ) override;
940951

941952
/**

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

@@ -517,9 +520,8 @@ QgsCategorizedSymbolRendererV2* QgsCategorizedSymbolRendererV2::clone() const
517520
return r;
518521
}
519522

520-
void QgsCategorizedSymbolRendererV2::toSld( QDomDocument &doc, QDomElement &element ) const
523+
void QgsCategorizedSymbolRendererV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
521524
{
522-
QgsStringMap props;
523525
props[ "attribute" ] = mAttrName;
524526
if ( mRotation.data() )
525527
props[ "angle" ] = mRotation->expression();

src/core/symbology-ng/qgscategorizedsymbolrendererv2.h

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

100100
virtual QgsCategorizedSymbolRendererV2* clone() const override;
101101

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

104104
//! returns bitwise OR-ed capabilities of the renderer
105105
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
@@ -152,7 +152,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
152152

153153
virtual QgsGraduatedSymbolRendererV2* clone() const override;
154154

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

157157
//! returns bitwise OR-ed capabilities of the renderer
158158
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
@@ -1450,7 +1450,7 @@ void QgsMarkerLineSymbolLayerV2::toSld( QDomDocument &doc, QDomElement &element,
14501450
if ( !gap.isEmpty() )
14511451
{
14521452
QDomElement gapElem = doc.createElement( "se:Gap" );
1453-
QgsSymbolLayerV2Utils::createFunctionElement( doc, gapElem, gap );
1453+
QgsSymbolLayerV2Utils::createExpressionElement( doc, gapElem, gap );
14541454
graphicStrokeElem.appendChild( gapElem );
14551455
}
14561456

0 commit comments

Comments
 (0)