diff --git a/python/core/qgsfeaturesource.sip b/python/core/qgsfeaturesource.sip index f955af2dd70..fdda2b40787 100644 --- a/python/core/qgsfeaturesource.sip +++ b/python/core/qgsfeaturesource.sip @@ -78,6 +78,14 @@ class QgsFeatureSource :rtype: set of QVariant %End + virtual QgsRectangle sourceExtent() const; +%Docstring + Returns the extent of all geometries from the source. + The base class implementation uses a non-optimised approach of looping through + all features in the source. + :rtype: QgsRectangle +%End + }; diff --git a/python/core/qgsvectordataprovider.sip b/python/core/qgsvectordataprovider.sip index 5f5670f454f..f583efb5716 100644 --- a/python/core/qgsvectordataprovider.sip +++ b/python/core/qgsvectordataprovider.sip @@ -126,6 +126,8 @@ Bitmask of all provider's editing capabilities virtual QgsCoordinateReferenceSystem sourceCrs() const; + virtual QgsRectangle sourceExtent() const; + virtual QString dataComment() const; %Docstring diff --git a/python/core/qgsvectorlayer.sip b/python/core/qgsvectorlayer.sip index 181d12af273..2a29f382778 100644 --- a/python/core/qgsvectorlayer.sip +++ b/python/core/qgsvectorlayer.sip @@ -1103,10 +1103,8 @@ Synchronises with changes in the datasource virtual QgsRectangle extent() const; -%Docstring -Return the extent of the layer - :rtype: QgsRectangle -%End + virtual QgsRectangle sourceExtent() const; + virtual QgsFields fields() const; %Docstring diff --git a/src/core/qgsfeaturesource.cpp b/src/core/qgsfeaturesource.cpp index 60c20543013..49765d842d1 100644 --- a/src/core/qgsfeaturesource.cpp +++ b/src/core/qgsfeaturesource.cpp @@ -40,3 +40,20 @@ QSet QgsFeatureSource::uniqueValues( int fieldIndex, int limit ) const return values; } +QgsRectangle QgsFeatureSource::sourceExtent() const +{ + QgsRectangle r; + + QgsFeatureRequest req; + req.setSubsetOfAttributes( QgsAttributeList() ); + + QgsFeatureIterator it = getFeatures( req ); + QgsFeature f; + while ( it.nextFeature( f ) ) + { + if ( f.hasGeometry() ) + r.combineExtentWith( f.geometry().boundingBox() ); + } + return r; +} + diff --git a/src/core/qgsfeaturesource.h b/src/core/qgsfeaturesource.h index 4da9fbd4ebd..f3f8957a438 100644 --- a/src/core/qgsfeaturesource.h +++ b/src/core/qgsfeaturesource.h @@ -87,6 +87,13 @@ class CORE_EXPORT QgsFeatureSource */ virtual QSet uniqueValues( int fieldIndex, int limit = -1 ) const; + /** + * Returns the extent of all geometries from the source. + * The base class implementation uses a non-optimised approach of looping through + * all features in the source. + */ + virtual QgsRectangle sourceExtent() const; + }; Q_DECLARE_METATYPE( QgsFeatureSource * ) diff --git a/src/core/qgsvectordataprovider.cpp b/src/core/qgsvectordataprovider.cpp index b5402c147a8..ffecc54e929 100644 --- a/src/core/qgsvectordataprovider.cpp +++ b/src/core/qgsvectordataprovider.cpp @@ -52,6 +52,11 @@ QgsCoordinateReferenceSystem QgsVectorDataProvider::sourceCrs() const return crs(); } +QgsRectangle QgsVectorDataProvider::sourceExtent() const +{ + return extent(); +} + QString QgsVectorDataProvider::dataComment() const { return QString(); diff --git a/src/core/qgsvectordataprovider.h b/src/core/qgsvectordataprovider.h index eb40f0e813c..262c493bf83 100644 --- a/src/core/qgsvectordataprovider.h +++ b/src/core/qgsvectordataprovider.h @@ -159,6 +159,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat virtual QgsFields fields() const override = 0; QgsCoordinateReferenceSystem sourceCrs() const override; + QgsRectangle sourceExtent() const override; /** * Return a short comment for the data that this provider is diff --git a/src/core/qgsvectorlayer.cpp b/src/core/qgsvectorlayer.cpp index 5fa5dca0959..6c567efc53f 100644 --- a/src/core/qgsvectorlayer.cpp +++ b/src/core/qgsvectorlayer.cpp @@ -879,6 +879,11 @@ QgsRectangle QgsVectorLayer::extent() const return rect; } +QgsRectangle QgsVectorLayer::sourceExtent() const +{ + return extent(); +} + QString QgsVectorLayer::subsetString() const { if ( !mValid || !mDataProvider ) diff --git a/src/core/qgsvectorlayer.h b/src/core/qgsvectorlayer.h index 152572c2294..f3717abefd6 100644 --- a/src/core/qgsvectorlayer.h +++ b/src/core/qgsvectorlayer.h @@ -1077,8 +1077,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte */ virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY; - //! Return the extent of the layer QgsRectangle extent() const override; + QgsRectangle sourceExtent() const override; /** * Returns the list of fields of this layer.