Skip to content

Commit d8ef8e2

Browse files
committed
Add method to retrieve map extent to QgsRenderContext
Previously only a "layer clipping extent" was available for retrieval from a QgsRenderContext instance, yet there's a need for rendering operations to have access to the original full extent of the map being rendered.
1 parent f4e0fa8 commit d8ef8e2

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

python/core/auto_generated/qgsrendercontext.sip.in

+32
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ than the actual visible portion of that layer.
184184
should never be used to determine the actual visible extent of a map render.
185185

186186
.. seealso:: :py:func:`setExtent`
187+
188+
.. seealso:: :py:func:`mapExtent`
189+
%End
190+
191+
QgsRectangle mapExtent() const;
192+
%Docstring
193+
Returns the original extent of the map being rendered.
194+
195+
Unlike extent(), this extent is always in the final destination CRS for the map
196+
render and represents the exact bounds of the map being rendered.
197+
198+
.. seealso:: :py:func:`extent`
199+
200+
.. seealso:: :py:func:`setMapExtent`
201+
202+
.. versionadded:: 3.4.8
187203
%End
188204

189205
const QgsMapToPixel &mapToPixel() const;
@@ -263,6 +279,22 @@ to the map. It may be larger than the actual visible area, but MUST contain at l
263279
entire visible area.
264280

265281
.. seealso:: :py:func:`setExtent`
282+
283+
.. seealso:: :py:func:`setMapExtent`
284+
%End
285+
286+
void setMapExtent( const QgsRectangle &extent );
287+
%Docstring
288+
Sets the original ``extent`` of the map being rendered.
289+
290+
Unlike setExtent(), this extent is always in the final destination CRS for the map
291+
render and represents the exact bounds of the map being rendered.
292+
293+
.. seealso:: :py:func:`mapExtent`
294+
295+
.. seealso:: :py:func:`setExtent`
296+
297+
.. versionadded:: 3.4.8
266298
%End
267299

268300
void setDrawEditingInformation( bool b );

src/core/qgsrendercontext.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ QgsRenderContext::QgsRenderContext( const QgsRenderContext &rh )
4343
, mCoordTransform( rh.mCoordTransform )
4444
, mDistanceArea( rh.mDistanceArea )
4545
, mExtent( rh.mExtent )
46+
, mOriginalMapExtent( rh.mOriginalMapExtent )
4647
, mMapToPixel( rh.mMapToPixel )
4748
, mRenderingStopped( rh.mRenderingStopped )
4849
, mScaleFactor( rh.mScaleFactor )
@@ -70,6 +71,7 @@ QgsRenderContext &QgsRenderContext::operator=( const QgsRenderContext &rh )
7071
mPainter = rh.mPainter;
7172
mCoordTransform = rh.mCoordTransform;
7273
mExtent = rh.mExtent;
74+
mOriginalMapExtent = rh.mOriginalMapExtent;
7375
mMapToPixel = rh.mMapToPixel;
7476
mRenderingStopped = rh.mRenderingStopped;
7577
mScaleFactor = rh.mScaleFactor;
@@ -157,6 +159,7 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSet
157159
QgsRenderContext ctx;
158160
ctx.setMapToPixel( mapSettings.mapToPixel() );
159161
ctx.setExtent( mapSettings.visibleExtent() );
162+
ctx.setMapExtent( mapSettings.visibleExtent() );
160163
ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
161164
ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
162165
ctx.setFlag( UseAdvancedEffects, mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) );

src/core/qgsrendercontext.h

+27
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,22 @@ class CORE_EXPORT QgsRenderContext
233233
* should never be used to determine the actual visible extent of a map render.
234234
*
235235
* \see setExtent()
236+
* \see mapExtent()
236237
*/
237238
const QgsRectangle &extent() const { return mExtent; }
238239

