QGIS API Documentation  2.99.0-Master (e077efd)
qgsmapsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapsettings.cpp
3  --------------------------------------
4  Date : December 2013
5  Copyright : (C) 2013 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsmapsettings.h"
17 
18 #include "qgsscalecalculator.h"
19 #include "qgsmaprendererjob.h"
20 #include "qgsmaptopixel.h"
21 #include "qgslogger.h"
22 
23 #include "qgsmessagelog.h"
24 #include "qgsmaplayer.h"
25 #include "qgsmaplayerregistry.h"
26 #include "qgsxmlutils.h"
27 #include "qgscsexception.h"
28 #include "qgsgeometry.h"
29 
30 Q_GUI_EXPORT extern int qt_defaultDpiX();
31 
32 
34  : mDpi( qt_defaultDpiX() ) // DPI that will be used by default for QImage instances
35  , mSize( QSize( 0, 0 ) )
36  , mExtent()
37  , mRotation( 0.0 )
38  , mMagnificationFactor( 1.0 )
39  , mProjectionsEnabled( false )
40  , mDestCRS( QgsCoordinateReferenceSystem::fromSrsId( GEOCRS_ID ) ) // WGS 84
41  , mDatumTransformStore( mDestCRS )
42  , mBackgroundColor( Qt::white )
43  , mSelectionColor( Qt::yellow )
44  , mFlags( Antialiasing | UseAdvancedEffects | DrawLabeling | DrawSelection )
45  , mImageFormat( QImage::Format_ARGB32_Premultiplied )
46  , mSegmentationTolerance( M_PI_2 / 90 )
47  , mSegmentationToleranceType( QgsAbstractGeometry::MaximumAngle )
48  , mValid( false )
49  , mVisibleExtent()
50  , mMapUnitsPerPixel( 1 )
51  , mScale( 1 )
52 {
53  updateDerived();
54 
55  // set default map units - we use WGS 84 thus use degrees
57 }
58 
60 {
61  double ratio = mMagnificationFactor / factor;
62 
63  mMagnificationFactor = factor;
64 
65  double rot = rotation();
66  setRotation( 0.0 );
67 
69  ext.scale( ratio );
70 
71  mRotation = rot;
72  mExtent = ext;
73  mDpi = mDpi / ratio;
74 
75  QgsDebugMsg( QString( "Magnification factor: %1 dpi: %2 ratio: %3" ).arg( factor ).arg( mDpi ).arg( ratio ) );
76 
77  updateDerived();
78 }
79 
81 {
82  return mMagnificationFactor;
83 }
84 
86 {
87  return mExtent;
88 }
89 
90 void QgsMapSettings::setExtent( const QgsRectangle& extent, bool magnified )
91 {
92  QgsRectangle magnifiedExtent = extent;
93 
94  if ( !magnified )
95  magnifiedExtent.scale( 1 / mMagnificationFactor );
96 
97  mExtent = magnifiedExtent;
98 
99  updateDerived();
100 }
101 
103 {
104  return mRotation;
105 }
106 
107 void QgsMapSettings::setRotation( double degrees )
108 {
109  if ( qgsDoubleNear( mRotation, degrees ) ) return;
110 
111  mRotation = degrees;
112 
113  // TODO: update extent while keeping scale ?
114  updateDerived();
115 }
116 
117 
119 {
121 
122  if ( extent.isEmpty() || !extent.isFinite() )
123  {
124  mValid = false;
125  return;
126  }
127 
128  // Don't allow zooms where the current extent is so small that it
129  // can't be accurately represented using a double (which is what
130  // currentExtent uses). Excluding 0 avoids a divide by zero and an
131  // infinite loop when rendering to a new canvas. Excluding extents
132  // greater than 1 avoids doing unnecessary calculations.
133 
134  // The scheme is to compare the width against the mean x coordinate
135  // (and height against mean y coordinate) and only allow zooms where
136  // the ratio indicates that there is more than about 12 significant
137  // figures (there are about 16 significant figures in a double).
138 
139  if ( extent.width() > 0 &&
140  extent.height() > 0 &&
141  extent.width() < 1 &&
142  extent.height() < 1 )
143  {
144  // Use abs() on the extent to avoid the case where the extent is
145  // symmetrical about 0.
146  double xMean = ( qAbs( extent.xMinimum() ) + qAbs( extent.xMaximum() ) ) * 0.5;
147  double yMean = ( qAbs( extent.yMinimum() ) + qAbs( extent.yMaximum() ) ) * 0.5;
148 
149  double xRange = extent.width() / xMean;
150  double yRange = extent.height() / yMean;
151 
152  static const double minProportion = 1e-12;
153  if ( xRange < minProportion || yRange < minProportion )
154  {
155  mValid = false;
156  return;
157  }
158  }
159 
160  double myHeight = mSize.height();
161  double myWidth = mSize.width();
162 
163  if ( !myWidth || !myHeight )
164  {
165  mValid = false;
166  return;
167  }
168 
169  // calculate the translation and scaling parameters
170  double mapUnitsPerPixelY = mExtent.height() / myHeight;
171  double mapUnitsPerPixelX = mExtent.width() / myWidth;
172  mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX;
173 
174  // calculate the actual extent of the mapCanvas
175  double dxmin = mExtent.xMinimum(), dxmax = mExtent.xMaximum(),
176  dymin = mExtent.yMinimum(), dymax = mExtent.yMaximum(), whitespace;
177 
178  if ( mapUnitsPerPixelY > mapUnitsPerPixelX )
179  {
180  whitespace = (( myWidth * mMapUnitsPerPixel ) - mExtent.width() ) * 0.5;
181  dxmin -= whitespace;
182  dxmax += whitespace;
183  }
184  else
185  {
186  whitespace = (( myHeight * mMapUnitsPerPixel ) - mExtent.height() ) * 0.5;
187  dymin -= whitespace;
188  dymax += whitespace;
189  }
190 
191  mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
192 
193  // update the scale
196 
198  visibleExtent().center().x(),
199  visibleExtent().center().y(),
200  outputSize().width(),
201  outputSize().height(),
202  mRotation );
203 
204 #if 1 // set visible extent taking rotation in consideration
205  if ( mRotation )
206  {
207  QgsPoint p1 = mMapToPixel.toMapCoordinates( QPoint( 0, 0 ) );
208  QgsPoint p2 = mMapToPixel.toMapCoordinates( QPoint( 0, myHeight ) );
209  QgsPoint p3 = mMapToPixel.toMapCoordinates( QPoint( myWidth, 0 ) );
210  QgsPoint p4 = mMapToPixel.toMapCoordinates( QPoint( myWidth, myHeight ) );
211  dxmin = std::min( p1.x(), std::min( p2.x(), std::min( p3.x(), p4.x() ) ) );
212  dymin = std::min( p1.y(), std::min( p2.y(), std::min( p3.y(), p4.y() ) ) );
213  dxmax = std::max( p1.x(), std::max( p2.x(), std::max( p3.x(), p4.x() ) ) );
214  dymax = std::max( p1.y(), std::max( p2.y(), std::max( p3.y(), p4.y() ) ) );
215  mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
216  }
217 #endif
218 
219  QgsDebugMsg( QString( "Map units per pixel (x,y) : %1, %2" ).arg( qgsDoubleToString( mapUnitsPerPixelX ), qgsDoubleToString( mapUnitsPerPixelY ) ) );
220  QgsDebugMsg( QString( "Pixmap dimensions (x,y) : %1, %2" ).arg( qgsDoubleToString( mSize.width() ), qgsDoubleToString( mSize.height() ) ) );
221  QgsDebugMsg( QString( "Extent dimensions (x,y) : %1, %2" ).arg( qgsDoubleToString( mExtent.width() ), qgsDoubleToString( mExtent.height() ) ) );
223  QgsDebugMsg( QString( "Adjusted map units per pixel (x,y) : %1, %2" ).arg( qgsDoubleToString( mVisibleExtent.width() / myWidth ), qgsDoubleToString( mVisibleExtent.height() / myHeight ) ) );
224  QgsDebugMsg( QString( "Recalced pixmap dimensions (x,y) : %1, %2" ).arg( qgsDoubleToString( mVisibleExtent.width() / mMapUnitsPerPixel ), qgsDoubleToString( mVisibleExtent.height() / mMapUnitsPerPixel ) ) );
225  QgsDebugMsg( QString( "Scale (assuming meters as map units) = 1:%1" ).arg( qgsDoubleToString( mScale ) ) );
226  QgsDebugMsg( QString( "Rotation: %1 degrees" ).arg( mRotation ) );
227 
228  mValid = true;
229 }
230 
231 
233 {
234  return mSize;
235 }
236 
238 {
239  mSize = size;
240 
241  updateDerived();
242 }
243 
245 {
246  return mDpi;
247 }
248 
250 {
251  mDpi = dpi;
252 
253  updateDerived();
254 }
255 
256 
257 QStringList QgsMapSettings::layers() const
258 {
259  return mLayers;
260 }
261 
262 void QgsMapSettings::setLayers( const QStringList& layers )
263 {
264  mLayers = layers;
265 }
266 
267 QMap<QString, QString> QgsMapSettings::layerStyleOverrides() const
268 {
269  return mLayerStyleOverrides;
270 }
271 
272 void QgsMapSettings::setLayerStyleOverrides( const QMap<QString, QString>& overrides )
273 {
274  mLayerStyleOverrides = overrides;
275 }
276 
278 {
279  mProjectionsEnabled = enabled;
280 }
281 
283 {
284  return mProjectionsEnabled;
285 }
286 
287 
289 {
290  mDestCRS = crs;
292 }
293 
295 {
296  return mDestCRS;
297 }
298 
299 
301 {
303 
304  // Since the map units have changed, force a recalculation of the scale.
305  updateDerived();
306 }
307 
308 void QgsMapSettings::setFlags( QgsMapSettings::Flags flags )
309 {
310  mFlags = flags;
311 }
312 
314 {
315  if ( on )
316  mFlags |= flag;
317  else
318  mFlags &= ~flag;
319 }
320 
321 QgsMapSettings::Flags QgsMapSettings::flags() const
322 {
323  return mFlags;
324 }
325 
327 {
328  return mFlags.testFlag( flag );
329 }
330 
332 {
333  return mScaleCalculator.mapUnits();
334 }
335 
336 
338 {
339  return mValid;
340 }
341 
343 {
344  return mVisibleExtent;
345 }
346 
348 {
349  QPolygonF poly;
350 
351  const QSize& sz = outputSize();
352  const QgsMapToPixel& m2p = mapToPixel();
353 
354  poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
355  poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
356  poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
357  poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
358 
359  return poly;
360 }
361 
363 {
364  return mMapUnitsPerPixel;
365 }
366 
367 double QgsMapSettings::scale() const
368 {
369  return mScale;
370 }
371 
372 
374 {
375  return mDatumTransformStore.transformation( layer );
376 }
377 
378 
379 double QgsMapSettings::layerToMapUnits( QgsMapLayer *theLayer, const QgsRectangle& referenceExtent ) const
380 {
381  QgsRectangle extent = referenceExtent.isEmpty() ? theLayer->extent() : referenceExtent;
382  QgsPoint l1( extent.xMinimum(), extent.yMinimum() );
383  QgsPoint l2( extent.xMaximum(), extent.yMaximum() );
384  double distLayerUnits = std::sqrt( l1.sqrDist( l2 ) );
385  QgsPoint m1 = layerToMapCoordinates( theLayer, l1 );
386  QgsPoint m2 = layerToMapCoordinates( theLayer, l2 );
387  double distMapUnits = std::sqrt( m1.sqrDist( m2 ) );
388  return distMapUnits / distLayerUnits;
389 }
390 
391 
393 {
394  if ( hasCrsTransformEnabled() )
395  {
396  try
397  {
398  QgsCoordinateTransform ct = layerTransform( theLayer );
399  if ( ct.isValid() )
400  {
401  QgsDebugMsg( QString( "sourceCrs = " + ct.sourceCrs().authid() ) );
402  QgsDebugMsg( QString( "destCRS = " + ct.destinationCrs().authid() ) );
403  QgsDebugMsg( QString( "extent = " + extent.toString() ) );
404  extent = ct.transformBoundingBox( extent );
405  }
406  }
407  catch ( QgsCsException &cse )
408  {
409  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
410  }
411  }
412 
413  QgsDebugMsg( QString( "proj extent = " + extent.toString() ) );
414 
415  return extent;
416 }
417 
418 
420 {
421  if ( hasCrsTransformEnabled() )
422  {
423  try
424  {
425  QgsCoordinateTransform ct = layerTransform( theLayer );
426  if ( ct.isValid() )
427  {
428  QgsDebugMsg( QString( "sourceCrs = " + ct.sourceCrs().authid() ) );
429  QgsDebugMsg( QString( "destCRS = " + ct.destinationCrs().authid() ) );
430  QgsDebugMsg( QString( "extent = " + extent.toString() ) );
432  }
433  }
434  catch ( QgsCsException &cse )
435  {
436  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
437  }
438  }
439 
440  QgsDebugMsg( QString( "proj extent = " + extent.toString() ) );
441 
442  return extent;
443 }
444 
445 
447 {
448  if ( hasCrsTransformEnabled() )
449  {
450  try
451  {
452  QgsCoordinateTransform ct = layerTransform( theLayer );
453  if ( ct.isValid() )
455  }
456  catch ( QgsCsException &cse )
457  {
458  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
459  }
460  }
461  else
462  {
463  // leave point without transformation
464  }
465  return point;
466 }
467 
468 
470 {
471  if ( hasCrsTransformEnabled() )
472  {
473  try
474  {
475  QgsCoordinateTransform ct = layerTransform( theLayer );
476  if ( ct.isValid() )
478  }
479  catch ( QgsCsException &cse )
480  {
481  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
482  }
483  }
484  else
485  {
486  // leave point without transformation
487  }
488  return rect;
489 }
490 
491 
493 {
494  if ( hasCrsTransformEnabled() )
495  {
496  try
497  {
498  QgsCoordinateTransform ct = layerTransform( theLayer );
499  if ( ct.isValid() )
501  }
502  catch ( QgsCsException &cse )
503  {
504  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
505  }
506  }
507  else
508  {
509  // leave point without transformation
510  }
511  return point;
512 }
513 
514 
516 {
517  if ( hasCrsTransformEnabled() )
518  {
519  try
520  {
521  QgsCoordinateTransform ct = layerTransform( theLayer );
522  if ( ct.isValid() )
524  }
525  catch ( QgsCsException &cse )
526  {
527  QgsMessageLog::logMessage( QStringLiteral( "Transform error caught: %1" ).arg( cse.what() ), QStringLiteral( "CRS" ) );
528  }
529  }
530  return rect;
531 }
532 
533 
534 
536 {
537  QgsDebugMsg( "called." );
539 
540  // reset the map canvas extent since the extent may now be smaller
541  // We can't use a constructor since QgsRectangle normalizes the rectangle upon construction
543  fullExtent.setMinimal();
544 
545  // iterate through the map layers and test each layers extent
546  // against the current min and max values
547  QStringList::const_iterator it = mLayers.begin();
548  QgsDebugMsg( QString( "Layer count: %1" ).arg( mLayers.count() ) );
549  while ( it != mLayers.end() )
550  {
551  QgsMapLayer * lyr = registry->mapLayer( *it );
552  if ( !lyr )
553  {
554  QgsDebugMsg( QString( "WARNING: layer '%1' not found in map layer registry!" ).arg( *it ) );
555  }
556  else
557  {
558  QgsDebugMsg( "Updating extent using " + lyr->name() );
559  QgsDebugMsg( "Input extent: " + lyr->extent().toString() );
560 
561  if ( lyr->extent().isNull() )
562  {
563  ++it;
564  continue;
565  }
566 
567  // Layer extents are stored in the coordinate system (CS) of the
568  // layer. The extent must be projected to the canvas CS
570 
571  QgsDebugMsg( "Output extent: " + extent.toString() );
572  fullExtent.unionRect( extent );
573 
574  }
575  ++it;
576  }
577 
578  if ( fullExtent.width() == 0.0 || fullExtent.height() == 0.0 )
579  {
580  // If all of the features are at the one point, buffer the
581  // rectangle a bit. If they are all at zero, do something a bit
582  // more crude.
583 
584  if ( fullExtent.xMinimum() == 0.0 && fullExtent.xMaximum() == 0.0 &&
585  fullExtent.yMinimum() == 0.0 && fullExtent.yMaximum() == 0.0 )
586  {
587  fullExtent.set( -1.0, -1.0, 1.0, 1.0 );
588  }
589  else
590  {
591  const double padFactor = 1e-8;
592  double widthPad = fullExtent.xMinimum() * padFactor;
593  double heightPad = fullExtent.yMinimum() * padFactor;
594  double xmin = fullExtent.xMinimum() - widthPad;
595  double xmax = fullExtent.xMaximum() + widthPad;
596  double ymin = fullExtent.yMinimum() - heightPad;
597  double ymax = fullExtent.yMaximum() + heightPad;
598  fullExtent.set( xmin, ymin, xmax, ymax );
599  }
600  }
601 
602  QgsDebugMsg( "Full extent: " + fullExtent.toString() );
603  return fullExtent;
604 }
605 
606 
607 void QgsMapSettings::readXml( QDomNode& theNode )
608 {
609  // set units
610  QDomNode mapUnitsNode = theNode.namedItem( QStringLiteral( "units" ) );
611  QgsUnitTypes::DistanceUnit units = QgsXmlUtils::readMapUnits( mapUnitsNode.toElement() );
612  setMapUnits( units );
613 
614  // set projections flag
615  QDomNode projNode = theNode.namedItem( QStringLiteral( "projections" ) );
616  setCrsTransformEnabled( projNode.toElement().text().toInt() );
617 
618  // set destination CRS
620  QDomNode srsNode = theNode.namedItem( QStringLiteral( "destinationsrs" ) );
621  srs.readXml( srsNode );
622  setDestinationCrs( srs );
623 
624  // set extent
625  QDomNode extentNode = theNode.namedItem( QStringLiteral( "extent" ) );
626  QgsRectangle aoi = QgsXmlUtils::readRectangle( extentNode.toElement() );
627  setExtent( aoi );
628 
629  // set rotation
630  QDomNode rotationNode = theNode.namedItem( QStringLiteral( "rotation" ) );
631  QString rotationVal = rotationNode.toElement().text();
632  if ( ! rotationVal.isEmpty() )
633  {
634  double rot = rotationVal.toDouble();
635  setRotation( rot );
636  }
637 
638  //render map tile
639  QDomElement renderMapTileElem = theNode.firstChildElement( QStringLiteral( "rendermaptile" ) );
640  if ( !renderMapTileElem.isNull() )
641  {
642  setFlag( QgsMapSettings::RenderMapTile, renderMapTileElem.text() == QLatin1String( "1" ) ? true : false );
643  }
644 
645  mDatumTransformStore.readXml( theNode );
646 }
647 
648 
649 
650 void QgsMapSettings::writeXml( QDomNode& theNode, QDomDocument& theDoc )
651 {
652  // units
653  theNode.appendChild( QgsXmlUtils::writeMapUnits( mapUnits(), theDoc ) );
654 
655  // Write current view extents
656  theNode.appendChild( QgsXmlUtils::writeRectangle( extent(), theDoc ) );
657 
658  // Write current view rotation
659  QDomElement rotNode = theDoc.createElement( QStringLiteral( "rotation" ) );
660  rotNode.appendChild(
661  theDoc.createTextNode( qgsDoubleToString( rotation() ) )
662  );
663  theNode.appendChild( rotNode );
664 
665  // projections enabled
666  QDomElement projNode = theDoc.createElement( QStringLiteral( "projections" ) );
667  projNode.appendChild( theDoc.createTextNode( QString::number( hasCrsTransformEnabled() ) ) );
668  theNode.appendChild( projNode );
669 
670  // destination CRS
671  QDomElement srsNode = theDoc.createElement( QStringLiteral( "destinationsrs" ) );
672  theNode.appendChild( srsNode );
673  destinationCrs().writeXml( srsNode, theDoc );
674 
675  //render map tile
676  QDomElement renderMapTileElem = theDoc.createElement( QStringLiteral( "rendermaptile" ) );
677  QDomText renderMapTileText = theDoc.createTextNode( testFlag( QgsMapSettings::RenderMapTile ) ? "1" : "0" );
678  renderMapTileElem.appendChild( renderMapTileText );
679  theNode.appendChild( renderMapTileElem );
680 
681  mDatumTransformStore.writeXml( theNode, theDoc );
682 }
void unionRect(const QgsRectangle &rect)
Updates rectangle to include passed argument.
double mMagnificationFactor
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setExtent(const QgsRectangle &rect, bool magnified=true)
Set coordinates of the rectangle which should be rendered.
double y
Definition: qgspoint.h:116
void writeXml(QDomNode &theNode, QDomDocument &theDoc)
QgsRectangle mVisibleExtent
Extent with some additional white space that matches the output aspect ratio.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
QgsMapToPixel mMapToPixel
QgsCoordinateTransform transformation(QgsMapLayer *layer) const
Will return transform from layer&#39;s CRS to current destination CRS.
double magnificationFactor() const
Return the magnification factor.
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspoint.cpp:124
double rotation() const
Return the rotation of the resulting map image Units are clockwise degrees.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QgsCoordinateReferenceSystem mDestCRS
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsRectangle layerExtentToOutputExtent(QgsMapLayer *theLayer, QgsRectangle extent) const
transform bounding box from layer&#39;s CRS to output CRS
QMap< QString, QString > mLayerStyleOverrides
void scale(double scaleFactor, const QgsPoint *c=nullptr)
Scale the rectangle around its center point.
void setOutputDpi(double dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, TransformDirection direction=ForwardTransform, const bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
void setDpi(double dpi)
Set the dpi to be used in scale calculations.
void writeXml(QDomNode &parentNode, QDomDocument &theDoc) const
void setDestinationCrs(const QgsCoordinateReferenceSystem &destCrs)
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc)
Definition: qgsxmlutils.cpp:78
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
void setFlags(QgsMapSettings::Flags flags)
Set combination of flags that will be used for rendering.
QgsPoint toMapCoordinates(int x, int y) const
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Set map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
bool writeXml(QDomNode &theNode, QDomDocument &theDoc) const
Stores state to the given Dom node in the given document.
void setLayers(const QStringList &layers)
Set list of layer IDs for map rendering.
Q_GUI_EXPORT int qt_defaultDpiX()
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:196
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
virtual QgsRectangle extent() const
Returns the extent of the layer.
#define M_PI_2
Definition: util.cpp:45
QString what() const
Definition: qgsexception.h:36
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
QgsUnitTypes::DistanceUnit mapUnits() const
Get units of map&#39;s geographical coordinates - used for scale calculation.
void set(const QgsPoint &p1, const QgsPoint &p2)
Set the rectangle from two QgsPoints.
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
bool mValid
Whether the actual settings are valid (set in updateDerived())
QgsRectangle extent() const
Return geographical coordinates of the rectangle that should be rendered.
void setOutputSize(QSize size)
Set the size of the resulting map image.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from...
QgsRectangle mExtent
double calculate(const QgsRectangle &mapExtent, int canvasWidth)
Calculate the scale denominator.
double layerToMapUnits(QgsMapLayer *theLayer, const QgsRectangle &referenceExtent=QgsRectangle()) const
Computes an estimated conversion factor between layer and map units: layerUnits * layerToMapUnits = m...
QgsPoint transform(const QgsPoint &point, TransformDirection direction=ForwardTransform) const
Transform the point from the source CRS to the destination CRS.
Flag
Enumeration of flags that adjust the way the map is rendered.
void setMagnificationFactor(double factor)
Set the magnification factor.
bool isEmpty() const
test if rectangle is empty.
static QgsUnitTypes::DistanceUnit readMapUnits(const QDomElement &element)
Decodes a distance unit from a DOM element.
Definition: qgsxmlutils.cpp:23
void setRotation(double degrees)
Set the rotation of the resulting map image Units are clockwise degrees.
double scale() const
Return the calculated scale of the map.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:211
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:184
double mapUnitsPerPixel() const
Return the distance in geographical coordinates that equals to one pixel in the map.
void setMapUnits(QgsUnitTypes::DistanceUnit mapUnits)
Set the map units.
const long GEOCRS_ID
Magic number for a geographic coord sys in QGIS srs.db tbl_srs.srs_id.
Definition: qgis.h:298
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
static QDomElement writeMapUnits(QgsUnitTypes::DistanceUnit units, QDomDocument &doc)
Encodes a distance unit to a DOM element.
Definition: qgsxmlutils.cpp:66
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:50
void readXml(const QDomNode &parentNode)
void readXml(QDomNode &theNode)
bool hasValidSettings() const
Check whether the map settings are valid and can be used for rendering.
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:452
Abstract base class for all geometries.
QgsScaleCalculator mScaleCalculator
A class to represent a point.
Definition: qgspoint.h:111
const QgsMapToPixel & mapToPixel() const
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
Definition: MathUtils.cc:437
This class tracks map layers that are currently loaded and provides various methods to retrieve match...
bool isFinite() const
Returns true if the rectangle has finite boundaries.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:206
QgsCoordinateTransform layerTransform(QgsMapLayer *layer) const
Return coordinate transform from layer&#39;s CRS to destination CRS.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:191
Draw map such that there are no problems between adjacent tiles.
double outputDpi() const
Return DPI used for conversion between real world units (e.g.
QMap< QString, QString > layerStyleOverrides() const
Get map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QgsPoint layerToMapCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from layer&#39;s CRS to output CRS
Transform from destination to source CRS.
double mMapUnitsPerPixel
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer&#39;s CRS
QgsRectangle fullExtent() const
returns current extent of layer set
This class represents a coordinate reference system (CRS).
bool isNull() const
test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Class for doing transforms between two map coordinate systems.
Flags flags() const
Return combination of flags used for rendering.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:196
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:201
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
Transform from source to destination CRS.
bool testFlag(Flag flag) const
Check whether a particular flag is enabled.
Custom exception class for Coordinate Reference System related exceptions.
QPolygonF visiblePolygon() const
Return the visible area as a polygon (may be rotated)
QStringList mLayers
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
QgsPoint toMapCoordinatesF(double x, double y) const
Transform device coordinates to map (world) coordinates.
QgsDatumTransformStore mDatumTransformStore
static QgsRectangle readRectangle(const QDomElement &element)
Definition: qgsxmlutils.cpp:36
bool readXml(const QDomNode &theNode)
Restores state from the given DOM node.
QSize outputSize() const
Return the size of the resulting map image.
QgsRectangle outputExtentToLayerExtent(QgsMapLayer *theLayer, QgsRectangle extent) const
transform bounding box from output CRS to layer&#39;s CRS
QString authid() const
Returns the authority identifier for the CRS.
void setMapUnits(QgsUnitTypes::DistanceUnit u)
Set units of map&#39;s geographical coordinates - used for scale calculation.
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:216
QgsUnitTypes::DistanceUnit mapUnits() const
Returns current map units.
void setCrsTransformEnabled(bool enabled)
sets whether to use projections for this layer set
double x
Definition: qgspoint.h:115