QGIS API Documentation 3.43.0-Master (0cdc48caa8d)
qgsmapsettingsutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapsettingsutils.cpp
3 -------------------
4 begin : May 2017
5 copyright : (C) 2017 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsmapsettings.h"
19#include "qgsmapsettingsutils.h"
20#include "qgspallabeling.h"
21#include "qgstextformat.h"
22#include "qgsvectorlayer.h"
25
26#include <QString>
27
29{
30 QSet< QString > layers;
31
32 QgsTextFormat layerFormat;
33 const auto constLayers = mapSettings.layers();
34 for ( QgsMapLayer *layer : constLayers )
35 {
36 if ( layer && layer->isInScaleRange( mapSettings.scale() ) )
37 {
38 bool layerHasAdvancedBlendMode = false;
39 if ( layer->blendMode() != QPainter::CompositionMode_SourceOver )
40 {
42 {
43 layerHasAdvancedBlendMode = !QgsAbstractGeospatialPdfExporter::compositionModeSupported( layer->blendMode() );
44 }
45 else
46 {
47 layerHasAdvancedBlendMode = true;
48 }
49 }
50
51 if ( layerHasAdvancedBlendMode )
52 {
53 layers << layer->name();
54 }
55 // if vector layer, check labels and feature blend mode
56 if ( QgsVectorLayer *currentVectorLayer = qobject_cast<QgsVectorLayer *>( layer ) )
57 {
58 if ( currentVectorLayer->featureBlendMode() != QPainter::CompositionMode_SourceOver )
59 {
60 layers << layer->name();
61 }
62 // check label blend modes
63 if ( QgsPalLabeling::staticWillUseLayer( currentVectorLayer ) )
64 {
65 if ( currentVectorLayer->labeling() && currentVectorLayer->labeling()->requiresAdvancedEffects() )
66 {
67 layers << layer->name();
68 }
69 }
70 }
71 }
72 }
73
74 return QStringList( layers.constBegin(), layers.constEnd() );
75}
76
77void QgsMapSettingsUtils::worldFileParameters( const QgsMapSettings &mapSettings, double &a, double &b, double &c, double &d, double &e, double &f )
78{
79 QgsMapSettings ms = mapSettings;
80
81 const double rotation = ms.rotation();
82 const double alpha = rotation / 180 * M_PI;
83
84 // reset rotation to 0 to calculate world file parameters
85 ms.setRotation( 0 );
86
87 const double xOrigin = ms.visibleExtent().xMinimum() + ( ms.mapUnitsPerPixel() / 2 );
88 const double yOrigin = ms.visibleExtent().yMaximum() - ( ms.mapUnitsPerPixel() / 2 );
89
90 const double xCenter = ms.visibleExtent().center().x();
91 const double yCenter = ms.visibleExtent().center().y();
92
93 // scaling matrix
94 double s[6];
95 s[0] = ms.mapUnitsPerPixel() / ms.devicePixelRatio();
96 s[1] = 0;
97 s[2] = xOrigin;
98 s[3] = 0;
99 s[4] = -ms.mapUnitsPerPixel() / ms.devicePixelRatio();
100 s[5] = yOrigin;
101
102 // rotation matrix
103 double r[6];
104 r[0] = std::cos( alpha );
105 r[1] = -std::sin( alpha );
106 r[2] = xCenter * ( 1 - std::cos( alpha ) ) + yCenter * std::sin( alpha );
107 r[3] = std::sin( alpha );
108 r[4] = std::cos( alpha );
109 r[5] = - xCenter * std::sin( alpha ) + yCenter * ( 1 - std::cos( alpha ) );
110
111 // result = rotation x scaling = rotation(scaling(X))
112 a = r[0] * s[0] + r[1] * s[3];
113 b = r[0] * s[1] + r[1] * s[4];
114 c = r[0] * s[2] + r[1] * s[5] + r[2];
115 d = r[3] * s[0] + r[4] * s[3];
116 // Pixel YDim - almost always negative
117 // See https://en.wikipedia.org/wiki/World_file#cite_ref-3, https://github.com/qgis/QGIS/issues/26379
118 e = r[3] * s[1] + r[4] * s[4];
119 f = r[3] * s[2] + r[4] * s[5] + r[5];
120}
121
123{
124 double a, b, c, d, e, f;
125 worldFileParameters( mapSettings, a, b, c, d, e, f );
126
127 QString content;
128 // Pixel XDim
129 content += qgsDoubleToString( a ) + "\r\n";
130 // Rotation on y axis
131 content += qgsDoubleToString( d ) + "\r\n";
132 // Rotation on x axis
133 content += qgsDoubleToString( b ) + "\r\n";
134 // Pixel YDim
135 content += qgsDoubleToString( e ) + "\r\n";
136 // Origin X (top left cell)
137 content += qgsDoubleToString( c ) + "\r\n";
138 // Origin Y (top left cell)
139 content += qgsDoubleToString( f ) + "\r\n";
140
141 return content;
142}
static bool compositionModeSupported(QPainter::CompositionMode mode)
Returns true if the specified composition mode is supported for layers during Geospatial PDF exports.
Base class for all map layer types.
Definition qgsmaplayer.h:77
@ IgnoreGeoPdfSupportedEffects
Ignore advanced effects which are supported in geospatial PDF exports.
QFlags< EffectsCheckFlag > EffectsCheckFlags
static void worldFileParameters(const QgsMapSettings &mapSettings, double &a, double &b, double &c, double &d, double &e, double &f)
Computes the six parameters of a world file.
static QStringList containsAdvancedEffects(const QgsMapSettings &mapSettings, EffectsCheckFlags flags=QgsMapSettingsUtils::EffectsCheckFlags())
Checks whether any of the layers attached to a map settings object contain advanced effects.
static QString worldFileContent(const QgsMapSettings &mapSettings)
Creates the content of a world file.
Contains configuration for rendering maps.
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
double scale() const
Returns the calculated map scale.
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
float devicePixelRatio() const
Returns the device pixel ratio.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
static bool staticWillUseLayer(const QgsMapLayer *layer)
Called to find out whether a specified layer is used for labeling.
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
double xMinimum
double yMaximum
QgsPointXY center
Container for all settings relating to text rendering.
Represents a vector layer which manages a vector based dataset.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6219