240+
/**
241+
* Returns the original extent of the map being rendered.
242+
*
243+
* Unlike extent(), this extent is always in the final destination CRS for the map
244+
* render and represents the exact bounds of the map being rendered.
245+
*
246+
* \see extent()
247+
* \see setMapExtent()
248+
* \since QGIS 3.4.8
249+
*/
250+
QgsRectangle mapExtent() const { return mOriginalMapExtent; }
251+
239252
const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}
240253

241254
/**
@@ -312,9 +325,22 @@ class CORE_EXPORT QgsRenderContext
312325
* entire visible area.
313326
*
314327
* \see setExtent()
328+
* \see setMapExtent()
315329
*/
316330
void setExtent( const QgsRectangle &extent ) {mExtent = extent;}
317331

332+
/**
333+
* Sets the original \a extent of the map being rendered.
334+
*
335+
* Unlike setExtent(), this extent is always in the final destination CRS for the map
336+
* render and represents the exact bounds of the map being rendered.
337+
*
338+
* \see mapExtent()
339+
* \see setExtent()
340+
* \since QGIS 3.4.8
341+
*/
342+
void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }
343+
318344
void setDrawEditingInformation( bool b );
319345

320346
void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
@@ -511,6 +537,7 @@ class CORE_EXPORT QgsRenderContext
511537
QgsDistanceArea mDistanceArea;
512538

513539
QgsRectangle mExtent;
540+
QgsRectangle mOriginalMapExtent;
514541

515542
QgsMapToPixel mMapToPixel;
516543

tests/src/python/test_qgsrendercontext.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
QgsCoordinateReferenceSystem,
2222
QgsMapUnitScale,
2323
QgsUnitTypes,
24-
QgsProject)
24+
QgsProject,
25+
QgsRectangle)
2526
from qgis.PyQt.QtCore import QSize
2627
from qgis.PyQt.QtGui import QPainter, QImage
2728
from qgis.testing import start_app, unittest
@@ -45,16 +46,21 @@ def testGettersSetters(self):
4546
c.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysOutlines)
4647
self.assertEqual(c.textRenderFormat(), QgsRenderContext.TextFormatAlwaysOutlines)
4748

49+
c.setMapExtent(QgsRectangle(1, 2, 3, 4))
50+
self.assertEqual(c.mapExtent(), QgsRectangle(1, 2, 3, 4))
51+
4852
def testCopyConstructor(self):
4953
"""
5054
Test the copy constructor
5155
"""
5256
c1 = QgsRenderContext()
5357

5458
c1.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysText)
59+
c1.setMapExtent(QgsRectangle(1, 2, 3, 4))
5560

5661
c2 = QgsRenderContext(c1)
5762
self.assertEqual(c2.textRenderFormat(), QgsRenderContext.TextFormatAlwaysText)
63+
self.assertEqual(c2.mapExtent(), QgsRectangle(1, 2, 3, 4))
5864

5965
c1.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysOutlines)
6066
c2 = QgsRenderContext(c1)
@@ -92,6 +98,9 @@ def testFromMapSettings(self):
9298
test QgsRenderContext.fromMapSettings()
9399
"""
94100
ms = QgsMapSettings()
101+
ms.setOutputSize(QSize(1000, 1000))
102+
ms.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
103+
ms.setExtent(QgsRectangle(10000, 20000, 30000, 40000))
95104

96105
ms.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysText)
97106
rc = QgsRenderContext.fromMapSettings(ms)
@@ -101,6 +110,8 @@ def testFromMapSettings(self):
101110
rc = QgsRenderContext.fromMapSettings(ms)
102111
self.assertEqual(rc.textRenderFormat(), QgsRenderContext.TextFormatAlwaysOutlines)
103112

113+
self.assertEqual(rc.mapExtent(), QgsRectangle(10000, 20000, 30000, 40000))
114+
104115
def testRenderMetersInMapUnits(self):
105116

106117
crs_wsg84 = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')

0 commit comments

Comments
 (0)