Skip to content
Permalink
Browse files

Implement setDataSource for rasters

  • Loading branch information
elpaso committed Oct 30, 2018
1 parent c32d542 commit 326640d881a9aea57548a8f1911ea6fed88ebd3f
@@ -736,11 +736,6 @@ Returns point, line or polygon

%Docstring
Returns the WKBType or WKBUnknown in case of error
%End

QString providerType() const;
%Docstring
Returns the provider type for this layer
%End

virtual QgsCoordinateReferenceSystem sourceCrs() const ${SIP_FINAL};
@@ -988,7 +983,8 @@ if the geometry type of the new data source matches the current geometry type of
.. deprecated:: Use version with ProviderOptions argument instead
%End

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

%Docstring
Updates the data source of the layer. The layer's renderer and legend will be preserved only
if the geometry type of the new data source matches the current geometry type of the layer.
@@ -2326,15 +2322,6 @@ by the backend data provider).

signals:

void dataSourceChanged();
%Docstring
Emitted whenever the layer's data source has been changed.

.. seealso:: :py:func:`setDataSource`

.. versionadded:: 3.4
%End

void selectionChanged( const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect );
%Docstring
This signal is emitted when selection was changed
@@ -114,7 +114,7 @@ Constructor for LayerOptions.

explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
const QString &providerType = "gdal",
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );
%Docstring
This is the constructor for the RasterLayer class.
@@ -192,6 +192,24 @@ Set the data provider.
:param options: provider options

.. versionadded:: 3.2
%End

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

%Docstring
Updates the data source of the layer. The layer's renderer and legend will be preserved only
if the geometry type of the new data source matches the current geometry type of the layer.

:param dataSource: new layer data source
:param baseName: base name of the layer
:param provider: provider string
:param options: provider options
:param loadDefaultStyleFlag: set to true to reset the layer's style to the default for the
data source

.. seealso:: :py:func:`dataSourceChanged`

.. versionadded:: 3.6
%End

LayerType rasterType();
@@ -145,11 +145,13 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
const QString &providerKey,
const LayerOptions &options )
: QgsMapLayer( VectorLayer, baseName, vectorLayerPath )
, mProviderKey( providerKey )
, mAuxiliaryLayer( nullptr )
, mAuxiliaryLayerKey( QString() )
, mReadExtentFromXml( options.readExtentFromXml )
{

setProviderType( providerKey );

mGeometryOptions = qgis::make_unique<QgsGeometryOptions>();
mActions = new QgsActionManager( this );
mConditionalStyles = new QgsConditionalLayerStyles();
@@ -315,12 +317,6 @@ QString QgsVectorLayer::dataComment() const
return QString();
}


QString QgsVectorLayer::providerType() const
{
return mProviderKey;
}

QgsCoordinateReferenceSystem QgsVectorLayer::sourceCrs() const
{
return crs();
@@ -1418,14 +1414,14 @@ bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c
QgsDataProvider::ProviderOptions options;
if ( !setDataProvider( mProviderKey, options ) )
{
return false;
QgsDebugMsg( QStringLiteral( "Could not set data provider for layer %1" ).arg( publicSource() ) );
}

QDomElement pkeyElem = pkeyNode.toElement();
if ( !pkeyElem.isNull() )
{
QString encodingString = pkeyElem.attribute( QStringLiteral( "encoding" ) );
if ( !encodingString.isEmpty() )
if ( mDataProvider && !encodingString.isEmpty() )
{
mDataProvider->setEncoding( encodingString );
}
@@ -1584,6 +1580,7 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv
mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, dataSource, options ) );
if ( !mDataProvider )
{
mValid = false;
QgsDebugMsgLevel( QStringLiteral( "Unable to get data provider" ), 2 );
return false;
}
@@ -1597,7 +1594,6 @@ bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProv
if ( !mValid )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
return false;
}

if ( mDataProvider->capabilities() & QgsVectorDataProvider::ReadLayerMetadata )
@@ -763,9 +763,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Returns the WKBType or WKBUnknown in case of error
QgsWkbTypes::Type wkbType() const FINAL;

//! Returns the provider type for this layer
QString providerType() const;

QgsCoordinateReferenceSystem sourceCrs() const FINAL;
QString sourceName() const FINAL;

