Skip to content

Commit

Permalink
Merge branch 'crs_transform_cache'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Dec 18, 2012
2 parents bcca81b + e0d9796 commit ac52671
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 56 deletions.
6 changes: 0 additions & 6 deletions python/core/qgsmaprenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ class QgsMapRenderer : QObject
//! called by signal from layer current being drawn
void onDrawingProgress( int current, int total );

//! invalidate cached layer CRS
void invalidateCachedLayerCrs();

//! cached layer was destroyed
void cachedLayerDestroyed();

protected:

//! adjust extent to fit the pixmap size
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsrendercontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class QgsRenderContext

//setters

/**Sets coordinate transformation. QgsRenderContext takes ownership and deletes if necessary*/
void setCoordinateTransform( QgsCoordinateTransform* t /Transfer/ );
/**Sets coordinate transformation. QgsRenderContext does not take ownership*/
void setCoordinateTransform( const QgsCoordinateTransform* t );
void setMapToPixel( const QgsMapToPixel& mtp );
void setExtent( const QgsRectangle& extent );
void setDrawEditingInformation( bool b );
Expand Down
40 changes: 40 additions & 0 deletions src/core/qgscrscache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@
***************************************************************************/

#include "qgscrscache.h"
#include "qgscoordinatetransform.h"

QgsCoordinateTransformCache* QgsCoordinateTransformCache::mInstance = 0;

QgsCoordinateTransformCache* QgsCoordinateTransformCache::instance()
{
if ( !mInstance )
{
mInstance = new QgsCoordinateTransformCache();
}
return mInstance;
}

QgsCoordinateTransformCache::~QgsCoordinateTransformCache()
{
QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator tIt = mTransforms.constBegin();
for ( ; tIt != mTransforms.constEnd(); ++tIt )
{
delete tIt.value();
}
delete mInstance;
}

const QgsCoordinateTransform* QgsCoordinateTransformCache::transform( const QString& srcAuthId, const QString& destAuthId )
{
QHash< QPair< QString, QString >, QgsCoordinateTransform* >::const_iterator ctIt =
mTransforms.find( qMakePair( srcAuthId, destAuthId ) );
if ( ctIt == mTransforms.constEnd() )
{
const QgsCoordinateReferenceSystem& srcCrs = QgsCRSCache::instance()->crsByAuthId( srcAuthId );
const QgsCoordinateReferenceSystem& destCrs = QgsCRSCache::instance()->crsByAuthId( destAuthId );
QgsCoordinateTransform* ct = new QgsCoordinateTransform( srcCrs, destCrs );
mTransforms.insert( qMakePair( srcAuthId, destAuthId ), ct );
return ct;
}
else
{
return ctIt.value();
}
}

QgsCRSCache* QgsCRSCache::mInstance = 0;

Expand Down
19 changes: 19 additions & 0 deletions src/core/qgscrscache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
#include "qgscoordinatereferencesystem.h"
#include <QHash>

class QgsCoordinateTransform;

/**Cache coordinate transform by authid of source/dest transformation to avoid the
overhead of initialisation for each redraw*/
class CORE_EXPORT QgsCoordinateTransformCache
{
public:
static QgsCoordinateTransformCache* instance();
~QgsCoordinateTransformCache();
/**Returns coordinate transformation. Cache keeps ownership
@param srcAuthId auth id string of source crs
@param destAuthId auth id string of dest crs*/
const QgsCoordinateTransform* transform( const QString& srcAuthId, const QString& destAuthId );

private:
static QgsCoordinateTransformCache* mInstance;
QHash< QPair< QString, QString >, QgsCoordinateTransform* > mTransforms;
};

class CORE_EXPORT QgsCRSCache
{
public:
Expand Down
41 changes: 8 additions & 33 deletions src/core/qgsmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cfloat>

#include "qgscoordinatetransform.h"
#include "qgscrscache.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgsmaprenderer.h"
Expand Down Expand Up @@ -46,8 +47,6 @@ QgsMapRenderer::QgsMapRenderer()
mScale = 1.0;
mScaleCalculator = new QgsScaleCalculator;
mDistArea = new QgsDistanceArea;
mCachedTrForLayer = 0;
mCachedTr = 0;

mDrawing = false;
mOverview = false;
Expand All @@ -71,7 +70,6 @@ QgsMapRenderer::~QgsMapRenderer()
delete mDistArea;
delete mDestCRS;
delete mLabelingEngine;
delete mCachedTr;
}

QgsRectangle QgsMapRenderer::extent() const
Expand Down Expand Up @@ -266,7 +264,7 @@ void QgsMapRenderer::render( QPainter* painter, double* forceWidthScale )

mDrawing = true;

QgsCoordinateTransform* ct;
const QgsCoordinateTransform* ct;

