Skip to content
Permalink
Browse files
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.
  • Loading branch information
nyalldawson committed May 7, 2019
1 parent f4e0fa8 commit d8ef8e2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
@@ -184,6 +184,22 @@ than the actual visible portion of that layer.
should never be used to determine the actual visible extent of a map render.

.. seealso:: :py:func:`setExtent`

.. seealso:: :py:func:`mapExtent`
%End

QgsRectangle mapExtent() const;
%Docstring
Returns the original extent of the map being rendered.

Unlike extent(), this extent is always in the final destination CRS for the map
render and represents the exact bounds of the map being rendered.

.. seealso:: :py:func:`extent`

.. seealso:: :py:func:`setMapExtent`

.. versionadded:: 3.4.8
%End

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
entire visible area.

.. seealso:: :py:func:`setExtent`

.. seealso:: :py:func:`setMapExtent`
%End

void setMapExtent( const QgsRectangle &extent );
%Docstring
Sets the original ``extent`` of the map being rendered.

Unlike setExtent(), this extent is always in the final destination CRS for the map
render and represents the exact bounds of the map being rendered.

.. seealso:: :py:func:`mapExtent`

.. seealso:: :py:func:`setExtent`

.. versionadded:: 3.4.8
%End

void setDrawEditingInformation( bool b );
@@ -43,6 +43,7 @@ QgsRenderContext::QgsRenderContext( const QgsRenderContext &rh )
, mCoordTransform( rh.mCoordTransform )
, mDistanceArea( rh.mDistanceArea )
, mExtent( rh.mExtent )
, mOriginalMapExtent( rh.mOriginalMapExtent )
, mMapToPixel( rh.mMapToPixel )
, mRenderingStopped( rh.mRenderingStopped )
, mScaleFactor( rh.mScaleFactor )
@@ -70,6 +71,7 @@ QgsRenderContext &QgsRenderContext::operator=( const QgsRenderContext &rh )
mPainter = rh.mPainter;
mCoordTransform = rh.mCoordTransform;
mExtent = rh.mExtent;
mOriginalMapExtent = rh.mOriginalMapExtent;
mMapToPixel = rh.mMapToPixel;
mRenderingStopped = rh.mRenderingStopped;
mScaleFactor = rh.mScaleFactor;
@@ -157,6 +159,7 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSet
QgsRenderContext ctx;
ctx.setMapToPixel( mapSettings.mapToPixel() );
ctx.setExtent( mapSettings.visibleExtent() );
ctx.setMapExtent( mapSettings.visibleExtent() );
ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
ctx.setFlag( UseAdvancedEffects, mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) );
@@ -233,9 +233,22 @@ class CORE_EXPORT QgsRenderContext
* should never be used to determine the actual visible extent of a map render.
*
* \see setExtent()
* \see mapExtent()
*/
const QgsRectangle &extent() const { return mExtent; }

/**
* Returns the original extent of the map being rendered.
*
* Unlike extent(), this extent is always in the final destination CRS for the map
* render and represents the exact bounds of the map being rendered.
*
* \see extent()
* \see setMapExtent()
* \since QGIS 3.4.8
*/
QgsRectangle mapExtent() const { return mOriginalMapExtent; }

const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}

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

/**
* Sets the original \a extent of the map being rendered.
*
* Unlike setExtent(), this extent is always in the final destination CRS for the map
* render and represents the exact bounds of the map being rendered.
*
* \see mapExtent()
* \see setExtent()
* \since QGIS 3.4.8
*/
void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }

void setDrawEditingInformation( bool b );

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

QgsRectangle mExtent;
QgsRectangle mOriginalMapExtent;

QgsMapToPixel mMapToPixel;

@@ -21,7 +21,8 @@
QgsCoordinateReferenceSystem,
QgsMapUnitScale,
QgsUnitTypes,
QgsProject)
QgsProject,
QgsRectangle)
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QPainter, QImage
from qgis.testing import start_app, unittest
@@ -45,16 +46,21 @@ def testGettersSetters(self):
c.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysOutlines)
self.assertEqual(c.textRenderFormat(), QgsRenderContext.TextFormatAlwaysOutlines)

c.setMapExtent(QgsRectangle(1, 2, 3, 4))
self.assertEqual(c.mapExtent(), QgsRectangle(1, 2, 3, 4))

def testCopyConstructor(self):
"""
Test the copy constructor
"""
c1 = QgsRenderContext()

c1.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysText)
c1.setMapExtent(QgsRectangle(1, 2, 3, 4))

c2 = QgsRenderContext(c1)
self.assertEqual(c2.textRenderFormat(), QgsRenderContext.TextFormatAlwaysText)
self.assertEqual(c2.mapExtent(), QgsRectangle(1, 2, 3, 4))

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

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

self.assertEqual(rc.mapExtent(), QgsRectangle(10000, 20000, 30000, 40000))

def testRenderMetersInMapUnits(self):

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

0 comments on commit d8ef8e2

Please sign in to comment.