Add option on layer to read extent from xml in case of data source without metadata

This commit is contained in:
Blottiere Paul 2017-08-21 05:48:48 +01:00
parent 21c5f3d36c
commit 384e85ca77
8 changed files with 134 additions and 5 deletions

View File

@ -524,6 +524,15 @@ Returns a list of available encodings
:rtype: str
%End
virtual bool hasMetadata() const;
%Docstring
Returns true if the data source has metadata, false otherwise.
:return: true if data source has metadata, false otherwise.
.. versionadded:: 3.0
:rtype: bool
%End
signals:
void raiseError( const QString &msg ) const;

View File

@ -315,7 +315,8 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
};
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true );
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
bool readExtent = false );
%Docstring
Constructor - creates a vector layer
@ -328,6 +329,7 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
\param baseName The name used to represent the layer in the legend
\param providerLib The name of the data provider, e.g., "memory", "postgres"
\param loadDefaultStyleFlag whether to load the default style
\param readExtent Read extent from XML if true or let provider determine it if false
%End
@ -1727,6 +1729,25 @@ Returns the current blending mode for features
.. versionadded:: 3.0
%End
void setReadExtent( bool readExtent );
%Docstring
Flag allowing to indicate if the extent has be read from the XML
document when data source has no metadata or if the data provider has
to determine it.
.. versionadded:: 3.0
%End
bool readExtent() const;
%Docstring
Returns true if the extent is read from the XML document when data
source has no metadata, false if it's the data provider which determines
it.
.. versionadded:: 3.0
:rtype: bool
%End
public slots:
void select( QgsFeatureId featureId );

View File

@ -717,6 +717,16 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
if ( type == QLatin1String( "vector" ) )
{
mapLayer = new QgsVectorLayer;
// apply specific settings to vector layer
QgsSettings s;
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer ) )
{
if ( s.value( QStringLiteral( "/qgis/trustProject" ), false ).toBool() )
{
vl->setReadExtent( true );
}
}
}
else if ( type == QLatin1String( "raster" ) )
{

View File

@ -516,6 +516,14 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
*/
virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
/** Returns true if the data source has metadata, false otherwise.
*
* \returns true if data source has metadata, false otherwise.
*
* \since QGIS 3.0
*/
virtual bool hasMetadata() const { return true; };
signals:
/**

View File

@ -133,7 +133,8 @@ typedef bool deleteStyleById_t(
QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
const QString &baseName,
const QString &providerKey,
bool loadDefaultStyleFlag )
bool loadDefaultStyleFlag,
bool readExtent )
: QgsMapLayer( VectorLayer, baseName, vectorLayerPath )
, mDataProvider( nullptr )
, mProviderKey( providerKey )
@ -153,6 +154,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
, mLazyExtent( true )
, mSymbolFeatureCounted( false )
, mEditCommandActive( false )
, mReadExtent( readExtent )
{
mActions = new QgsActionManager( this );
@ -222,6 +224,7 @@ QgsVectorLayer *QgsVectorLayer::clone() const
layer->setAttributeTableConfig( attributeTableConfig() );
layer->setFeatureBlendMode( featureBlendMode() );
layer->setOpacity( opacity() );
layer->setReadExtent( readExtent() );
Q_FOREACH ( const QgsAction &action, actions()->actions() )
{
@ -800,8 +803,19 @@ QgsRectangle QgsVectorLayer::extent() const
if ( !mValidExtent && mLazyExtent && mDataProvider )
{
// get the extent
QgsRectangle mbr = mDataProvider->extent();
QgsRectangle mbr;
// get the extent from xml
if ( mReadExtent && !mXmlExtent.isNull() && !mDataProvider->hasMetadata() )
{
mbr = mXmlExtent;
}
// get the extent data provider if not yet defined
if ( mbr.isNull() )
{
mbr = mDataProvider->extent();
}
// show the extent
QgsDebugMsg( "Extent of layer: " + mbr.toString() );
@ -1428,6 +1442,16 @@ bool QgsVectorLayer::readXml( const QDomNode &layer_node, const QgsReadWriteCont
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
// read extent
if ( mReadExtent )
{
QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
mXmlExtent = QgsXmlUtils::readRectangle( extentNode.toElement() );
}
}
return mValid; // should be true if read successfully
} // void QgsVectorLayer::readXml
@ -4418,3 +4442,13 @@ QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties
return labeling;
}
void QgsVectorLayer::setReadExtent( bool readExtent )
{
mReadExtent = readExtent;
}
bool QgsVectorLayer::readExtent() const
{
return mReadExtent;
}

View File

@ -389,10 +389,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* \param baseName The name used to represent the layer in the legend
* \param providerLib The name of the data provider, e.g., "memory", "postgres"
* \param loadDefaultStyleFlag whether to load the default style
* \param readExtent Read extent from XML if true or let provider determine it if false
*
*/
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true );
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
bool readExtent = false );
virtual ~QgsVectorLayer();
@ -1604,6 +1606,24 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void setEditFormConfig( const QgsEditFormConfig &editFormConfig );
/**
* Flag allowing to indicate if the extent has be read from the XML
* document when data source has no metadata or if the data provider has
* to determine it.
*
* \since QGIS 3.0
*/
void setReadExtent( bool readExtent );
/**
* Returns true if the extent is read from the XML document when data
* source has no metadata, false if it's the data provider which determines
* it.
*
* \since QGIS 3.0
*/
bool readExtent() const;
public slots:
/**
@ -2041,6 +2061,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! True while an undo command is active
bool mEditCommandActive;
bool mReadExtent;
QgsRectangle mXmlExtent;
QgsFeatureIds mDeletedFids;
QgsAttributeTableConfig mAttributeTableConfig;

View File

@ -4291,6 +4291,19 @@ QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
return kind;
}
bool QgsPostgresProvider::hasMetadata() const
{
bool hasMetadata = true;
QgsPostgresProvider::Relkind kind = relkind();
if ( kind == Relkind::View || kind == Relkind::MaterializedView )
{
hasMetadata = false;
}
return hasMetadata;
}
/**
* Class factory to return a pointer to a newly created
* QgsPostgresProvider object

View File

@ -197,6 +197,17 @@ class QgsPostgresProvider : public QgsVectorDataProvider
virtual QList<QgsRelation> discoverRelations( const QgsVectorLayer *self, const QList<QgsVectorLayer *> &layers ) const override;
virtual QgsAttrPalIndexNameHash palAttributeIndexNames() const override;
/** Returns true if the data source has metadata, false otherwise. For
* example, if the kind of relation for the layer is a view or a
* materialized view, then no metadata are associated with the data
* source.
*
* \returns true if data source has metadata, false otherwise.
*
* \since QGIS 3.0
*/
virtual bool hasMetadata() const override;
signals:
/**