Skip to content
Permalink
Browse files

Fix more incorrect 'invalid data source' errors when loading mesh layers

  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent a1d8d9a commit 73fa58a5af10fb269a5e75c6fdb9dfe3a20e5e2a
Showing with 60 additions and 40 deletions.
  1. +52 −37 src/app/qgisapp.cpp
  2. +8 −3 src/app/qgisapp.h
@@ -4623,14 +4623,20 @@ static void setupVectorLayer( const QString &vectorLayerPath,
}
}


bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
{
return addVectorLayersPrivate( layerQStringList, enc, dataSourceType );
}

bool QgisApp::addVectorLayersPrivate( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType, const bool guiWarning )
{
bool wasfrozen = mMapCanvas->isFrozen();
QList<QgsMapLayer *> layersToAdd;
QList<QgsMapLayer *> addedLayers;
QgsSettings settings;

Q_FOREACH ( QString src, layerQStringList )
for ( QString src : layerQStringList )
{
src = src.trimmed();
QString baseName;
@@ -4730,8 +4736,11 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
}
else
{
QString msg = tr( "%1 doesn't have any layers." ).arg( src );
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
if ( guiWarning )
{
QString msg = tr( "%1 doesn't have any layers." ).arg( src );
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
}
delete layer;
}
}
@@ -4745,9 +4754,9 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
QMessageBox::question( this, tr( "Invalid Data Source" ),
tr( "Download with \"Protocol\" source type has failed, do you want to try the \"File\" source type?" ) ) == QMessageBox::Yes )
{
return addVectorLayers( QStringList() << src.replace( QLatin1String( "/vsicurl/" ), " " ), enc, dataSourceType );
return addVectorLayersPrivate( QStringList() << src.replace( QLatin1String( "/vsicurl/" ), " " ), enc, dataSourceType, guiWarning );
}
else
else if ( guiWarning )
{
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
}
@@ -4777,10 +4786,6 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
freezeCanvases( false );
refreshMapCanvas();
}
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();

// statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );

return true;
}
@@ -4954,23 +4959,24 @@ bool QgisApp::askUserForZipItemLayers( const QString &path )
}

// present a dialog to choose GDAL raster sublayers
void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
QList< QgsMapLayer * > QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
{
QList< QgsMapLayer * > result;
if ( !layer )
return;
return result;

QStringList sublayers = layer->subLayers();
QgsDebugMsg( QStringLiteral( "raster has %1 sublayers" ).arg( layer->subLayers().size() ) );

if ( sublayers.empty() )
return;
return result;

// if promptLayers=Load all, load all sublayers without prompting
QgsSettings settings;
if ( settings.value( QStringLiteral( "qgis/promptForRasterSublayers" ), 1 ).toInt() == 3 )
{
loadGDALSublayers( layer->source(), sublayers );
return;
result.append( loadGDALSublayers( layer->source(), sublayers ) );
return result;
}

// We initialize a selection dialog and display it.
@@ -5061,13 +5067,15 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
{
addRasterLayer( rlayer );
}
result << rlayer;
}
}

// Respect if user don't want the new group of layers visible.
if ( addToGroup && ! newLayersVisible )
group->setItemVisibilityCheckedRecursive( newLayersVisible );
}
return result;
}

// should the GDAL sublayers dialog should be presented to the user?
@@ -5089,8 +5097,9 @@ bool QgisApp::shouldAskUserForGDALSublayers( QgsRasterLayer *layer )

// This method will load with GDAL the layers in parameter.
// It is normally triggered by the sublayer selection dialog.
void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
QList< QgsMapLayer * > QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
{
QList< QgsMapLayer * > result;
QString path, name;
QgsRasterLayer *subLayer = nullptr;
QgsSettings settings;
@@ -5110,6 +5119,7 @@ void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
if ( subLayer )
{
if ( subLayer->isValid() )
{
if ( addToGroup )
{
QgsProject::instance()->addMapLayer( subLayer, false );
@@ -5119,11 +5129,14 @@ void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
{
addRasterLayer( subLayer );
}
result << subLayer;
}
else
delete subLayer;
}

}
return result;
}

// This method is the method that does the real job. If the layer given in
@@ -5340,12 +5353,12 @@ void QgisApp::addVirtualLayer()
this, SLOT( replaceSelectedVectorLayer( QString, QString, QString, QString ) ) );
dts->exec();
delete dts;
} // QgisApp::addVirtualLayer()
}

void QgisApp::addSelectedVectorLayer( const QString &uri, const QString &layerName, const QString &provider )
{
addVectorLayer( uri, layerName, provider );
} // QgisApp:addSelectedVectorLayer
}

