Skip to content

Commit

Permalink
Add option on layer to read extent from xml in case of data source wi…
Browse files Browse the repository at this point in the history
…thout metadata
  • Loading branch information
pblottiere committed Sep 6, 2017
1 parent 21c5f3d commit 384e85c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 5 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -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;
Expand Down
23 changes: 22 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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 );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -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" ) )
{
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -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:

/**
Expand Down
40 changes: 37 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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 )
Expand All @@ -153,6 +154,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
, mLazyExtent( true )
, mSymbolFeatureCounted( false )
, mEditCommandActive( false )
, mReadExtent( readExtent )

{
mActions = new QgsActionManager( this );
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4418,3 +4442,13 @@ QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties

return labeling;
}

void QgsVectorLayer::setReadExtent( bool readExtent )
{
mReadExtent = readExtent;
}

bool QgsVectorLayer::readExtent() const
{
return mReadExtent;
}
25 changes: 24 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -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:

/**
Expand Down

0 comments on commit 384e85c

Please sign in to comment.