Skip to content
Permalink
Browse files
QgsMapLayer::ReadFlag::FlagReadExtentFromXml to extend vector layer r…
…ead capabilities to other layer types

In QgsVectorLayer, it is possible through the flag trust layer metadata to use extent read in XML
instead of extent provided by the provider.

The QgsMapLayer::ReadFlag::FlagReadExtentFromXml can be used inlayer readXML to read Extent from XML for raster, point cloud and other layers.

Funded by Ifremer
  • Loading branch information
rldhont committed May 21, 2021
1 parent 59a6217 commit 65761a177cb698c3248c7412995493d850488cf5
@@ -75,6 +75,7 @@ Abstract base class for spatial data provider implementations.
FlagTrustDataSource,
SkipFeatureCount,
FlagLoadDefaultStyle,
SkipGetExtent,
};
typedef QFlags<QgsDataProvider::ReadFlag> ReadFlags;

@@ -579,6 +579,7 @@ or layer with temporary data (as temporary mesh layer dataset)
{
FlagDontResolveLayers,
FlagTrustLayerMetadata,
FlagReadExtentFromXml,
};
typedef QFlags<QgsMapLayer::ReadFlag> ReadFlags;

@@ -30,6 +30,7 @@
#include "qgspointcloudrendererregistry.h"
#include "qgspointcloudlayerelevationproperties.h"
#include "qgsmaplayerlegend.h"
#include "qgsxmlutils.h"
#include "qgsmaplayerfactory.h"
#include <QUrl>

@@ -108,6 +109,22 @@ bool QgsPointCloudLayer::readXml( const QDomNode &layerNode, QgsReadWriteContext
{
QgsDataProvider::ProviderOptions providerOptions { context.transformContext() };
QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
// read extent
if ( mReadFlags & QgsMapLayer::FlagReadExtentFromXml )
{
const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
// get the extent
const QgsRectangle mbr = QgsXmlUtils::readRectangle( extentNode.toElement() );

// store the extent
setExtent( mbr );

// skip get extent
flags |= QgsDataProvider::SkipGetExtent;
}
}
if ( mReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
{
flags |= QgsDataProvider::FlagTrustDataSource;
@@ -325,7 +342,10 @@ void QgsPointCloudLayer::setDataSourcePrivate( const QString &dataSource, const

// Load initial extent, crs and renderer
setCrs( mDataProvider->crs() );
setExtent( mDataProvider->extent() );
if ( !( flags & QgsDataProvider::SkipGetExtent ) )
{
setExtent( mDataProvider->extent() );
}

bool loadDefaultStyleFlag = false;
if ( flags & QgsDataProvider::FlagLoadDefaultStyle )
@@ -123,6 +123,7 @@ class CORE_EXPORT QgsDataProvider : public QObject
FlagTrustDataSource = 1 << 0, //!< Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load time by skipping expensive checks like primary key unicity, geometry type and srid and by using estimated metadata on data load. Since QGIS 3.16
SkipFeatureCount = 1 << 1, //!< Make featureCount() return -1 to indicate unknown, and subLayers() to return a unknown feature count as well. Since QGIS 3.18. Only implemented by OGR provider at time of writing.
FlagLoadDefaultStyle = 1 << 2, //!< Reset the layer's style to the default for the datasource
SkipGetExtent = 1 << 3, //!< Skip the extent from provider
};
Q_DECLARE_FLAGS( ReadFlags, ReadFlag )

@@ -416,6 +416,16 @@ bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &cont
Q_UNUSED( context )
// NOP by default; children will over-ride with behavior specific to them

// read Extent
if ( mReadFlags & QgsMapLayer::FlagReadExtentFromXml )
{
const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
mExtent = QgsXmlUtils::readRectangle( extentNode.toElement() );
}
}

return true;
} // void QgsMapLayer::readXml

@@ -579,6 +579,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
{
FlagDontResolveLayers = 1 << 0, //!< Don't resolve layer paths or create data providers for layers.
FlagTrustLayerMetadata = 1 << 1, //!< Trust layer metadata. Improves layer load time by skipping expensive checks like primary key unicity, geometry type and srid and by using estimated metadata on layer load. Since QGIS 3.16
FlagReadExtentFromXml = 1 << 2, //!< Read extent from xml and skip get extent from provider.
};
Q_DECLARE_FLAGS( ReadFlags, ReadFlag )

@@ -43,6 +43,7 @@ email : tim at linfiniti.com
#include "qgsrasterresamplefilter.h"
#include "qgsrastershader.h"
#include "qgsreadwritecontext.h"
#include "qgsxmlutils.h"
#include "qgsrectangle.h"
#include "qgsrendercontext.h"
#include "qgssinglebandcolordatarenderer.h"
@@ -673,13 +674,14 @@ void QgsRasterLayer::setDataProvider( QString const &provider, const QgsDataProv
mDataSource = mDataProvider->dataSourceUri();
}

// get the extent
QgsRectangle mbr = mDataProvider->extent();
if ( !( flags & QgsDataProvider::SkipGetExtent ) )
{
// get the extent
const QgsRectangle mbr = mDataProvider->extent();

// show the extent
QgsDebugMsgLevel( "Extent of layer: " + mbr.toString(), 4 );
// store the extent
setExtent( mbr );
// store the extent
setExtent( mbr );
}

// upper case the first letter of the layer name
QgsDebugMsgLevel( "mLayerName: " + name(), 4 );
@@ -2031,6 +2033,22 @@ bool QgsRasterLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c
{
flags |= QgsDataProvider::FlagTrustDataSource;
}
// read extent
if ( mReadFlags & QgsMapLayer::FlagReadExtentFromXml )
{
const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
// get the extent
const QgsRectangle mbr = QgsXmlUtils::readRectangle( extentNode.toElement() );

// store the extent
setExtent( mbr );

// skip get extent
flags |= QgsDataProvider::SkipGetExtent;
}
}
setDataProvider( mProviderKey, providerOptions, flags );
}

@@ -189,6 +189,7 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
providerFlags |= QgsDataProvider::FlagLoadDefaultStyle;
}
setDataSourcePrivate( vectorLayerPath, baseName, providerKey, providerOptions, providerFlags );
emit dataSourceChanged();
}

for ( const QgsField &field : std::as_const( mFields ) )
@@ -1611,9 +1612,13 @@ bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );

// read extent
if ( mReadFlags & QgsMapLayer::FlagReadExtentFromXml )
{
mReadExtentFromXml = true;
}
if ( mReadExtentFromXml )
{
QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
mXmlExtent = QgsXmlUtils::readRectangle( extentNode.toElement() );
@@ -1635,6 +1640,7 @@ bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c

} // void QgsVectorLayer::readXml


void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
{

0 comments on commit 65761a1

Please sign in to comment.