QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
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
19#include "qgsellipsoidutils.h"
20#include "qgsexception.h"
21#include "qgsgeometry.h"
22#include "qgsgrouplayer.h"
23#include "qgslogger.h"
24#include "qgsmaplayer.h"
26#include "qgsmapsettingsutils.h"
27#include "qgsmaptopixel.h"
28#include "qgsmessagelog.h"
29#include "qgspainting.h"
30#include "qgsscalecalculator.h"
31#include "qgsunittypes.h"
32#include "qgsxmlutils.h"
33
34#include <QString>
35
36using namespace Qt::StringLiterals;
37
39 : mDpi( QgsPainting::qtDefaultDpiX() ) // DPI that will be used by default for QImage instances
40 , mSize( QSize( 0, 0 ) )
41 , mBackgroundColor( Qt::white )
42 , mSelectionColor( Qt::yellow )
43 , mFlags( Qgis::MapSettingsFlag::Antialiasing | Qgis::MapSettingsFlag::UseAdvancedEffects | Qgis::MapSettingsFlag::DrawLabeling | Qgis::MapSettingsFlag::DrawSelection )
44 , mSegmentationTolerance( M_PI_2 / 90 )
45{
48
50}
51
52void QgsMapSettings::setMagnificationFactor( double factor, const QgsPointXY *center )
53{
54 const double ratio = mMagnificationFactor / factor;
55
56 mMagnificationFactor = factor;
57
58 const double rot = rotation();
59 setRotation( 0.0 );
60
62 ext.scale( ratio, center );
63
64 mRotation = rot;
65 mExtent = ext;
66 mDpi = mDpi / ratio;
67
68 QgsDebugMsgLevel( u"Magnification factor: %1 dpi: %2 ratio: %3"_s.arg( factor ).arg( mDpi ).arg( ratio ), 3 );
69
71}
72
77
79{
80 return mExtent;
81}
82
83void QgsMapSettings::setExtent( const QgsRectangle &extent, bool magnified )
84{
85 QgsRectangle magnifiedExtent = extent;
86
87 if ( !magnified )
88 magnifiedExtent.scale( 1 / mMagnificationFactor );
89
90 mExtent = magnifiedExtent;
91
93}
94
96{
97 return mExtentBuffer;
98}
99
100void QgsMapSettings::setExtentBuffer( const double buffer )
101{
102 mExtentBuffer = buffer;
103}
104
106{
107 return mRotation;
108}
109
110void QgsMapSettings::setRotation( double degrees )
111{
112 if ( qgsDoubleNear( mRotation, degrees ) )
113 return;
114
115 mRotation = degrees;
116
117 // TODO: update extent while keeping scale ?
119}
120
121
123{
125
126 // Don't allow zooms where the current extent is so small that it
127 // can't be accurately represented using a double (which is what
128 // currentExtent uses).
130 {
131 mValid = false;
132 return;
133 }
134
135 const int widthPixels = mSize.width();
136 const int heightPixels = mSize.height();
137 if ( widthPixels == 0 || heightPixels == 0 )
138 {
139 mValid = false;
140 return;
141 }
142
143 // calculate the translation and scaling parameters
144 const double mapUnitsPerPixelY = mExtent.height() / static_cast< double >( heightPixels );
145 const double mapUnitsPerPixelX = mExtent.width() / static_cast< double >( widthPixels );
146 mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX;
147
148 // calculate the actual extent of the mapCanvas
149 double dxmin = mExtent.xMinimum(), dxmax = mExtent.xMaximum(), dymin = mExtent.yMinimum(), dymax = mExtent.yMaximum(), whitespace;
150
151 if ( mapUnitsPerPixelY > mapUnitsPerPixelX )
152 {
153 whitespace = ( ( widthPixels * mMapUnitsPerPixel ) - mExtent.width() ) * 0.5;
154 dxmin -= whitespace;
155 dxmax += whitespace;
156 }
157 else
158 {
159 whitespace = ( ( heightPixels * mMapUnitsPerPixel ) - mExtent.height() ) * 0.5;
160 dymin -= whitespace;
161 dymax += whitespace;
162 }
163
164 mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
165
166 // update the scale
167 mScaleCalculator.setDpi( mDpi );
168 mScale = mScaleCalculator.calculate( mVisibleExtent, widthPixels );
169
170 bool ok = true;
171 mMapToPixel.setParameters( mapUnitsPerPixel(), visibleExtent().center().x(), visibleExtent().center().y(), outputSize().width(), outputSize().height(), mRotation, &ok );
172
173 mValid = ok;
174
175 // set visible extent taking rotation in consideration
176 if ( !qgsDoubleNear( mRotation, 0 ) )
177 {
178 const QgsPointXY p1 = mMapToPixel.toMapCoordinates( QPoint( 0, 0 ) );
179 const QgsPointXY p2 = mMapToPixel.toMapCoordinates( QPoint( 0, heightPixels ) );
180 const QgsPointXY p3 = mMapToPixel.toMapCoordinates( QPoint( widthPixels, 0 ) );
181 const QgsPointXY p4 = mMapToPixel.toMapCoordinates( QPoint( widthPixels, heightPixels ) );
182 dxmin = std::min( p1.x(), std::min( p2.x(), std::min( p3.x(), p4.x() ) ) );
183 dymin = std::min( p1.y(), std::min( p2.y(), std::min( p3.y(), p4.y() ) ) );
184 dxmax = std::max( p1.x(), std::max( p2.x(), std::max( p3.x(), p4.x() ) ) );
185 dymax = std::max( p1.y(), std::max( p2.y(), std::max( p3.y(), p4.y() ) ) );
186 mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
187 }
188
189 QgsDebugMsgLevel( u"Map units per pixel (x,y) : %1, %2"_s.arg( qgsDoubleToString( mapUnitsPerPixelX ), qgsDoubleToString( mapUnitsPerPixelY ) ), 5 );
190 QgsDebugMsgLevel( u"Pixmap dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( widthPixels ), qgsDoubleToString( heightPixels ) ), 5 );
191 QgsDebugMsgLevel( u"Extent dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( mExtent.width() ), qgsDoubleToString( mExtent.height() ) ), 5 );
192 QgsDebugMsgLevel( mExtent.toString(), 5 );
193 QgsDebugMsgLevel( u"Adjusted map units per pixel (x,y) : %1, %2"_s.arg( qgsDoubleToString( mVisibleExtent.width() / widthPixels ), qgsDoubleToString( mVisibleExtent.height() / heightPixels ) ), 5 );
194 QgsDebugMsgLevel( u"Recalced pixmap dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( mVisibleExtent.width() / mMapUnitsPerPixel ), qgsDoubleToString( mVisibleExtent.height() / mMapUnitsPerPixel ) ), 5 );
195 QgsDebugMsgLevel( u"Scale (assuming meters as map units) = 1:%1"_s.arg( qgsDoubleToString( mScale ) ), 5 );
196 QgsDebugMsgLevel( u"Rotation: %1 degrees"_s.arg( mRotation ), 5 );
197 QgsDebugMsgLevel( u"Extent: %1"_s.arg( mExtent.asWktCoordinates() ), 5 );
198 QgsDebugMsgLevel( u"Visible Extent: %1"_s.arg( mVisibleExtent.asWktCoordinates() ), 5 );
199 QgsDebugMsgLevel( u"Magnification factor: %1"_s.arg( mMagnificationFactor ), 5 );
200}
201
202void QgsMapSettings::matchRasterizedRenderingPolicyToFlags()
203{
210}
211
213{
214 return mSize;
215}
216
218{
219 mSize = size;
220
222}
223
225{
226 return mDevicePixelRatio;
227}
228
230{
231 mDevicePixelRatio = dpr;
233}
234
236{
237 return outputSize() * mDevicePixelRatio;
238}
239
241{
242 return mDpi;
243}
244
246{
247 mDpi = dpi;
248
250}
251
253{
254 return mDpiTarget;
255}
256
258{
259 mDpiTarget = dpi;
260}
261
262QStringList QgsMapSettings::layerIds( bool expandGroupLayers ) const
263{
264 if ( !expandGroupLayers || !mHasGroupLayers )
265 {
266 return mLayerIds;
267 }
268 else
269 {
270 const QList<QgsMapLayer * > mapLayers = layers( expandGroupLayers );
271 QStringList res;
272 res.reserve( mapLayers.size() );
273 for ( const QgsMapLayer *layer : mapLayers )
274 res << layer->id();
275 return res;
276 }
277}
278
279QList<QgsMapLayer *> QgsMapSettings::layers( bool expandGroupLayers ) const
280{
281 const QList<QgsMapLayer *> actualLayers = _qgis_listQPointerToRaw( mLayers );
282 if ( !expandGroupLayers )
283 return actualLayers;
284
285 QList< QgsMapLayer * > result;
286
287 std::function< void( const QList< QgsMapLayer * > &layers ) > expandLayers;
288 expandLayers = [&result, &expandLayers]( const QList< QgsMapLayer * > &layers ) {
289 for ( QgsMapLayer *layer : layers )
290 {
291 if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( layer ) )
292 {
293 expandLayers( groupLayer->childLayers() );
294 }
295 else
296 {
297 result << layer;
298 }
299 }
300 };
301
302 expandLayers( actualLayers );
303 return result;
304}
305
306template<typename T> QVector<T> QgsMapSettings::layers() const
307{
308 const QList<QgsMapLayer *> actualLayers = _qgis_listQPointerToRaw( mLayers );
309
310 QVector<T> layers;
311 for ( QgsMapLayer *layer : actualLayers )
312 {
313 T tLayer = qobject_cast<T>( layer );
314 if ( tLayer )
315 {
316 layers << tLayer;
317 }
318 }
319 return layers;
320}
321
322void QgsMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
323{
324 // filter list, removing null layers and non-spatial layers
325 auto filteredList = layers;
326 filteredList.erase( std::remove_if( filteredList.begin(), filteredList.end(), []( QgsMapLayer *layer ) { return !layer || !layer->isSpatial(); } ), filteredList.end() );
327
328 mLayers = _qgis_listRawToQPointer( filteredList );
329
330 // pre-generate and store layer IDs, so that we can safely access them from other threads
331 // without needing to touch the actual map layer object
332 mLayerIds.clear();
333 mHasGroupLayers = false;
334 mLayerIds.reserve( mLayers.size() );
335 for ( const QgsMapLayer *layer : std::as_const( mLayers ) )
336 {
337 mLayerIds << layer->id();
338 if ( layer->type() == Qgis::LayerType::Group )
339 mHasGroupLayers = true;
340 }
341}
342
343QMap<QString, QString> QgsMapSettings::layerStyleOverrides() const
344{
346}
347
348void QgsMapSettings::setLayerStyleOverrides( const QMap<QString, QString> &overrides )
349{
350 mLayerStyleOverrides = overrides;
351}
352
354{
355 if ( crs == mDestCRS )
356 return;
357
358 mDestCRS = crs;
359 mScaleCalculator.setMapUnits( crs.mapUnits() );
360 // Since the map units have changed, force a recalculation of the scale.
362}
363
368
370{
371 if ( ellipsoid == mEllipsoid )
372 return true;
373
375 if ( !params.valid )
376 {
377 return false;
378 }
379 else
380 {
382 mScaleCalculator.setEllipsoid( ellipsoid );
383 // Since mScaleCalculator ellipsoid have changed, force a recalculation of the scale.
385 return true;
386 }
387}
388
390{
391 mFlags = flags;
392 matchRasterizedRenderingPolicyToFlags();
393}
394
396{
397 if ( on )
398 mFlags |= flag;
399 else
400 mFlags &= ~( static_cast< int >( flag ) );
401 matchRasterizedRenderingPolicyToFlags();
402}
403
408
410{
411 return mFlags.testFlag( flag );
412}
413
415{
416 return mScaleCalculator.mapUnits();
417}
418
423
425{
426 mScaleCalculator.setMethod( method );
428}
429
431{
432 return mValid;
433}
434
439
441{
442 QPolygonF poly;
443
444 const QSize &sz = outputSize();
445 const QgsMapToPixel &m2p = mapToPixel();
446
447 poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
448 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
449 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
450 poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();
451
452 return poly;
453}
454
456{
457 QPolygonF poly;
458
459 const QSize &sz = outputSize();
460 const QgsMapToPixel &m2p = mapToPixel();
461
462 // Transform tilebuffer in pixel.
463 // Original tilebuffer is in pixel and transformed only according
464 // extent width (see QgsWmsRenderContext::mapTileBuffer)
465
466 const double mapUnitsPerPixel = mExtent.width() / sz.width();
467 const double buffer = mExtentBuffer / mapUnitsPerPixel;
468
469 poly << m2p.toMapCoordinates( -buffer, -buffer ).toQPointF();
470 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), -buffer ).toQPointF();
471 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), static_cast<double>( sz.height() + buffer ) ).toQPointF();
472 poly << m2p.toMapCoordinates( -buffer, static_cast<double>( sz.height() + buffer ) ).toQPointF();
473
474 return poly;
475}
476
478{
479 return mMapUnitsPerPixel;
480}
481
483{
484 return mScale;
485}
486
488{
489#ifdef QGISDEBUG
490 if ( !mHasTransformContext )
491 QgsDebugMsgLevel( u"No QgsCoordinateTransformContext context set for transform"_s, 4 );
492#endif
493
494 return mTransformContext;
495}
496
498{
499 mTransformContext = context;
500#ifdef QGISDEBUG
501 mHasTransformContext = true;
502#endif
503}
504
506{
507 if ( !layer )
508 return QgsCoordinateTransform();
509
511}
512
514{
515 // Output width in inches
516 const double outputWidthInInches = outputSize().width() / outputDpi();
517
518 // Desired visible width (honouring scale)
519 const double scaledWidthInInches = outputWidthInInches * scale;
520
522 {
523 // Start with some fraction of the current extent around the center
524 const double delta = mExtent.width() / 100.;
525 QgsRectangle ext( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
526 // Get scale at extent, and then scale extent to the desired scale
527 const double testScale = mScaleCalculator.calculate( ext, outputSize().width() );
528 ext.scale( scale / testScale );
529 return ext;
530 }
531 else
532 {
533 // Conversion from inches to mapUnits - this is safe to use, because we know here that the map units AREN'T in degrees
534 const double conversionFactor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Feet, mapUnits() ) / 12;
535
536 const double delta = 0.5 * scaledWidthInInches * conversionFactor;
537 return QgsRectangle( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
538 }
539}
540
542{
543 return mScaleCalculator.calculate( extent, outputSize().width() );
544}
545
546double QgsMapSettings::layerToMapUnits( const QgsMapLayer *layer, const QgsRectangle &referenceExtent ) const
547{
548 return layerTransform( layer ).scaleFactor( referenceExtent );
549}
550
551
553{
554 try
555 {
557 if ( ct.isValid() )
558 {
559 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
560 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
561 QgsDebugMsgLevel( u"extent %1"_s.arg( extent.toString() ), 3 );
564 }
565 }
566 catch ( QgsCsException &cse )
567 {
568 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
569 }
570
571 QgsDebugMsgLevel( u"proj extent = %1 "_s.arg( extent.toString() ), 3 );
572
573 return extent;
574}
575
576
578{
579 try
580 {
583 if ( ct.isValid() )
584 {
585 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
586 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
587 QgsDebugMsgLevel( u"extent = %1"_s.arg( extent.toString() ), 3 );
589 }
590 }
591 catch ( QgsCsException &cse )
592 {
593 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
594 }
595
596 QgsDebugMsgLevel( u"proj extent = %1"_s.arg( extent.toString() ), 3 );
597
598 return extent;
599}
600
601
603{
604 try
605 {
606 const QgsCoordinateTransform ct = layerTransform( layer );
607 if ( ct.isValid() )
608 point = ct.transform( point, Qgis::TransformDirection::Forward );
609 }
610 catch ( QgsCsException &cse )
611 {
612 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
613 }
614
615 return point;
616}
617
619{
620 double x = point.x();
621 double y = point.y();
622 double z = point.z();
623 const double m = point.m();
624
625 try
626 {
627 const QgsCoordinateTransform ct = layerTransform( layer );
628 if ( ct.isValid() )
630 }
631 catch ( QgsCsException &cse )
632 {
633 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
634 }
635
636 return QgsPoint( x, y, z, m );
637}
638
639
641{
642 try
643 {
644 const QgsCoordinateTransform ct = layerTransform( layer );
645 if ( ct.isValid() )
647 }
648 catch ( QgsCsException &cse )
649 {
650 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
651 }
652
653 return rect;
654}
655
656
658{
659 try
660 {
661 const QgsCoordinateTransform ct = layerTransform( layer );
662 if ( ct.isValid() )
663 point = ct.transform( point, Qgis::TransformDirection::Reverse );
664 }
665 catch ( QgsCsException &cse )
666 {
667 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
668 }
669
670 return point;
671}
672
674{
675 double x = point.x();
676 double y = point.y();
677 double z = point.z();
678 const double m = point.m();
679
680 try
681 {
682 const QgsCoordinateTransform ct = layerTransform( layer );
683 if ( ct.isValid() )
685 }
686 catch ( QgsCsException &cse )
687 {
688 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
689 }
690
691 return QgsPoint( x, y, z, m );
692}
693
694
696{
697 try
698 {
699 const QgsCoordinateTransform ct = layerTransform( layer );
700 if ( ct.isValid() )
702 }
703 catch ( QgsCsException &cse )
704 {
705 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
706 }
707
708 return rect;
709}
710
711
713{
714 // reset the map canvas extent since the extent may now be smaller
715 // We can't use a constructor since QgsRectangle normalizes the rectangle upon construction
717 fullExtent.setNull();
718
719 // iterate through the map layers and test each layers extent
720 // against the current min and max values
721 QgsDebugMsgLevel( u"Layer count: %1"_s.arg( mLayers.count() ), 5 );
722 const auto constMLayers = mLayers;
723 for ( const QgsWeakMapLayerPointer &layerPtr : constMLayers )
724 {
725 if ( QgsMapLayer *lyr = layerPtr.data() )
726 {
727 QgsDebugMsgLevel( "Updating extent using " + lyr->name(), 5 );
728 QgsDebugMsgLevel( "Input extent: " + lyr->extent().toString(), 5 );
729
730 if ( lyr->extent().isNull() )
731 continue;
732
733 // Layer extents are stored in the coordinate system (CS) of the
734 // layer. The extent must be projected to the canvas CS
735 const QgsRectangle extent = layerExtentToOutputExtent( lyr, lyr->extent() );
736
737 QgsDebugMsgLevel( "Output extent: " + extent.toString(), 5 );
738 fullExtent.combineExtentWith( extent );
739 }
740 }
741
742 if ( fullExtent.width() == 0.0 || fullExtent.height() == 0.0 )
743 {
744 // If all of the features are at the one point, buffer the
745 // rectangle a bit. If they are all at zero, do something a bit
746 // more crude.
747
748 if ( fullExtent.xMinimum() == 0.0 && fullExtent.xMaximum() == 0.0 && fullExtent.yMinimum() == 0.0 && fullExtent.yMaximum() == 0.0 )
749 {
750 fullExtent.set( -1.0, -1.0, 1.0, 1.0 );
751 }
752 else
753 {
754 const double padFactor = 1e-8;
755 const double widthPad = fullExtent.xMinimum() * padFactor;
756 const double heightPad = fullExtent.yMinimum() * padFactor;
757 const double xmin = fullExtent.xMinimum() - widthPad;
758 const double xmax = fullExtent.xMaximum() + widthPad;
759 const double ymin = fullExtent.yMinimum() - heightPad;
760 const double ymax = fullExtent.yMaximum() + heightPad;
761 fullExtent.set( xmin, ymin, xmax, ymax );
762 }
763 }
764
765 QgsDebugMsgLevel( "Full extent: " + fullExtent.toString(), 5 );
766 return fullExtent;
767}
768
769
770void QgsMapSettings::readXml( QDomNode &node )
771{
772 // set destination CRS
774 const QDomNode srsNode = node.namedItem( u"destinationsrs"_s );
775 if ( !srsNode.isNull() )
776 {
777 srs.readXml( srsNode );
778 }
779 setDestinationCrs( srs );
780
781 // set extent
782 const QDomNode extentNode = node.namedItem( u"extent"_s );
783 const QgsRectangle aoi = QgsXmlUtils::readRectangle( extentNode.toElement() );
784 setExtent( aoi );
785
786 // set rotation
787 const QDomNode rotationNode = node.namedItem( u"rotation"_s );
788 const QString rotationVal = rotationNode.toElement().text();
789 if ( !rotationVal.isEmpty() )
790 {
791 const double rot = rotationVal.toDouble();
792 setRotation( rot );
793 }
794
795 //render map tile
796 const QDomElement renderMapTileElem = node.firstChildElement( u"rendermaptile"_s );
797 if ( !renderMapTileElem.isNull() )
798 {
799 setFlag( Qgis::MapSettingsFlag::RenderMapTile, renderMapTileElem.text() == "1"_L1 );
800 }
801}
802
803
804void QgsMapSettings::writeXml( QDomNode &node, QDomDocument &doc )
805{
806 // units
807 node.appendChild( QgsXmlUtils::writeMapUnits( mapUnits(), doc ) );
808
809 // Write current view extents
810 node.appendChild( QgsXmlUtils::writeRectangle( extent(), doc ) );
811
812 // Write current view rotation
813 QDomElement rotNode = doc.createElement( u"rotation"_s );
814 rotNode.appendChild( doc.createTextNode( qgsDoubleToString( rotation() ) ) );
815 node.appendChild( rotNode );
816
817 // destination CRS
818 if ( mDestCRS.isValid() )
819 {
820 QDomElement srsNode = doc.createElement( u"destinationsrs"_s );
821 node.appendChild( srsNode );
822 mDestCRS.writeXml( srsNode, doc );
823 }
824
825 //render map tile
826 QDomElement renderMapTileElem = doc.createElement( u"rendermaptile"_s );
827 const QDomText renderMapTileText = doc.createTextNode( testFlag( Qgis::MapSettingsFlag::RenderMapTile ) ? "1" : "0" );
828 renderMapTileElem.appendChild( renderMapTileText );
829 node.appendChild( renderMapTileElem );
830}
831
836
838{
839 mLabelBoundaryGeometry = boundary;
840}
841
843{
844 mClippingRegions.append( region );
845}
846
847void QgsMapSettings::setClippingRegions( const QList<QgsMapClippingRegion> &regions )
848{
849 mClippingRegions = regions;
850}
851
852QList<QgsMapClippingRegion> QgsMapSettings::clippingRegions() const
853{
854 return mClippingRegions;
855}
856
858{
859 mMaskRenderSettings = settings;
860}
861
863{
864 mRenderedFeatureHandlers.append( handler );
865}
866
867QList<QgsRenderedFeatureHandlerInterface *> QgsMapSettings::renderedFeatureHandlers() const
868{
869 return mRenderedFeatureHandlers;
870}
871
873{
874 return mZRange;
875}
876
878{
879 mZRange = zRange;
880}
881
886
891
893{
894 return mFrameRate;
895}
896
898{
899 mFrameRate = rate;
900}
901
903{
904 return mCurrentFrame;
905}
906
907void QgsMapSettings::setCurrentFrame( long long frame )
908{
909 mCurrentFrame = frame;
910}
911
916
921
926
946
947QHash<QString, QgsSelectiveMaskingSourceSet> QgsMapSettings::selectiveMaskingSourceSets() const
948{
950}
951
952void QgsMapSettings::setSelectiveMaskingSourceSets( const QVector<QgsSelectiveMaskingSourceSet> &sets )
953{
955 mSelectiveMaskingSourceSets.reserve( sets.size() );
956 for ( const QgsSelectiveMaskingSourceSet &set : sets )
957 {
958 mSelectiveMaskingSourceSets.insert( set.id(), set );
959 }
960}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
RasterizedRenderingPolicy
Policies controlling when rasterisation of content during renders is permitted.
Definition qgis.h:2865
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2866
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2867
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2868
QFlags< MapSettingsFlag > MapSettingsFlags
Map settings flags.
Definition qgis.h:2902
@ NoSimplification
No simplification can be applied.
Definition qgis.h:3198
DistanceUnit
Units of distance.
Definition qgis.h:5326
@ Feet
Imperial feet.
Definition qgis.h:5329
@ Unknown
Unknown distance unit.
Definition qgis.h:5376
@ Degrees
Degrees, for planar geographic CRS distance measurements.
Definition qgis.h:5333
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5603
RendererUsage
Usage of the renderer.
Definition qgis.h:3626
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2832
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2833
MapSettingsFlag
Flags which adjust the way maps are rendered.
Definition qgis.h:2878
@ RenderMapTile
Draw map such that there are no problems between adjacent tiles.
Definition qgis.h:2887
@ ForceVectorOutput
Vector graphics should not be cached and drawn as raster images.
Definition qgis.h:2881
@ UseAdvancedEffects
Enable layer opacity and blending effects.
Definition qgis.h:2882
Represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source coordinate reference system, which the transform will transform coordinates from.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
double scaleFactor(const QgsRectangle &referenceExtent) const
Computes an estimated conversion factor between source and destination units:
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system, which the transform will transform coordinates t...
Custom exception class for Coordinate Reference System related exceptions.
QgsRange which stores a range of double values.
Definition qgsrange.h:217
Renders elevation shading on an image with different methods (eye dome lighting, hillshading,...
static EllipsoidParameters ellipsoidParameters(const QString &ellipsoid)
Returns the parameters for the specified ellipsoid.
QString what() const
A geometry is the spatial representation of a feature.
A map layer which consists of a set of child layers, where all component layers are rendered as a sin...
A map clipping region (in map coordinates and CRS).
Base class for all map layer types.
Definition qgsmaplayer.h:83
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
QString id
Definition qgsmaplayer.h:86
Qgis::LayerType type
Definition qgsmaplayer.h:93
static bool isValidExtent(const QgsRectangle &extent)
Returns true if an extent is a valid extent which can be used by QgsMapSettings.
void setElevationShadingRenderer(const QgsElevationShadingRenderer &renderer)
Sets the shading renderer used to render shading on the entire map.
QSize deviceOutputSize() const
Returns the device output size of the map render.
Qgis::DistanceUnit mapUnits() const
Returns the units of the map's geographical coordinates - used for scale calculation.
Qgis::RendererUsage rendererUsage() const
Returns the rendering usage.
QgsVectorSimplifyMethod mSimplifyMethod
void addClippingRegion(const QgsMapClippingRegion &region)
Adds a new clipping region to the map settings.
void writeXml(QDomNode &node, QDomDocument &doc)
Writes the map settings to an XML node.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
QgsMaskRenderSettings mMaskRenderSettings
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
Qgis::RendererUsage mRendererUsage
QgsRectangle mVisibleExtent
Extent with some additional white space that matches the output aspect ratio.
QPolygonF visiblePolygon() const
Returns the visible area as a polygon (may be rotated).
void addRenderedFeatureHandler(QgsRenderedFeatureHandlerInterface *handler)
Adds a rendered feature handler to use while rendering the map settings.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
double scale() const
Returns the calculated map scale.
void setFrameRate(double rate)
Sets the frame rate of the map (in frames per second), for maps which are part of an animation.
void setFlags(Qgis::MapSettingsFlags flags)
Sets combination of flags that will be used for rendering.
Qgis::MapSettingsFlags mFlags
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
QgsDoubleRange zRange() const
Returns the range of z-values which will be visible in the map.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
void setScaleMethod(Qgis::ScaleCalculationMethod method)
Sets the method to use for scale calculations for the map.
void setDpiTarget(double dpi)
Sets the target dpi (dots per inch) to be taken into consideration when rendering.
double magnificationFactor() const
Returns the magnification factor.
QHash< QString, QgsSelectiveMaskingSourceSet > mSelectiveMaskingSourceSets
QStringList mLayerIds
QgsGeometry labelBoundaryGeometry() const
Returns the label boundary geometry, which restricts where in the rendered map labels are permitted t...
QList< QgsRenderedFeatureHandlerInterface * > renderedFeatureHandlers() const
Returns the list of rendered feature handlers to use while rendering the map settings.
QStringList layerIds(bool expandGroupLayers=false) const
Returns the list of layer IDs which will be rendered in the map.
void setDevicePixelRatio(float dpr)
Sets the device pixel ratio.
QString mEllipsoid
ellipsoid acronym (from table tbl_ellipsoids)
void setZRange(const QgsDoubleRange &range)
Sets the range of z-values which will be visible in the map.
double dpiTarget() const
Returns the target DPI (dots per inch) to be taken into consideration when rendering.
long long currentFrame() const
Returns the current frame number of the map, for maps which are part of an animation.
bool mValid
Whether the actual settings are valid (set in updateDerived()).
void setOutputDpi(double dpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
const QgsMapToPixel & mapToPixel() const
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
void setRendererUsage(Qgis::RendererUsage rendererUsage)
Sets the rendering usage.
float devicePixelRatio() const
Returns the device pixel ratio.
void setRasterizedRenderingPolicy(Qgis::RasterizedRenderingPolicy policy)
Sets the policy controlling when rasterisation of content during renders is permitted.
QgsRectangle mExtent
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
void setMaskSettings(const QgsMaskRenderSettings &settings)
Sets the mask render settings, which control how masks are drawn and behave during the map render.
QMap< QString, QString > mLayerStyleOverrides
QgsGeometry mLabelBoundaryGeometry
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
QgsElevationShadingRenderer mShadingRenderer
QgsCoordinateTransformContext mTransformContext
void setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
void setClippingRegions(const QList< QgsMapClippingRegion > &regions)
Sets the list of clipping regions to apply to the map.
double layerToMapUnits(const QgsMapLayer *layer, const QgsRectangle &referenceExtent=QgsRectangle()) const
Computes an estimated conversion factor between layer and map units, where layerUnits × layerToMapUni...
double extentBuffer() const
Returns the buffer in map units to use around the visible extent for rendering symbols whose correspo...
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
void setSelectiveMaskingSourceSets(const QVector< QgsSelectiveMaskingSourceSet > &sets)
Sets a list of all selective masking source sets defined for the map.
QgsScaleCalculator mScaleCalculator
Qgis::MapSettingsFlags flags() const
Returns combination of flags used for rendering.
Qgis::ScaleCalculationMethod scaleMethod() const
Returns the method to use for scale calculations for the map.
double frameRate() const
Returns the frame rate of the map (in frames per second), for maps which are part of an animation.
void setExtentBuffer(double buffer)
Sets the buffer in map units to use around the visible extent for rendering symbols whose correspondi...
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
QgsRectangle outputExtentToLayerExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from output CRS to layer's CRS
QgsRectangle fullExtent() const
returns current extent of layer set
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
double computeScaleForExtent(const QgsRectangle &extent) const
Compute the scale that corresponds to the specified extent.
QString ellipsoid() const
Returns ellipsoid's acronym.
double mMagnificationFactor
void setCurrentFrame(long long frame)
Sets the current frame of the map, for maps which are part of an animation.
const QgsElevationShadingRenderer & elevationShadingRenderer() const
Returns the shading renderer used to render shading on the entire map.
QHash< QString, QgsSelectiveMaskingSourceSet > selectiveMaskingSourceSets() const
Returns a hash of all selective masking source sets defined for the map.
QgsCoordinateReferenceSystem mDestCRS
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
void setLabelBoundaryGeometry(const QgsGeometry &boundary)
Sets the label boundary geometry, which restricts where in the rendered map labels are permitted to b...
Qgis::RasterizedRenderingPolicy mRasterizedRenderingPolicy
QgsMapToPixel mMapToPixel
bool testFlag(Qgis::MapSettingsFlag flag) const
Check whether a particular flag is enabled.
QMap< QString, QString > layerStyleOverrides() const
Returns the map of map layer style overrides (key: layer ID, value: style name) where a different sty...
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
QList< QgsMapClippingRegion > clippingRegions() const
Returns the list of clipping regions to apply to the map.
bool hasValidSettings() const
Check whether the map settings are valid and can be used for rendering.
QgsWeakMapLayerPointerList mLayers
list of layers to be rendered (stored as weak pointers)
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
long long mCurrentFrame
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
QPolygonF visiblePolygonWithBuffer() const
Returns the visible area as a polygon (may be rotated) with extent buffer included.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
void readXml(QDomNode &node)
Restore the map settings from a XML node.
double mSegmentationTolerance
QgsRectangle computeExtentForScale(const QgsPointXY &center, double scale) const
Compute the extent such that its center is at the specified position (mapped to the destinatonCrs) an...
void setMagnificationFactor(double factor, const QgsPointXY *center=nullptr)
Set the magnification factor.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Perform transforms between map coordinates and device coordinates.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
Contains settings regarding how masks are calculated and handled during a map render.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Contains miscellaneous painting utility functions.
Definition qgspainting.h:32
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:168
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double z
Definition qgspoint.h:58
double x
Definition qgspoint.h:56
double m
Definition qgspoint.h:59
double y
Definition qgspoint.h:57
A rectangle specified with double values.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
An interface for classes which provide custom handlers for features rendered as part of a map render ...
Represents a named set of selective masking sources (QgsSelectiveMaskSource).
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=u"extent"_s)
Encodes a rectangle to a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
static QDomElement writeMapUnits(Qgis::DistanceUnit units, QDomDocument &doc)
Encodes a distance unit to a DOM element.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:7052
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7134
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Contains parameters for an ellipsoid.
bool valid
Whether ellipsoid parameters are valid.