QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 mDestCRS = crs;
356 mScaleCalculator.setMapUnits( crs.mapUnits() );
357 // Since the map units have changed, force a recalculation of the scale.
359}
360
365
367{
369 if ( !params.valid )
370 {
371 return false;
372 }
373 else
374 {
376 return true;
377 }
378}
379
381{
382 mFlags = flags;
383 matchRasterizedRenderingPolicyToFlags();
384}
385
387{
388 if ( on )
389 mFlags |= flag;
390 else
391 mFlags &= ~( static_cast< int >( flag ) );
392 matchRasterizedRenderingPolicyToFlags();
393}
394
399
401{
402 return mFlags.testFlag( flag );
403}
404
406{
407 return mScaleCalculator.mapUnits();
408}
409
414
416{
417 mScaleCalculator.setMethod( method );
419}
420
422{
423 return mValid;
424}
425
430
432{
433 QPolygonF poly;
434
435 const QSize &sz = outputSize();
436 const QgsMapToPixel &m2p = mapToPixel();
437
438 poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
439 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
440 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
441 poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();
442
443 return poly;
444}
445
447{
448 QPolygonF poly;
449
450 const QSize &sz = outputSize();
451 const QgsMapToPixel &m2p = mapToPixel();
452
453 // Transform tilebuffer in pixel.
454 // Original tilebuffer is in pixel and transformed only according
455 // extent width (see QgsWmsRenderContext::mapTileBuffer)
456
457 const double mapUnitsPerPixel = mExtent.width() / sz.width();
458 const double buffer = mExtentBuffer / mapUnitsPerPixel;
459
460 poly << m2p.toMapCoordinates( -buffer, -buffer ).toQPointF();
461 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), -buffer ).toQPointF();
462 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), static_cast<double>( sz.height() + buffer ) ).toQPointF();
463 poly << m2p.toMapCoordinates( -buffer, static_cast<double>( sz.height() + buffer ) ).toQPointF();
464
465 return poly;
466}
467
469{
470 return mMapUnitsPerPixel;
471}
472
474{
475 return mScale;
476}
477
479{
480#ifdef QGISDEBUG
481 if ( !mHasTransformContext )
482 QgsDebugMsgLevel( u"No QgsCoordinateTransformContext context set for transform"_s, 4 );
483#endif
484
485 return mTransformContext;
486}
487
489{
490 mTransformContext = context;
491#ifdef QGISDEBUG
492 mHasTransformContext = true;
493#endif
494}
495
497{
498 if ( !layer )
499 return QgsCoordinateTransform();
500
502}
503
505{
506 // Output width in inches
507 const double outputWidthInInches = outputSize().width() / outputDpi();
508
509 // Desired visible width (honouring scale)
510 const double scaledWidthInInches = outputWidthInInches * scale;
511
513 {
514 // Start with some fraction of the current extent around the center
515 const double delta = mExtent.width() / 100.;
516 QgsRectangle ext( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
517 // Get scale at extent, and then scale extent to the desired scale
518 const double testScale = mScaleCalculator.calculate( ext, outputSize().width() );
519 ext.scale( scale / testScale );
520 return ext;
521 }
522 else
523 {
524 // Conversion from inches to mapUnits - this is safe to use, because we know here that the map units AREN'T in degrees
525 const double conversionFactor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Feet, mapUnits() ) / 12;
526
527 const double delta = 0.5 * scaledWidthInInches * conversionFactor;
528 return QgsRectangle( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
529 }
530}
531
533{
534 return mScaleCalculator.calculate( extent, outputSize().width() );
535}
536
537double QgsMapSettings::layerToMapUnits( const QgsMapLayer *layer, const QgsRectangle &referenceExtent ) const
538{
539 return layerTransform( layer ).scaleFactor( referenceExtent );
540}
541
542
544{
545 try
546 {
548 if ( ct.isValid() )
549 {
550 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
551 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
552 QgsDebugMsgLevel( u"extent %1"_s.arg( extent.toString() ), 3 );
555 }
556 }
557 catch ( QgsCsException &cse )
558 {
559 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
560 }
561
562 QgsDebugMsgLevel( u"proj extent = %1 "_s.arg( extent.toString() ), 3 );
563
564 return extent;
565}
566
567
569{
570 try
571 {
574 if ( ct.isValid() )
575 {
576 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
577 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
578 QgsDebugMsgLevel( u"extent = %1"_s.arg( extent.toString() ), 3 );
580 }
581 }
582 catch ( QgsCsException &cse )
583 {
584 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
585 }
586
587 QgsDebugMsgLevel( u"proj extent = %1"_s.arg( extent.toString() ), 3 );
588
589 return extent;
590}
591
592
594{
595 try
596 {
597 const QgsCoordinateTransform ct = layerTransform( layer );
598 if ( ct.isValid() )
599 point = ct.transform( point, Qgis::TransformDirection::Forward );
600 }
601 catch ( QgsCsException &cse )
602 {
603 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
604 }
605
606 return point;
607}
608
610{
611 double x = point.x();
612 double y = point.y();
613 double z = point.z();
614 const double m = point.m();
615
616 try
617 {
618 const QgsCoordinateTransform ct = layerTransform( layer );
619 if ( ct.isValid() )
621 }
622 catch ( QgsCsException &cse )
623 {
624 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
625 }
626
627 return QgsPoint( x, y, z, m );
628}
629
630
632{
633 try
634 {
635 const QgsCoordinateTransform ct = layerTransform( layer );
636 if ( ct.isValid() )
638 }
639 catch ( QgsCsException &cse )
640 {
641 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
642 }
643
644 return rect;
645}
646
647
649{
650 try
651 {
652 const QgsCoordinateTransform ct = layerTransform( layer );
653 if ( ct.isValid() )
654 point = ct.transform( point, Qgis::TransformDirection::Reverse );
655 }
656 catch ( QgsCsException &cse )
657 {
658 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
659 }
660
661 return point;
662}
663
665{
666 double x = point.x();
667 double y = point.y();
668 double z = point.z();
669 const double m = point.m();
670
671 try
672 {
673 const QgsCoordinateTransform ct = layerTransform( layer );
674 if ( ct.isValid() )
676 }
677 catch ( QgsCsException &cse )
678 {
679 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
680 }
681
682 return QgsPoint( x, y, z, m );
683}
684
685
687{
688 try
689 {
690 const QgsCoordinateTransform ct = layerTransform( layer );
691 if ( ct.isValid() )
693 }
694 catch ( QgsCsException &cse )
695 {
696 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
697 }
698
699 return rect;
700}
701
702
704{
705 // reset the map canvas extent since the extent may now be smaller
706 // We can't use a constructor since QgsRectangle normalizes the rectangle upon construction
708 fullExtent.setNull();
709
710 // iterate through the map layers and test each layers extent
711 // against the current min and max values
712 QgsDebugMsgLevel( u"Layer count: %1"_s.arg( mLayers.count() ), 5 );
713 const auto constMLayers = mLayers;
714 for ( const QgsWeakMapLayerPointer &layerPtr : constMLayers )
715 {
716 if ( QgsMapLayer *lyr = layerPtr.data() )
717 {
718 QgsDebugMsgLevel( "Updating extent using " + lyr->name(), 5 );
719 QgsDebugMsgLevel( "Input extent: " + lyr->extent().toString(), 5 );
720
721 if ( lyr->extent().isNull() )
722 continue;
723
724 // Layer extents are stored in the coordinate system (CS) of the
725 // layer. The extent must be projected to the canvas CS
726 const QgsRectangle extent = layerExtentToOutputExtent( lyr, lyr->extent() );
727
728 QgsDebugMsgLevel( "Output extent: " + extent.toString(), 5 );
729 fullExtent.combineExtentWith( extent );
730 }
731 }
732
733 if ( fullExtent.width() == 0.0 || fullExtent.height() == 0.0 )
734 {
735 // If all of the features are at the one point, buffer the
736 // rectangle a bit. If they are all at zero, do something a bit
737 // more crude.
738
739 if ( fullExtent.xMinimum() == 0.0 && fullExtent.xMaximum() == 0.0 && fullExtent.yMinimum() == 0.0 && fullExtent.yMaximum() == 0.0 )
740 {
741 fullExtent.set( -1.0, -1.0, 1.0, 1.0 );
742 }
743 else
744 {
745 const double padFactor = 1e-8;
746 const double widthPad = fullExtent.xMinimum() * padFactor;
747 const double heightPad = fullExtent.yMinimum() * padFactor;
748 const double xmin = fullExtent.xMinimum() - widthPad;
749 const double xmax = fullExtent.xMaximum() + widthPad;
750 const double ymin = fullExtent.yMinimum() - heightPad;
751 const double ymax = fullExtent.yMaximum() + heightPad;
752 fullExtent.set( xmin, ymin, xmax, ymax );
753 }
754 }
755
756 QgsDebugMsgLevel( "Full extent: " + fullExtent.toString(), 5 );
757 return fullExtent;
758}
759
760
761void QgsMapSettings::readXml( QDomNode &node )
762{
763 // set destination CRS
765 const QDomNode srsNode = node.namedItem( u"destinationsrs"_s );
766 if ( !srsNode.isNull() )
767 {
768 srs.readXml( srsNode );
769 }
770 setDestinationCrs( srs );
771
772 // set extent
773 const QDomNode extentNode = node.namedItem( u"extent"_s );
774 const QgsRectangle aoi = QgsXmlUtils::readRectangle( extentNode.toElement() );
775 setExtent( aoi );
776
777 // set rotation
778 const QDomNode rotationNode = node.namedItem( u"rotation"_s );
779 const QString rotationVal = rotationNode.toElement().text();
780 if ( !rotationVal.isEmpty() )
781 {
782 const double rot = rotationVal.toDouble();
783 setRotation( rot );
784 }
785
786 //render map tile
787 const QDomElement renderMapTileElem = node.firstChildElement( u"rendermaptile"_s );
788 if ( !renderMapTileElem.isNull() )
789 {
790 setFlag( Qgis::MapSettingsFlag::RenderMapTile, renderMapTileElem.text() == "1"_L1 );
791 }
792}
793
794
795void QgsMapSettings::writeXml( QDomNode &node, QDomDocument &doc )
796{
797 // units
798 node.appendChild( QgsXmlUtils::writeMapUnits( mapUnits(), doc ) );
799
800 // Write current view extents
801 node.appendChild( QgsXmlUtils::writeRectangle( extent(), doc ) );
802
803 // Write current view rotation
804 QDomElement rotNode = doc.createElement( u"rotation"_s );
805 rotNode.appendChild( doc.createTextNode( qgsDoubleToString( rotation() ) ) );
806 node.appendChild( rotNode );
807
808 // destination CRS
809 if ( mDestCRS.isValid() )
810 {
811 QDomElement srsNode = doc.createElement( u"destinationsrs"_s );
812 node.appendChild( srsNode );
813 mDestCRS.writeXml( srsNode, doc );
814 }
815
816 //render map tile
817 QDomElement renderMapTileElem = doc.createElement( u"rendermaptile"_s );
818 const QDomText renderMapTileText = doc.createTextNode( testFlag( Qgis::MapSettingsFlag::RenderMapTile ) ? "1" : "0" );
819 renderMapTileElem.appendChild( renderMapTileText );
820 node.appendChild( renderMapTileElem );
821}
822
827
829{
830 mLabelBoundaryGeometry = boundary;
831}
832
834{
835 mClippingRegions.append( region );
836}
837
838void QgsMapSettings::setClippingRegions( const QList<QgsMapClippingRegion> &regions )
839{
840 mClippingRegions = regions;
841}
842
843QList<QgsMapClippingRegion> QgsMapSettings::clippingRegions() const
844{
845 return mClippingRegions;
846}
847
849{
850 mMaskRenderSettings = settings;
851}
852
854{
855 mRenderedFeatureHandlers.append( handler );
856}
857
858QList<QgsRenderedFeatureHandlerInterface *> QgsMapSettings::renderedFeatureHandlers() const
859{
860 return mRenderedFeatureHandlers;
861}
862
864{
865 return mZRange;
866}
867
869{
870 mZRange = zRange;
871}
872
877
882
884{
885 return mFrameRate;
886}
887
889{
890 mFrameRate = rate;
891}
892
894{
895 return mCurrentFrame;
896}
897
898void QgsMapSettings::setCurrentFrame( long long frame )
899{
900 mCurrentFrame = frame;
901}
902
907
912
917
937
938QHash<QString, QgsSelectiveMaskingSourceSet> QgsMapSettings::selectiveMaskingSourceSets() const
939{
941}
942
943void QgsMapSettings::setSelectiveMaskingSourceSets( const QVector<QgsSelectiveMaskingSourceSet> &sets )
944{
946 mSelectiveMaskingSourceSets.reserve( sets.size() );
947 for ( const QgsSelectiveMaskingSourceSet &set : sets )
948 {
949 mSelectiveMaskingSourceSets.insert( set.id(), set );
950 }
951}
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:2798
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2799
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2800
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2801
QFlags< MapSettingsFlag > MapSettingsFlags
Map settings flags.
Definition qgis.h:2835
@ NoSimplification
No simplification can be applied.
Definition qgis.h:3131
DistanceUnit
Units of distance.
Definition qgis.h:5170
@ Feet
Imperial feet.
Definition qgis.h:5173
@ Unknown
Unknown distance unit.
Definition qgis.h:5220
@ Degrees
Degrees, for planar geographic CRS distance measurements.
Definition qgis.h:5177
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5447
RendererUsage
Usage of the renderer.
Definition qgis.h:3559
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2765
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
MapSettingsFlag
Flags which adjust the way maps are rendered.
Definition qgis.h:2811
@ RenderMapTile
Draw map such that there are no problems between adjacent tiles.
Definition qgis.h:2820
@ ForceVectorOutput
Vector graphics should not be cached and drawn as raster images.
Definition qgis.h:2814
@ UseAdvancedEffects
Enable layer opacity and blending effects.
Definition qgis.h:2815
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:6893
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
#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.