@@ -987,7 +984,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* \see dataSourceChanged()
* \since QGIS 3.2
*/
void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false );
void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false ) override;

QString loadDefaultStyle( bool &resultFlag SIP_OUT ) FINAL;

@@ -2135,15 +2132,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

signals:

/**
* Emitted whenever the layer's data source has been changed.
*
* \see setDataSource()
*
* \since QGIS 3.4
*/
void dataSourceChanged();

/**
* This signal is emitted when selection was changed
*
@@ -2444,9 +2432,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

QString mMapTipTemplate;

//! Data provider key
QString mProviderKey;

//! The user-defined actions that are accessed from the Identify Results dialog box
QgsActionManager *mActions = nullptr;

@@ -116,29 +116,15 @@ QgsRasterLayer::QgsRasterLayer( const QString &uri,
// Constant that signals property not used.
, QSTRING_NOT_SET( QStringLiteral( "Not Set" ) )
, TRSTRING_NOT_SET( tr( "Not Set" ) )
, mProviderKey( providerKey )
{
QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
setProviderType( providerKey );
init();

QgsDataProvider::ProviderOptions providerOptions;
setDataProvider( providerKey, providerOptions );
if ( !mValid ) return;

// load default style
bool defaultLoadedFlag = false;
if ( mValid && options.loadDefaultStyle )
{
loadDefaultStyle( defaultLoadedFlag );
}
if ( !defaultLoadedFlag )
{
setDefaultContrastEnhancement();
}

// TODO: Connect signals from the dataprovider to the qgisapp
setDataSource( uri, baseName, providerKey, providerOptions, options.loadDefaultStyle );

emit statusChanged( tr( "QgsRasterLayer created" ) );
} // QgsRasterLayer ctor

QgsRasterLayer::~QgsRasterLayer()
@@ -797,7 +783,37 @@ void QgsRasterLayer::setDataProvider( QString const &provider, const QgsDataProv
mValid = true;

QgsDebugMsgLevel( QStringLiteral( "exiting." ), 4 );
} // QgsRasterLayer::setDataProvider
}

void QgsRasterLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
{
if ( mDataProvider )
closeDataProvider();
mDataSource = dataSource;
mLayerName = baseName;
setDataProvider( provider, options );
if ( mValid )
{

// load default style
bool defaultLoadedFlag = false;
if ( loadDefaultStyleFlag )
{
loadDefaultStyle( defaultLoadedFlag );
}
if ( !defaultLoadedFlag )
{
setDefaultContrastEnhancement();
}

// TODO: Connect signals from the dataprovider to the qgisapp

emit statusChanged( tr( "QgsRasterLayer created" ) );

// Load default style
}
emit dataSourceChanged();
}

void QgsRasterLayer::closeDataProvider()
{
@@ -1474,7 +1490,6 @@ bool QgsRasterLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &c

QgsDataProvider::ProviderOptions providerOptions;
setDataProvider( mProviderKey, providerOptions );
if ( !mValid ) return false;

QString error;
bool res = readSymbology( layer_node, error, context );
@@ -199,7 +199,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
* */
explicit QgsRasterLayer( const QString &uri,
const QString &baseName = QString(),
const QString &providerKey = "gdal",
const QString &providerType = "gdal",
const QgsRasterLayer::LayerOptions &options = QgsRasterLayer::LayerOptions() );

~QgsRasterLayer() override;
@@ -259,6 +259,20 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
*/
void setDataProvider( const QString &provider, const QgsDataProvider::ProviderOptions &options );

/**
* Updates the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
* \param dataSource new layer data source
* \param baseName base name of the layer
* \param provider provider string
* \param options provider options
* \param loadDefaultStyleFlag set to true to reset the layer's style to the default for the
* data source
* \see dataSourceChanged()
* \since QGIS 3.6
*/
void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false ) override;

/**
* Returns the raster layer type (which is a read only property).
*/
@@ -453,9 +467,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer

QgsRasterViewPort mLastViewPort;

//! [ data provider interface ] Data provider key
QString mProviderKey;

LayerType mRasterType;

QgsRasterPipe mPipe;

0 comments on commit 326640d

Please sign in to comment.
You can’t perform that action at this time.