QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgstextrenderer.h"
22 #include "qgsvectorlayer.h"
23 
24 #include <QString>
25 
26 const QStringList QgsMapSettingsUtils::containsAdvancedEffects( const QgsMapSettings &mapSettings )
27 {
28  QSet< QString > layers;
29 
30  QgsTextFormat layerFormat;
31  const auto constLayers = mapSettings.layers();
32  for ( QgsMapLayer *layer : constLayers )
33  {
34  if ( layer )
35  {
36  if ( layer->blendMode() != QPainter::CompositionMode_SourceOver )
37  {
38  layers << layer->name();
39  }
40  // if vector layer, check labels and feature blend mode
41  QgsVectorLayer *currentVectorLayer = qobject_cast<QgsVectorLayer *>( layer );
42  if ( currentVectorLayer )
43  {
44  if ( !qgsDoubleNear( currentVectorLayer->opacity(), 1.0 ) )
45  {
46  layers << layer->name();
47  }
48  if ( currentVectorLayer->featureBlendMode() != QPainter::CompositionMode_SourceOver )
49  {
50  layers << layer->name();
51  }
52  // check label blend modes
53  if ( QgsPalLabeling::staticWillUseLayer( currentVectorLayer ) )
54  {
55  // Check all label blending properties
56  layerFormat.readFromLayer( currentVectorLayer );
57  if ( layerFormat.containsAdvancedEffects() )
58  {
59  layers << layer->name();
60  }
61  }
62  }
63  }
64  }
65 
66  return layers.toList();
67 }
68 
69 void QgsMapSettingsUtils::worldFileParameters( const QgsMapSettings &mapSettings, double &a, double &b, double &c, double &d, double &e, double &f )
70 {
71  QgsMapSettings ms = mapSettings;
72 
73  double rotation = ms.rotation();
74  double alpha = rotation / 180 * M_PI;
75 
76  // reset rotation to 0 to calculate world file parameters
77  ms.setRotation( 0 );
78 
79  double xOrigin = ms.visibleExtent().xMinimum() + ( ms.mapUnitsPerPixel() / 2 );
80  double yOrigin = ms.visibleExtent().yMaximum() - ( ms.mapUnitsPerPixel() / 2 );
81 
82  double xCenter = ms.visibleExtent().center().x();
83  double yCenter = ms.visibleExtent().center().y();
84 
85  // scaling matrix
86  double s[6];
87  s[0] = ms.mapUnitsPerPixel();
88  s[1] = 0;
89  s[2] = xOrigin;
90  s[3] = 0;
91  s[4] = -ms.mapUnitsPerPixel();
92  s[5] = yOrigin;
93 
94  // rotation matrix
95  double r[6];
96  r[0] = std::cos( alpha );
97  r[1] = -std::sin( alpha );
98  r[2] = xCenter * ( 1 - std::cos( alpha ) ) + yCenter * std::sin( alpha );
99  r[3] = std::sin( alpha );
100  r[4] = std::cos( alpha );
101  r[5] = - xCenter * std::sin( alpha ) + yCenter * ( 1 - std::cos( alpha ) );
102 
103  // result = rotation x scaling = rotation(scaling(X))
104  a = r[0] * s[0] + r[1] * s[3];
105  b = r[0] * s[1] + r[1] * s[4];
106  c = r[0] * s[2] + r[1] * s[5] + r[2];
107  d = r[3] * s[0] + r[4] * s[3];
108  // Pixel YDim - almost always negative
109  // See https://en.wikipedia.org/wiki/World_file#cite_ref-3, https://github.com/qgis/QGIS/issues/26379
110  e = r[3] * s[1] + r[4] * s[4];
111  f = r[3] * s[2] + r[4] * s[5] + r[5];
112 }
113 
115 {
116  double a, b, c, d, e, f;
117  worldFileParameters( mapSettings, a, b, c, d, e, f );
118 
119  QString content;
120  // Pixel XDim
121  content += qgsDoubleToString( a ) + "\r\n";
122  // Rotation on y axis
123  content += qgsDoubleToString( d ) + "\r\n";
124  // Rotation on x axis
125  content += qgsDoubleToString( b ) + "\r\n";
126  // Pixel YDim
127  content += qgsDoubleToString( e ) + "\r\n";
128  // Origin X (top left cell)
129  content += qgsDoubleToString( c ) + "\r\n";
130  // Origin Y (top left cell)
131  content += qgsDoubleToString( f ) + "\r\n";
132 
133  return content;
134 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
double y
Definition: qgspointxy.h:48
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:280
static const QStringList containsAdvancedEffects(const QgsMapSettings &mapSettings)
Checks whether any of the layers attached to a map settings object contain advanced effects...
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
static QString worldFileContent(const QgsMapSettings &mapSettings)
Creates the content of a world file.
The QgsMapSettings class contains configuration for rendering of the map.
static bool staticWillUseLayer(QgsVectorLayer *layer)
called to find out whether the layer is used for labeling
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:240
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
QPainter::CompositionMode featureBlendMode() const
Returns the current blending mode for features.
double x
Definition: qgspointxy.h:47
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.
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
QString name
Definition: qgsmaplayer.h:83
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
Container for all settings relating to text rendering.
Represents a vector layer which manages a vector based data sets.
bool containsAdvancedEffects() const
Returns true if any component of the font format requires advanced effects such as blend modes...
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer&#39;s custom properties (for QGIS 2.x projects).