Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setting point cloud layer data source follow expected behavior #24

Merged
merged 4 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Constructor for LayerOptions with optional ``transformContext``.

QgsCoordinateTransformContext transformContext;

bool loadDefaultStyle;

bool skipCrsValidation;
};

Expand Down Expand Up @@ -83,6 +85,8 @@ QgsPointCloudLayer cannot be copied.

virtual QString loadDefaultStyle( bool &resultFlag /Out/ );

virtual void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false );


private:
QgsPointCloudLayer( const QgsPointCloudLayer &rhs );
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "qgssymbollayerutils.h"
#include "qgsxmlutils.h"
#include "qgsmessagebar.h"
#include "qgspointcloudlayer.h"


QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
Expand Down Expand Up @@ -145,6 +146,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QgsMapLayer *layer = QgsLayerTree::toLayer( node )->layer();
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
QgsPointCloudLayer *pcLayer = qobject_cast<QgsPointCloudLayer * >( layer );

if ( layer && layer->isSpatial() )
{
Expand Down Expand Up @@ -270,8 +272,8 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( tr( "&Filter…" ), QgisApp::instance(), qgis::overload<>::of( &QgisApp::layerSubsetString ) );
}

// change data source is only supported for vectors and rasters
if ( vlayer || rlayer )
// change data source is only supported for vectors and rasters, point clouds
if ( vlayer || rlayer || pcLayer )
{

QAction *a = new QAction( layer->isValid() ? tr( "Change Data Source…" ) : tr( "Repair Data Source…" ), menu );
Expand Down
68 changes: 39 additions & 29 deletions src/core/pointcloud/qgspointcloudlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,23 @@ QgsPointCloudLayer::QgsPointCloudLayer( const QString &path,
const QgsPointCloudLayer::LayerOptions &options )
: QgsMapLayer( QgsMapLayerType::PointCloudLayer, baseName, path )
{
Q_UNUSED( options )
bool ok = false;
if ( !path.isEmpty() && !providerLib.isEmpty() )
{
QgsDataProvider::ProviderOptions providerOptions { options.transformContext };
QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
if ( mReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
{
flags |= QgsDataProvider::FlagTrustDataSource;
}
ok = loadDataSource( providerLib, providerOptions, flags );
setDataSource( path, baseName, providerLib, providerOptions, options.loadDefaultStyle );
}
setValid( ok );
}

QgsPointCloudLayer::~QgsPointCloudLayer() = default;

QgsPointCloudLayer *QgsPointCloudLayer::clone() const
{
QgsPointCloudLayer *layer = new QgsPointCloudLayer( source(), name() );
LayerOptions options;
options.loadDefaultStyle = false;
options.transformContext = transformContext();
options.skipCrsValidation = true;

QgsPointCloudLayer *layer = new QgsPointCloudLayer( source(), name(), mProviderKey, options );
QgsMapLayer::clone( layer );
return layer;
}
Expand Down Expand Up @@ -80,17 +77,15 @@ bool QgsPointCloudLayer::readXml( const QDomNode &layerNode, QgsReadWriteContext
{
// create provider
QDomNode pkeyNode = layerNode.namedItem( QStringLiteral( "provider" ) );
QString providerKey = pkeyNode.toElement().text();
QgsDataProvider::ProviderOptions providerOptions { context.transformContext() };
QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
if ( mReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
mProviderKey = pkeyNode.toElement().text();

if ( !( mReadFlags & QgsMapLayer::FlagDontResolveLayers ) )
{
flags |= QgsDataProvider::FlagTrustDataSource;
QgsDataProvider::ProviderOptions providerOptions { context.transformContext() };
setDataSource( mDataSource, mLayerName, mProviderKey, providerOptions, false );
}
// TODO: support QgsMapLayer::FlagDontResolveLayers ?
bool ok = loadDataSource( providerKey, providerOptions, flags );
setValid( ok );
if ( !ok )

if ( !isValid() )
{
return false;
}
Expand Down Expand Up @@ -156,17 +151,30 @@ QString QgsPointCloudLayer::loadDefaultStyle( bool &resultFlag )
return QString();
}


bool QgsPointCloudLayer::loadDataSource( const QString &providerLib, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
void QgsPointCloudLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
{
QString dataSource = mDataSource;
mProviderKey = providerLib;
Q_UNUSED( loadDefaultStyleFlag )

if ( mDataProvider )
disconnect( mDataProvider.get(), &QgsPointCloudDataProvider::dataChanged, this, &QgsPointCloudLayer::dataChanged );

mDataProvider.reset( qobject_cast<QgsPointCloudDataProvider *>( QgsProviderRegistry::instance()->createProvider( providerLib, dataSource, options, flags ) ) );
setName( baseName );
mProviderKey = provider;
mDataSource = dataSource;

QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
if ( mReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
{
flags |= QgsDataProvider::FlagTrustDataSource;
}

mDataProvider.reset( qobject_cast<QgsPointCloudDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, dataSource, options, flags ) ) );
if ( !mDataProvider )
{
QgsDebugMsgLevel( QStringLiteral( "Unable to get point cloud data provider" ), 2 );
return false;
QgsDebugMsg( QStringLiteral( "Unable to get point cloud data provider" ) );
setValid( false );
emit dataSourceChanged();
return;
}

mDataProvider->setParent( this );
Expand All @@ -175,13 +183,15 @@ bool QgsPointCloudLayer::loadDataSource( const QString &providerLib, const QgsDa
setValid( mDataProvider->isValid() );
if ( !isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid point cloud provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
return false;
QgsDebugMsg( QStringLiteral( "Invalid point cloud provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ) );
emit dataSourceChanged();
return;
}

setCrs( mDataProvider->crs() );

connect( mDataProvider.get(), &QgsPointCloudDataProvider::dataChanged, this, &QgsPointCloudLayer::dataChanged );

return true;
emit dataSourceChanged();
triggerRepaint();
}
11 changes: 7 additions & 4 deletions src/core/pointcloud/qgspointcloudlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ class CORE_EXPORT QgsPointCloudLayer : public QgsMapLayer
: transformContext( transformContext )
{}

/**
* Coordinate transform context
*/
QgsCoordinateTransformContext transformContext;

//! Set to TRUE if the default layer style should be loaded
bool loadDefaultStyle = true;

/**
* Controls whether the layer is allowed to have an invalid/unknown CRS.
*
Expand Down Expand Up @@ -105,13 +111,10 @@ class CORE_EXPORT QgsPointCloudLayer : public QgsMapLayer

void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;
QString loadDefaultStyle( bool &resultFlag SIP_OUT ) override;
void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false ) override;

private:
bool loadDataSource( const QString &providerLib, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags );

/**
* Returns TRUE if the provider is in read-only mode
*/
bool isReadOnly() const override {return true;}

#ifdef SIP_RUN
Expand Down