#ifdef QGISDEBUG
QgsDebugMsg( "Starting to render layer stack." );
Expand Down Expand Up @@ -402,7 +400,7 @@ void QgsMapRenderer::render( QPainter* painter, double* forceWidthScale )
{
r1 = mExtent;
split = splitLayersExtent( ml, r1, r2 );
ct = new QgsCoordinateTransform( ml->crs(), *mDestCRS );
ct = QgsCoordinateTransformCache::instance()->transform( ml->crs().authid(), mDestCRS->authid() );
mRenderContext.setExtent( r1 );
QgsDebugMsg( " extent 1: " + r1.toString() );
QgsDebugMsg( " extent 2: " + r2.toString() );
Expand Down Expand Up @@ -692,7 +690,6 @@ void QgsMapRenderer::setDestinationCrs( const QgsCoordinateReferenceSystem& crs
rect = transform.transformBoundingBox( mExtent );
}

invalidateCachedLayerCrs();
QgsDebugMsg( "Setting DistArea CRS to " + QString::number( crs.srsid() ) );
mDistArea->setSourceCrs( crs.srsid() );
*mDestCRS = crs;
Expand Down Expand Up @@ -734,7 +731,7 @@ bool QgsMapRenderer::splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent
// extent separately.
static const double splitCoord = 180.0;

if ( mCachedTr->sourceCrs().geographicFlag() )
if ( layer->crs().geographicFlag() )
{
// Note: ll = lower left point
// and ur = upper right point
Expand Down Expand Up @@ -1138,35 +1135,13 @@ void QgsMapRenderer::setLabelingEngine( QgsLabelingEngineInterface* iface )
mLabelingEngine = iface;
}

QgsCoordinateTransform *QgsMapRenderer::tr( QgsMapLayer *layer )
const QgsCoordinateTransform* QgsMapRenderer::tr( QgsMapLayer *layer )
{
if ( mCachedTrForLayer != layer )
if ( !layer || !mDestCRS )
{
invalidateCachedLayerCrs();

delete mCachedTr;
mCachedTr = new QgsCoordinateTransform( layer->crs(), *mDestCRS );
mCachedTrForLayer = layer;

connect( layer, SIGNAL( layerCrsChanged() ), this, SLOT( invalidateCachedLayerCrs() ) );
connect( layer, SIGNAL( destroyed() ), this, SLOT( cachedLayerDestroyed() ) );
return 0;
}

return mCachedTr;
}

void QgsMapRenderer::cachedLayerDestroyed()
{
if ( mCachedTrForLayer == sender() )
mCachedTrForLayer = 0;
}

void QgsMapRenderer::invalidateCachedLayerCrs()
{
if ( mCachedTrForLayer )
disconnect( mCachedTrForLayer, SIGNAL( layerCrsChanged() ), this, SLOT( invalidateCachedLayerCrs() ) );

mCachedTrForLayer = 0;
return QgsCoordinateTransformCache::instance()->transform( layer->crs().authid(), mDestCRS->authid() );
}

bool QgsMapRenderer::mDrawing = false;
10 changes: 1 addition & 9 deletions src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,6 @@ class CORE_EXPORT QgsMapRenderer : public QObject
//! called by signal from layer current being drawn
void onDrawingProgress( int current, int total );

//! invalidate cached layer CRS
void invalidateCachedLayerCrs();

//! cached layer was destroyed
void cachedLayerDestroyed();

protected:

//! adjust extent to fit the pixmap size
Expand Down Expand Up @@ -343,9 +337,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
QMutex mRenderMutex;

private:
QgsCoordinateTransform *tr( QgsMapLayer *layer );
QgsCoordinateTransform *mCachedTr;
QgsMapLayer *mCachedTrForLayer;
const QgsCoordinateTransform* tr( QgsMapLayer *layer );
};

#endif
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsrendercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ QgsRenderContext::QgsRenderContext()

QgsRenderContext::~QgsRenderContext()
{
delete mCoordTransform;
}

void QgsRenderContext::setCoordinateTransform( QgsCoordinateTransform* t )
void QgsRenderContext::setCoordinateTransform( const QgsCoordinateTransform* t )
{
delete mCoordTransform;
mCoordTransform = t;
}

6 changes: 3 additions & 3 deletions src/core/qgsrendercontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class CORE_EXPORT QgsRenderContext

//setters

/**Sets coordinate transformation. QgsRenderContext takes ownership and deletes if necessary*/
void setCoordinateTransform( QgsCoordinateTransform* t );
/**Sets coordinate transformation. QgsRenderContext does not take ownership*/
void setCoordinateTransform( const QgsCoordinateTransform* t );
void setMapToPixel( const QgsMapToPixel& mtp ) {mMapToPixel = mtp;}
void setExtent( const QgsRectangle& extent ) {mExtent = extent;}
void setDrawEditingInformation( bool b ) {mDrawEditingInformation = b;}
Expand All @@ -87,7 +87,7 @@ class CORE_EXPORT QgsRenderContext
QPainter* mPainter;

/**For transformation between coordinate systems. Can be 0 if on-the-fly reprojection is not used*/
QgsCoordinateTransform* mCoordTransform;
const QgsCoordinateTransform* mCoordTransform;

/**True if vertex markers for editing should be drawn*/
bool mDrawEditingInformation;
Expand Down

0 comments on commit ac52671

Please sign in to comment.