QGIS API Documentation 4.1.0-Master (659fe69c07c)
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 , mEllipsoid( Qgis::geoNone() )
42 , mBackgroundColor( Qt::white )
43 , mSelectionColor( Qt::yellow )
44 , mFlags( Qgis::MapSettingsFlag::Antialiasing | Qgis::MapSettingsFlag::UseAdvancedEffects | Qgis::MapSettingsFlag::DrawLabeling | Qgis::MapSettingsFlag::DrawSelection )
45 , mSegmentationTolerance( M_PI_2 / 90 )
46{
49
51}
52
53void QgsMapSettings::setMagnificationFactor( double factor, const QgsPointXY *center )
54{
55 const double ratio = mMagnificationFactor / factor;
56
57 mMagnificationFactor = factor;
58
59 const double rot = rotation();
60 setRotation( 0.0 );
61
63 ext.scale( ratio, center );
64
65 mRotation = rot;
66 mExtent = ext;
67 mDpi = mDpi / ratio;
68
69 QgsDebugMsgLevel( u"Magnification factor: %1 dpi: %2 ratio: %3"_s.arg( factor ).arg( mDpi ).arg( ratio ), 3 );
70
72}
73
78
80{
81 return mExtent;
82}
83
84void QgsMapSettings::setExtent( const QgsRectangle &extent, bool magnified )
85{
86 QgsRectangle magnifiedExtent = extent;
87
88 if ( !magnified )
89 magnifiedExtent.scale( 1 / mMagnificationFactor );
90
91 mExtent = magnifiedExtent;
92
94}
95
97{
98 return mExtentBuffer;
99}
100
101void QgsMapSettings::setExtentBuffer( const double buffer )
102{
103 mExtentBuffer = buffer;
104}
105
107{
108 return mRotation;
109}
110
111void QgsMapSettings::setRotation( double degrees )
112{
113 if ( qgsDoubleNear( mRotation, degrees ) )
114 return;
115
116 mRotation = degrees;
117
118 // TODO: update extent while keeping scale ?
120}
121
122
124{
126
127 // Don't allow zooms where the current extent is so small that it
128 // can't be accurately represented using a double (which is what
129 // currentExtent uses).
131 {
132 mValid = false;
133 return;
134 }
135
136 const int widthPixels = mSize.width();
137 const int heightPixels = mSize.height();
138 if ( widthPixels == 0 || heightPixels == 0 )
139 {
140 mValid = false;
141 return;
142 }
143
144 // calculate the translation and scaling parameters
145 const double mapUnitsPerPixelY = mExtent.height() / static_cast< double >( heightPixels );
146 const double mapUnitsPerPixelX = mExtent.width() / static_cast< double >( widthPixels );
147 mMapUnitsPerPixel = mapUnitsPerPixelY > mapUnitsPerPixelX ? mapUnitsPerPixelY : mapUnitsPerPixelX;
148
149 // calculate the actual extent of the mapCanvas
150 double dxmin = mExtent.xMinimum(), dxmax = mExtent.xMaximum(), dymin = mExtent.yMinimum(), dymax = mExtent.yMaximum(), whitespace;
151
152 if ( mapUnitsPerPixelY > mapUnitsPerPixelX )
153 {
154 whitespace = ( ( widthPixels * mMapUnitsPerPixel ) - mExtent.width() ) * 0.5;
155 dxmin -= whitespace;
156 dxmax += whitespace;
157 }
158 else
159 {
160 whitespace = ( ( heightPixels * mMapUnitsPerPixel ) - mExtent.height() ) * 0.5;
161 dymin -= whitespace;
162 dymax += whitespace;
163 }
164
165 mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
166
167 // update the scale
168 mScaleCalculator.setDpi( mDpi );
169 mScale = mScaleCalculator.calculate( mVisibleExtent, widthPixels );
170
171 bool ok = true;
172 mMapToPixel.setParameters( mapUnitsPerPixel(), visibleExtent().center().x(), visibleExtent().center().y(), outputSize().width(), outputSize().height(), mRotation, &ok );
173
174 mValid = ok;
175
176 // set visible extent taking rotation in consideration
177 if ( !qgsDoubleNear( mRotation, 0 ) )
178 {
179 const QgsPointXY p1 = mMapToPixel.toMapCoordinates( QPoint( 0, 0 ) );
180 const QgsPointXY p2 = mMapToPixel.toMapCoordinates( QPoint( 0, heightPixels ) );
181 const QgsPointXY p3 = mMapToPixel.toMapCoordinates( QPoint( widthPixels, 0 ) );
182 const QgsPointXY p4 = mMapToPixel.toMapCoordinates( QPoint( widthPixels, heightPixels ) );
183 dxmin = std::min( p1.x(), std::min( p2.x(), std::min( p3.x(), p4.x() ) ) );
184 dymin = std::min( p1.y(), std::min( p2.y(), std::min( p3.y(), p4.y() ) ) );
185 dxmax = std::max( p1.x(), std::max( p2.x(), std::max( p3.x(), p4.x() ) ) );
186 dymax = std::max( p1.y(), std::max( p2.y(), std::max( p3.y(), p4.y() ) ) );
187 mVisibleExtent.set( dxmin, dymin, dxmax, dymax );
188 }
189
190 QgsDebugMsgLevel( u"Map units per pixel (x,y) : %1, %2"_s.arg( qgsDoubleToString( mapUnitsPerPixelX ), qgsDoubleToString( mapUnitsPerPixelY ) ), 5 );
191 QgsDebugMsgLevel( u"Pixmap dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( widthPixels ), qgsDoubleToString( heightPixels ) ), 5 );
192 QgsDebugMsgLevel( u"Extent dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( mExtent.width() ), qgsDoubleToString( mExtent.height() ) ), 5 );
193 QgsDebugMsgLevel( mExtent.toString(), 5 );
194 QgsDebugMsgLevel( u"Adjusted map units per pixel (x,y) : %1, %2"_s.arg( qgsDoubleToString( mVisibleExtent.width() / widthPixels ), qgsDoubleToString( mVisibleExtent.height() / heightPixels ) ), 5 );
195 QgsDebugMsgLevel( u"Recalced pixmap dimensions (x,y) : %1, %2"_s.arg( qgsDoubleToString( mVisibleExtent.width() / mMapUnitsPerPixel ), qgsDoubleToString( mVisibleExtent.height() / mMapUnitsPerPixel ) ), 5 );
196 QgsDebugMsgLevel( u"Scale (assuming meters as map units) = 1:%1"_s.arg( qgsDoubleToString( mScale ) ), 5 );
197 QgsDebugMsgLevel( u"Rotation: %1 degrees"_s.arg( mRotation ), 5 );
198 QgsDebugMsgLevel( u"Extent: %1"_s.arg( mExtent.asWktCoordinates() ), 5 );
199 QgsDebugMsgLevel( u"Visible Extent: %1"_s.arg( mVisibleExtent.asWktCoordinates() ), 5 );
200 QgsDebugMsgLevel( u"Magnification factor: %1"_s.arg( mMagnificationFactor ), 5 );
201}
202
203void QgsMapSettings::matchRasterizedRenderingPolicyToFlags()
204{
211}
212
214{
215 return mSize;
216}
217
219{
220 mSize = size;
221
223}
224
226{
227 return mDevicePixelRatio;
228}
229
231{
232 mDevicePixelRatio = dpr;
234}
235
237{
238 return outputSize() * mDevicePixelRatio;
239}
240
242{
243 return mDpi;
244}
245
247{
248 mDpi = dpi;
249
251}
252
254{
255 return mDpiTarget;
256}
257
259{
260 mDpiTarget = dpi;
261}
262
263QStringList QgsMapSettings::layerIds( bool expandGroupLayers ) const
264{
265 if ( !expandGroupLayers || !mHasGroupLayers )
266 {
267 return mLayerIds;
268 }
269 else
270 {
271 const QList<QgsMapLayer * > mapLayers = layers( expandGroupLayers );
272 QStringList res;
273 res.reserve( mapLayers.size() );
274 for ( const QgsMapLayer *layer : mapLayers )
275 res << layer->id();
276 return res;
277 }
278}
279
280QList<QgsMapLayer *> QgsMapSettings::layers( bool expandGroupLayers ) const
281{
282 const QList<QgsMapLayer *> actualLayers = _qgis_listQPointerToRaw( mLayers );
283 if ( !expandGroupLayers )
284 return actualLayers;
285
286 QList< QgsMapLayer * > result;
287
288 std::function< void( const QList< QgsMapLayer * > &layers ) > expandLayers;
289 expandLayers = [&result, &expandLayers]( const QList< QgsMapLayer * > &layers ) {
290 for ( QgsMapLayer *layer : layers )
291 {
292 if ( QgsGroupLayer *groupLayer = qobject_cast< QgsGroupLayer * >( layer ) )
293 {
294 expandLayers( groupLayer->childLayers() );
295 }
296 else
297 {
298 result << layer;
299 }
300 }
301 };
302
303 expandLayers( actualLayers );
304 return result;
305}
306
307template<typename T> QVector<T> QgsMapSettings::layers() const
308{
309 const QList<QgsMapLayer *> actualLayers = _qgis_listQPointerToRaw( mLayers );
310
311 QVector<T> layers;
312 for ( QgsMapLayer *layer : actualLayers )
313 {
314 T tLayer = qobject_cast<T>( layer );
315 if ( tLayer )
316 {
317 layers << tLayer;
318 }
319 }
320 return layers;
321}
322
323void QgsMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
324{
325 // filter list, removing null layers and non-spatial layers
326 auto filteredList = layers;
327 filteredList.erase( std::remove_if( filteredList.begin(), filteredList.end(), []( QgsMapLayer *layer ) { return !layer || !layer->isSpatial(); } ), filteredList.end() );
328
329 mLayers = _qgis_listRawToQPointer( filteredList );
330
331 // pre-generate and store layer IDs, so that we can safely access them from other threads
332 // without needing to touch the actual map layer object
333 mLayerIds.clear();
334 mHasGroupLayers = false;
335 mLayerIds.reserve( mLayers.size() );
336 for ( const QgsMapLayer *layer : std::as_const( mLayers ) )
337 {
338 mLayerIds << layer->id();
339 if ( layer->type() == Qgis::LayerType::Group )
340 mHasGroupLayers = true;
341 }
342}
343
344QMap<QString, QString> QgsMapSettings::layerStyleOverrides() const
345{
347}
348
349void QgsMapSettings::setLayerStyleOverrides( const QMap<QString, QString> &overrides )
350{
351 mLayerStyleOverrides = overrides;
352}
353
355{
356 if ( crs == mDestCRS )
357 return;
358
359 mDestCRS = crs;
360 mScaleCalculator.setMapUnits( crs.mapUnits() );
361 // Since the map units have changed, force a recalculation of the scale.
363}
364
369
371{
372 if ( ellipsoid == mEllipsoid )
373 return true;
374
376 if ( !params.valid )
377 {
378 return false;
379 }
380 else
381 {
383 mScaleCalculator.setEllipsoid( ellipsoid );
384 // Since mScaleCalculator ellipsoid have changed, force a recalculation of the scale.
386 return true;
387 }
388}
389
391{
392 mFlags = flags;
393 matchRasterizedRenderingPolicyToFlags();
394}
395
397{
398 if ( on )
399 mFlags |= flag;
400 else
401 mFlags &= ~( static_cast< int >( flag ) );
402 matchRasterizedRenderingPolicyToFlags();
403}
404
409
411{
412 return mFlags.testFlag( flag );
413}
414
416{
417 return mScaleCalculator.mapUnits();
418}
419
424
426{
427 mScaleCalculator.setMethod( method );
429}
430
432{
433 return mValid;
434}
435
440
442{
443 QPolygonF poly;
444
445 const QSize &sz = outputSize();
446 const QgsMapToPixel &m2p = mapToPixel();
447
448 poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
449 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
450 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
451 poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();
452
453 return poly;
454}
455
457{
458 QPolygonF poly;
459
460 const QSize &sz = outputSize();
461 const QgsMapToPixel &m2p = mapToPixel();
462
463 // Transform tilebuffer in pixel.
464 // Original tilebuffer is in pixel and transformed only according
465 // extent width (see QgsWmsRenderContext::mapTileBuffer)
466
467 const double mapUnitsPerPixel = mExtent.width() / sz.width();
468 const double buffer = mExtentBuffer / mapUnitsPerPixel;
469
470 poly << m2p.toMapCoordinates( -buffer, -buffer ).toQPointF();
471 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), -buffer ).toQPointF();
472 poly << m2p.toMapCoordinates( static_cast<double>( sz.width() + buffer ), static_cast<double>( sz.height() + buffer ) ).toQPointF();
473 poly << m2p.toMapCoordinates( -buffer, static_cast<double>( sz.height() + buffer ) ).toQPointF();
474
475 return poly;
476}
477
479{
480 return mMapUnitsPerPixel;
481}
482
484{
485 return mScale;
486}
487
489{
490#ifdef QGISDEBUG
491 if ( !mHasTransformContext )
492 QgsDebugMsgLevel( u"No QgsCoordinateTransformContext context set for transform"_s, 4 );
493#endif
494
495 return mTransformContext;
496}
497
499{
500 mTransformContext = context;
501#ifdef QGISDEBUG
502 mHasTransformContext = true;
503#endif
504}
505
507{
508 if ( !layer )
509 return QgsCoordinateTransform();
510
512}
513
515{
516 // Output width in inches
517 const double outputWidthInInches = outputSize().width() / outputDpi();
518
519 // Desired visible width (honouring scale)
520 const double scaledWidthInInches = outputWidthInInches * scale;
521
523 {
524 // Start with some fraction of the current extent around the center
525 const double delta = mExtent.width() / 100.;
526 QgsRectangle ext( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
527 // Get scale at extent, and then scale extent to the desired scale
528 const double testScale = mScaleCalculator.calculate( ext, outputSize().width() );
529 ext.scale( scale / testScale );
530 return ext;
531 }
532 else
533 {
534 // Conversion from inches to mapUnits - this is safe to use, because we know here that the map units AREN'T in degrees
535 const double conversionFactor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Feet, mapUnits() ) / 12;
536
537 const double delta = 0.5 * scaledWidthInInches * conversionFactor;
538 return QgsRectangle( center.x() - delta, center.y() - delta, center.x() + delta, center.y() + delta );
539 }
540}
541
543{
544 return mScaleCalculator.calculate( extent, outputSize().width() );
545}
546
547double QgsMapSettings::layerToMapUnits( const QgsMapLayer *layer, const QgsRectangle &referenceExtent ) const
548{
549 return layerTransform( layer ).scaleFactor( referenceExtent );
550}
551
552
554{
555 try
556 {
558 if ( ct.isValid() )
559 {
560 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
561 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
562 QgsDebugMsgLevel( u"extent %1"_s.arg( extent.toString() ), 3 );
565 }
566 }
567 catch ( QgsCsException &cse )
568 {
569 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
570 }
571
572 QgsDebugMsgLevel( u"proj extent = %1 "_s.arg( extent.toString() ), 3 );
573
574 return extent;
575}
576
577
579{
580 try
581 {
584 if ( ct.isValid() )
585 {
586 QgsDebugMsgLevel( u"sourceCrs = %1"_s.arg( ct.sourceCrs().authid() ), 3 );
587 QgsDebugMsgLevel( u"destCRS = %1"_s.arg( ct.destinationCrs().authid() ), 3 );
588 QgsDebugMsgLevel( u"extent = %1"_s.arg( extent.toString() ), 3 );
590 }
591 }
592 catch ( QgsCsException &cse )
593 {
594 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
595 }
596
597 QgsDebugMsgLevel( u"proj extent = %1"_s.arg( extent.toString() ), 3 );
598
599 return extent;
600}
601
602
604{
605 try
606 {
607 const QgsCoordinateTransform ct = layerTransform( layer );
608 if ( ct.isValid() )
609 point = ct.transform( point, Qgis::TransformDirection::Forward );
610 }
611 catch ( QgsCsException &cse )
612 {
613 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
614 }
615
616 return point;
617}
618
620{
621 double x = point.x();
622 double y = point.y();
623 double z = point.z();
624 const double m = point.m();
625
626 try
627 {
628 const QgsCoordinateTransform ct = layerTransform( layer );
629 if ( ct.isValid() )
631 }
632 catch ( QgsCsException &cse )
633 {
634 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
635 }
636
637 return QgsPoint( x, y, z, m );
638}
639
640
642{
643 try
644 {
645 const QgsCoordinateTransform ct = layerTransform( layer );
646 if ( ct.isValid() )
648 }
649 catch ( QgsCsException &cse )
650 {
651 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
652 }
653
654 return rect;
655}
656
657
659{
660 try
661 {
662 const QgsCoordinateTransform ct = layerTransform( layer );
663 if ( ct.isValid() )
664 point = ct.transform( point, Qgis::TransformDirection::Reverse );
665 }
666 catch ( QgsCsException &cse )
667 {
668 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
669 }
670
671 return point;
672}
673
675{
676 double x = point.x();
677 double y = point.y();
678 double z = point.z();
679 const double m = point.m();
680
681 try
682 {
683 const QgsCoordinateTransform ct = layerTransform( layer );
684 if ( ct.isValid() )
686 }
687 catch ( QgsCsException &cse )
688 {
689 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
690 }
691
692 return QgsPoint( x, y, z, m );
693}
694
695
697{
698 try
699 {
700 const QgsCoordinateTransform ct = layerTransform( layer );
701 if ( ct.isValid() )
703 }
704 catch ( QgsCsException &cse )
705 {
706 QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
707 }
708
709 return rect;
710}
711
712
714{
715 // reset the map canvas extent since the extent may now be smaller
716 // We can't use a constructor since QgsRectangle normalizes the rectangle upon construction
718 fullExtent.setNull();
719
720 // iterate through the map layers and test each layers extent
721 // against the current min and max values
722 QgsDebugMsgLevel( u"Layer count: %1"_s.arg( mLayers.count() ), 5 );
723 const auto constMLayers = mLayers;
724 for ( const QgsWeakMapLayerPointer &layerPtr : constMLayers )
725 {
726 if ( QgsMapLayer *lyr = layerPtr.data() )
727 {
728 QgsDebugMsgLevel( "Updating extent using " + lyr->name(), 5 );
729 QgsDebugMsgLevel( "Input extent: " + lyr->extent().toString(), 5 );
730
731 if ( lyr->extent().isNull() )
732 continue;
733
734 // Layer extents are stored in the coordinate system (CS) of the
735 // layer. The extent must be projected to the canvas CS
736 const QgsRectangle extent = layerExtentToOutputExtent( lyr, lyr->extent() );
737
738 QgsDebugMsgLevel( "Output extent: " + extent.toString(), 5 );
739 fullExtent.combineExtentWith( extent );
740 }
741 }
742
743 if ( fullExtent.width() == 0.0 || fullExtent.height() == 0.0 )
744 {
745 // If all of the features are at the one point, buffer the
746 // rectangle a bit. If they are all at zero, do something a bit
747 // more crude.
748
749 if ( fullExtent.xMinimum() == 0.0 && fullExtent.xMaximum() == 0.0 && fullExtent.yMinimum() == 0.0 && fullExtent.yMaximum() == 0.0 )
750 {
751 fullExtent.set( -1.0, -1.0, 1.0, 1.0 );
752 }
753 else
754 {
755 const double padFactor = 1e-8;
756 const double widthPad = fullExtent.xMinimum() * padFactor;
757 const double heightPad = fullExtent.yMinimum() * padFactor;
758 const double xmin = fullExtent.xMinimum() - widthPad;
759 const double xmax = fullExtent.xMaximum() + widthPad;
760 const double ymin = fullExtent.yMinimum() - heightPad;
761 const double ymax = fullExtent.yMaximum() + heightPad;
762 fullExtent.set( xmin, ymin, xmax, ymax );
763 }
764 }
765
766 QgsDebugMsgLevel( "Full extent: " + fullExtent.toString(), 5 );
767 return fullExtent;
768}
769
770
771void QgsMapSettings::readXml( QDomNode &node )
772{
773 // set destination CRS
775 const QDomNode srsNode = node.namedItem( u"destinationsrs"_s );
776 if ( !srsNode.isNull() )
777 {
778 srs.readXml( srsNode );
779 }
780 setDestinationCrs( srs );
781
782 // set extent
783 const QDomNode extentNode = node.namedItem( u"extent"_s );
784 const QgsRectangle aoi = QgsXmlUtils::readRectangle( extentNode.toElement() );
785 setExtent( aoi );
786
787 // set rotation
788 const QDomNode rotationNode = node.namedItem( u"rotation"_s );
789 const QString rotationVal = rotationNode.toElement().text();
790 if ( !rotationVal.isEmpty() )
791 {
792 const double rot = rotationVal.toDouble();
793 setRotation( rot );
794 }
795
796 //render map tile
797 const QDomElement renderMapTileElem = node.firstChildElement( u"rendermaptile"_s );
798 if ( !renderMapTileElem.isNull() )
799 {
800 setFlag( Qgis::MapSettingsFlag::RenderMapTile, renderMapTileElem.text() == "1"_L1 );
801 }
802}
803
804
805void QgsMapSettings::writeXml( QDomNode &node, QDomDocument &doc )
806{
807 // units
808 node.appendChild( QgsXmlUtils::writeMapUnits( mapUnits(), doc ) );
809
810 // Write current view extents
811 node.appendChild( QgsXmlUtils::writeRectangle( extent(), doc ) );
812
813 // Write current view rotation
814 QDomElement rotNode = doc.createElement( u"rotation"_s );
815 rotNode.appendChild( doc.createTextNode( qgsDoubleToString( rotation() ) ) );
816 node.appendChild( rotNode );
817
818 // destination CRS
819 if ( mDestCRS.isValid() )
820 {
821 QDomElement srsNode = doc.createElement( u"destinationsrs"_s );
822 node.appendChild( srsNode );
823 mDestCRS.writeXml( srsNode, doc );
824 }
825
826 //render map tile
827 QDomElement renderMapTileElem = doc.createElement( u"rendermaptile"_s );
828 const QDomText renderMapTileText = doc.createTextNode( testFlag( Qgis::MapSettingsFlag::RenderMapTile ) ? "1" : "0" );
829 renderMapTileElem.appendChild( renderMapTileText );
830 node.appendChild( renderMapTileElem );
831}
832
837
839{
840 mLabelBoundaryGeometry = boundary;
841}
842
844{
845 mClippingRegions.append( region );
846}
847
848void QgsMapSettings::setClippingRegions( const QList<QgsMapClippingRegion> &regions )
849{
850 mClippingRegions = regions;
851}
852
853QList<QgsMapClippingRegion> QgsMapSettings::clippingRegions() const
854{
855 return mClippingRegions;
856}
857
859{
860 mMaskRenderSettings = settings;
861}
862
864{
865 mRenderedFeatureHandlers.append( handler );
866}
867
868QList<QgsRenderedFeatureHandlerInterface *> QgsMapSettings::renderedFeatureHandlers() const
869{
870 return mRenderedFeatureHandlers;
871}
872
874{
875 return mZRange;
876}
877
879{
880 mZRange = zRange;
881}
882
887
892
894{
895 return mFrameRate;
896}
897
899{
900 mFrameRate = rate;
901}
902
904{
905 return mCurrentFrame;
906}
907
908void QgsMapSettings::setCurrentFrame( long long frame )
909{
910 mCurrentFrame = frame;
911}
912
917
922
927
947
948QHash<QString, QgsSelectiveMaskingSourceSet> QgsMapSettings::selectiveMaskingSourceSets() const
949{
951}
952
953void QgsMapSettings::setSelectiveMaskingSourceSets( const QVector<QgsSelectiveMaskingSourceSet> &sets )
954{
956 mSelectiveMaskingSourceSets.reserve( sets.size() );
957 for ( const QgsSelectiveMaskingSourceSet &set : sets )
958 {
959 mSelectiveMaskingSourceSets.insert( set.id(), set );
960 }
961}
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:2866
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2867
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2868
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2869
QFlags< MapSettingsFlag > MapSettingsFlags
Map settings flags.
Definition qgis.h:2903
@ NoSimplification
No simplification can be applied.
Definition qgis.h:3199
DistanceUnit
Units of distance.
Definition qgis.h:5437
@ Feet
Imperial feet.
Definition qgis.h:5440
@ Unknown
Unknown distance unit.
Definition qgis.h:5487
@ Degrees
Degrees, for planar geographic CRS distance measurements.
Definition qgis.h:5444
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:214
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5714
RendererUsage
Usage of the renderer.
Definition qgis.h:3627
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2833
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2834
MapSettingsFlag
Flags which adjust the way maps are rendered.
Definition qgis.h:2879
@ RenderMapTile
Draw map such that there are no problems between adjacent tiles.
Definition qgis.h:2888
@ ForceVectorOutput
Vector graphics should not be cached and drawn as raster images.
Definition qgis.h:2882
@ UseAdvancedEffects
Enable layer opacity and blending effects.
Definition qgis.h:2883
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:7247
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7340
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:80
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Contains parameters for an ellipsoid.
bool valid
Whether ellipsoid parameters are valid.