Skip to content

Commit

Permalink
Implement setDataSource for rasters
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 5, 2018
1 parent c32d542 commit 326640d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 64 deletions.
17 changes: 2 additions & 15 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion python/core/auto_generated/raster/qgsrasterlayer.sip.in
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 6 additions & 10 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -315,12 +317,6 @@ QString QgsVectorLayer::dataComment() const
return QString();
}


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

QgsCoordinateReferenceSystem QgsVectorLayer::sourceCrs() const
{
return crs();
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 )
Expand Down
17 changes: 1 addition & 16 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;

Expand Down
51 changes: 33 additions & 18 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 );
Expand Down
19 changes: 15 additions & 4 deletions src/core/raster/qgsrasterlayer.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
*/
Expand Down Expand Up @@ -453,9 +467,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer

QgsRasterViewPort mLastViewPort;

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

LayerType mRasterType;

QgsRasterPipe mPipe;
Expand Down

0 comments on commit 326640d

Please sign in to comment.