void QgisApp::replaceSelectedVectorLayer( const QString &oldId, const QString &uri, const QString &layerName, const QString &provider )
{
@@ -6462,24 +6475,17 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )
else
ok = addRasterLayer( fileName, fileInfo.completeBaseName() );
}
// TODO - should we really call isValidRasterFileName() before addRasterLayer()
// this results in 2 calls to GDALOpen()
// I think (Radim) that it is better to test only first if valid,
// addRasterLayer() is really trying to add layer and gives error if fails
//
// if ( addRasterLayer( fileName, fileInfo.completeBaseName() ) )
// {
// ok = true );
// }
else // nope - try to load it as a shape/ogr

// try as a vector
if ( !ok )
{
if ( allowInteractive )
{
ok = addVectorLayers( QStringList( fileName ), QStringLiteral( "System" ), QStringLiteral( "file" ) );
ok = addVectorLayersPrivate( QStringList( fileName ), QStringLiteral( "System" ), QStringLiteral( "file" ), false );
}
else
{
ok = addVectorLayer( fileName, fileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
ok = addVectorLayerPrivate( fileName, fileInfo.completeBaseName(), QStringLiteral( "ogr" ), false );
}
}

@@ -6495,6 +6501,9 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )
{
// we have no idea what this file is...
QgsMessageLog::logMessage( tr( "Unable to load %1" ).arg( fileName ) );

const QString msg = tr( "%1 is not a valid or recognized data source." ).arg( fileName );
visibleMessageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );
}

return ok;
@@ -11020,6 +11029,11 @@ void QgisApp::showLayoutManager()
}

QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const QString &name, const QString &providerKey )
{
return addVectorLayerPrivate( vectorLayerPath, name, providerKey );
}

QgsVectorLayer *QgisApp::addVectorLayerPrivate( const QString &vectorLayerPath, const QString &name, const QString &providerKey, const bool guiWarning )
{
bool wasfrozen = mMapCanvas->isFrozen();
QgsSettings settings;
@@ -11094,9 +11108,12 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
}
else
{
QString message = layer->dataProvider()->error().message( QgsErrorMessage::Text );
QString msg = tr( "The layer %1 is not a valid layer and can not be added to the map. Reason: %2" ).arg( vectorLayerPath, message );
visibleMessageBar()->pushMessage( tr( "Layer is not valid" ), msg, Qgis::Critical, messageTimeout() );
if ( guiWarning )
{
QString message = layer->dataProvider()->error().message( QgsErrorMessage::Text );
QString msg = tr( "The layer %1 is not a valid layer and can not be added to the map. Reason: %2" ).arg( vectorLayerPath, message );
visibleMessageBar()->pushMessage( tr( "Layer is not valid" ), msg, Qgis::Critical, messageTimeout() );
}

delete layer;
freezeCanvases( false );
@@ -11116,9 +11133,7 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q

return layer;

} // QgisApp::addVectorLayer


}

void QgisApp::addMapLayer( QgsMapLayer *mapLayer )
{
@@ -13203,13 +13218,13 @@ QgsRasterLayer *QgisApp::addRasterLayerPrivate(

if ( shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( layer );
QList< QgsMapLayer * > subLayers = askUserForGDALSublayers( layer );
ok = true;

// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
layer = nullptr;
layer = !subLayers.isEmpty() ? qobject_cast< QgsRasterLayer * >( subLayers.at( 0 ) ) : nullptr;
}
}
else
@@ -865,7 +865,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

//! copies features to internal clipboard
void copyFeatures( QgsFeatureStore &featureStore );
void loadGDALSublayers( const QString &uri, const QStringList &list );
QList<QgsMapLayer *> loadGDALSublayers( const QString &uri, const QStringList &list );

//! Deletes the selected attributes for the currently selected vector layer
void deleteSelected( QgsMapLayer *layer = nullptr, QWidget *parent = nullptr, bool checkFeaturesVisible = false );
@@ -1776,9 +1776,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
bool askUserForZipItemLayers( const QString &path );

/**
* This method will open a dialog so the user can select GDAL sublayers to load
* This method will open a dialog so the user can select GDAL sublayers to load,
* and then returns a list of these layers.
*/
void askUserForGDALSublayers( QgsRasterLayer *layer );
QList< QgsMapLayer * > askUserForGDALSublayers( QgsRasterLayer *layer );

/**
* This method will verify if a GDAL layer contains sublayers
@@ -1806,6 +1807,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsMeshLayer *addMeshLayerPrivate( const QString &uri, const QString &baseName,
const QString &providerKey, bool guiWarning = true );

bool addVectorLayersPrivate( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType, bool guiWarning = true );
QgsVectorLayer *addVectorLayerPrivate( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey, bool guiWarning = true );


/**
* Add the current project to the recently opened/saved projects list
* pass settings by reference since creating more than one

0 comments on commit 73fa58a

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