Skip to content
Permalink
Browse files

recalculate vector layer extent only when necessary (fixes #6172)

  • Loading branch information
jef-n committed Aug 8, 2012
1 parent d73368f commit 2ab022184d377d1f50614b2887e18cef05eb38e1
@@ -126,9 +126,9 @@ QString const & QgsMapLayer::source() const
return mDataSource;
}

QgsRectangle QgsMapLayer::extent() const
QgsRectangle QgsMapLayer::extent()
{
return mLayerExtent;
return mExtent;
}

bool QgsMapLayer::draw( QgsRenderContext& rendererContext )
@@ -1247,3 +1247,8 @@ QString QgsMapLayer::metadata()
{
return QString();
}

void QgsMapLayer::setExtent( const QgsRectangle &r )
{
mExtent = r;
}
@@ -110,8 +110,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
virtual void drawLabels( QgsRenderContext& rendererContext );

/** Return the extent of the layer as a QRect */
QgsRectangle extent() const;
/** Return the extent of the layer */
virtual QgsRectangle extent();

/*! Return the status of the layer. An invalid layer is one which has a bad datasource
* or other problem. Child classes set this flag when intialized
@@ -126,7 +126,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
QString publicSource() const;

/** Returns the source for the layer */
QString const & source() const;
QString const &source() const;

/**
* Returns the sublayers of this layer
@@ -390,6 +390,8 @@ class CORE_EXPORT QgsMapLayer : public QObject
void dataChanged();

protected:
/** Set the extent */
virtual void setExtent( const QgsRectangle &rect );

/** set whether layer is valid or not - should be used in constructor.
\note added in v1.5 */
@@ -421,7 +423,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
unsigned int mTransparencyLevel;

/** Extent of the layer */
QgsRectangle mLayerExtent;
QgsRectangle mExtent;

/** Indicates if the layer is valid and can be drawn */
bool mValid;
@@ -24,7 +24,7 @@ QString QgsPluginLayer::pluginLayerType()
return mPluginLayerType;
}

void QgsPluginLayer::setExtent( const QgsRectangle & extent )
void QgsPluginLayer::setExtent( const QgsRectangle &extent )
{
mLayerExtent = extent;
mExtent = extent;
}
@@ -105,6 +105,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
, mJoinBuffer( 0 )
, mDiagramRenderer( 0 )
, mDiagramLayerSettings( 0 )
, mValidExtent( false )
{
mActions = new QgsAttributeAction( this );

@@ -1461,10 +1462,24 @@ long QgsVectorLayer::updateFeatureCount() const

void QgsVectorLayer::updateExtents()
{
if ( !hasGeometryType() )
return;
mValidExtent = false;
}

void QgsVectorLayer::setExtent( const QgsRectangle &r )
{
QgsMapLayer::setExtent( r );
mValidExtent = true;
}

mLayerExtent.setMinimal();
QgsRectangle QgsVectorLayer::extent()
{
if ( mValidExtent )
return QgsMapLayer::extent();

QgsRectangle rect;

if ( !hasGeometryType() )
return rect;

if ( !mDataProvider )
{
@@ -1480,13 +1495,13 @@ void QgsVectorLayer::updateExtents()
if ( mDataProvider->featureCount() != 0 )
{
QgsRectangle r = mDataProvider->extent();
mLayerExtent.combineExtentWith( &r );
rect.combineExtentWith( &r );
}

for ( QgsFeatureList::iterator it = mAddedFeatures.begin(); it != mAddedFeatures.end(); it++ )
{
QgsRectangle r = it->geometry()->boundingBox();
mLayerExtent.combineExtentWith( &r );
rect.combineExtentWith( &r );
}
}
else
@@ -1499,19 +1514,23 @@ void QgsVectorLayer::updateExtents()
if ( fet.geometry() )
{
QgsRectangle bb = fet.geometry()->boundingBox();
mLayerExtent.combineExtentWith( &bb );
rect.combineExtentWith( &bb );
}
}
}

if ( mLayerExtent.xMinimum() > mLayerExtent.xMaximum() && mLayerExtent.yMinimum() > mLayerExtent.yMaximum() )
if ( rect.xMinimum() > rect.xMaximum() && rect.yMinimum() > rect.yMaximum() )
{
// special case when there are no features in provider nor any added
mLayerExtent = QgsRectangle(); // use rectangle with zero coordinates
rect = QgsRectangle(); // use rectangle with zero coordinates
}

setExtent( rect );

// Send this (hopefully) up the chain to the map canvas
emit recalculateExtents();

return rect;
}

QString QgsVectorLayer::subsetString()
@@ -2819,10 +2838,7 @@ bool QgsVectorLayer::setDataProvider( QString const & provider )
QString s = mbr.toString();
QgsDebugMsg( "Extent of layer: " + s );
// store the extent
mLayerExtent.setXMaximum( mbr.xMaximum() );
mLayerExtent.setXMinimum( mbr.xMinimum() );
mLayerExtent.setYMaximum( mbr.yMaximum() );
mLayerExtent.setYMinimum( mbr.yMinimum() );
setExtent( mbr );

// get and store the feature type
mWkbType = mDataProvider->geometryType();
@@ -493,6 +493,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Draws the layer labels using coordinate transformation */
void drawLabels( QgsRenderContext& rendererContext );

/** Return the extent of the layer as a QRect */
QgsRectangle extent();

/** returns field list in the to-be-committed state */
const QgsFieldMap &pendingFields() const;

@@ -745,6 +748,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
void committedAttributeValuesChanges( const QString& layerId, const QgsChangedAttributesMap& changedAttributesValues );
void committedGeometriesChanges( const QString& layerId, const QgsGeometryMap& changedGeometries );

protected:
/** Set the extent */
void setExtent( const QgsRectangle &rect );

private: // Private methods

/** vector layers are not copyable */
@@ -986,6 +993,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer

//stores infos about diagram placement (placement type, priority, position distance)
QgsDiagramLayerSettings *mDiagramLayerSettings;

bool mValidExtent;
};

#endif
@@ -693,7 +693,7 @@ bool QgsRasterLayer::draw( QgsRenderContext& rendererContext )

try
{
myProjectedLayerExtent = rendererContext.coordinateTransform()->transformBoundingBox( mLayerExtent );
myProjectedLayerExtent = rendererContext.coordinateTransform()->transformBoundingBox( extent() );
}
catch ( QgsCsException &cs )
{
@@ -705,7 +705,7 @@ bool QgsRasterLayer::draw( QgsRenderContext& rendererContext )
{
QgsDebugMsg( "coordinateTransform not set" );
myProjectedViewExtent = rendererContext.extent();
myProjectedLayerExtent = mLayerExtent;
myProjectedLayerExtent = extent();
}

QPainter* theQPainter = rendererContext.painter();
@@ -1691,10 +1691,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider )
QString s = mbr.toString();
QgsDebugMsg( "Extent of layer: " + s );
// store the extent
mLayerExtent.setXMaximum( mbr.xMaximum() );
mLayerExtent.setXMinimum( mbr.xMinimum() );
mLayerExtent.setYMaximum( mbr.yMaximum() );
mLayerExtent.setYMinimum( mbr.yMinimum() );
setExtent( mbr );

mWidth = mDataProvider->xSize();
mHeight = mDataProvider->ySize();
@@ -1704,9 +1701,6 @@ void QgsRasterLayer::setDataProvider( QString const & provider )
QgsDebugMsg( "mLayerName: " + name() );

mValidNoDataValue = mDataProvider->isNoDataValueValid();
if ( mValidNoDataValue )
{
}

// set up the raster drawing style
setDrawingStyle( MultiBandColor ); //sensible default

0 comments on commit 2ab0221

Please sign in to comment.
You can’t perform that action at this time.