Skip to content

Commit

Permalink
[GRASS] catch fatal error when run with different GRASS version
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 12, 2015
1 parent 97675a9 commit 95e9ade
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/plugins/grass/qgsgrassplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ int QgsGrassPlugin::type()
*/
void QgsGrassPlugin::initGui()
{
if ( !QgsGrass::init() )
{
// TODO: add a widget with warning
return;
}

mToolBarPointer = 0;
mTools = 0;
mNewMapset = 0;

QSettings settings;
QgsGrass::init();
mCanvas = qGisInterface->mapCanvas();
QWidget* qgis = qGisInterface->mainWindow();

Expand Down
40 changes: 30 additions & 10 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,29 @@ QString QgsGrass::shortPath( const QString &path )
}
#endif

void QgsGrass::init( void )
bool QgsGrass::init( void )
{
// Warning!!!
// G_set_error_routine() once called from plugin
// is not valid in provider -> call it always

// nonInitializable is set to tru if G_no_gisinit() fails to avoid other attempts
static bool nonInitializable = false;

if ( nonInitializable )
{
return false;
}

if ( initialized )
{
return true;
}

// Set error function
G_set_error_routine( &error_routine );

if ( initialized )
return;
lock();

QgsDebugMsg( "do init" );
QSettings settings;
Expand All @@ -327,7 +339,18 @@ void QgsGrass::init( void )
G_set_gisrc_mode( G_GISRC_MODE_MEMORY );

// Init GRASS libraries (required)
G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit("libgrass++");
// G_no_gisinit() may end with fatal error if QGIS is run with a version of GRASS different from that used for compilation
G_TRY
{
G_no_gisinit(); // Doesn't check write permissions for mapset compare to G_gisinit("libgrass++");
}
G_CATCH( QgsGrass::Exception &e )
{
warning( tr( "Problem in GRASS initialization, GRASS provider and plugin will not work" ) + " : " + e.what() );
nonInitializable = true;
unlock();
return false;
}

// I think that mask should not be used in QGIS as it can only confuses people,
// anyway, I don't think anybody is using MASK
Expand Down Expand Up @@ -518,6 +541,8 @@ void QgsGrass::init( void )
}

initialized = 1;
unlock();
return true;
}

/*
Expand Down Expand Up @@ -560,25 +585,21 @@ QgsGrass *QgsGrass::instance()

bool QgsGrass::activeMode()
{
init();
return active;
}

QString QgsGrass::getDefaultGisdbase()
{
init();
return defaultGisdbase;
}

QString QgsGrass::getDefaultLocation()
{
init();
return defaultLocation;
}

QString QgsGrass::getDefaultLocationPath()
{
init();
if ( !active )
{
return QString();
Expand All @@ -588,7 +609,6 @@ QString QgsGrass::getDefaultLocationPath()

QString QgsGrass::getDefaultMapset()
{
init();
return defaultMapset;
}

Expand Down Expand Up @@ -2260,7 +2280,7 @@ bool QgsGrass::isExternal( const QgsGrassObject & object )
}
G_CATCH( QgsGrass::Exception &e )
{
QgsDebugMsg( "error getting external link: " + QString(e.what()) );
QgsDebugMsg( "error getting external link: " + QString( e.what() ) );
}
unlock();
return isExternal;
Expand Down
6 changes: 4 additions & 2 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,12 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
static void extendRegion( struct Cell_head *source,
struct Cell_head *target );

static void init( void );
/** Initialize GRASS library. This has to be called before any other function is used.
* @return true if successfully initialized */
static bool init( void );

//! test if the directory is location
static bool isLocation( const QString& path );;
static bool isLocation( const QString& path );

// ! test if the directory is mapset
static bool isMapset( const QString& path );
Expand Down
8 changes: 5 additions & 3 deletions src/providers/grass/qgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ QgsGrassProvider::QgsGrassProvider( QString uri )
{
QgsDebugMsg( QString( "QgsGrassProvider URI: %1" ).arg( uri ) );

QgsGrass::init();
mValid = false;
if ( !QgsGrass::init() )
{
return;
}

QTime time;
time.start();

mValid = false;

// Parse URI
QDir dir( uri ); // it is not a directory in fact
QString myURI = dir.path(); // no dupl '/'
Expand Down
9 changes: 8 additions & 1 deletion src/providers/grass/qgsgrassprovidermodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,10 @@ QGISEXTERN int dataCapabilities()

QGISEXTERN QgsDataItem * dataItem( QString theDirPath, QgsDataItem* parentItem )
{
if ( !QgsGrass::init() )
{
return 0;
}
if ( QgsGrass::isLocation( theDirPath ) )
{
QString path;
Expand Down Expand Up @@ -993,6 +997,9 @@ QGISEXTERN bool isProvider()
// Init GRASS in the first function called by provider registry so that it is called
// on main thread, not sure but suspicious that init in thread is causing problems,
// at least on Windows, not that dataItem() is called in thread
QgsGrass::init();
if ( !QgsGrass::init() )
{
QgsDebugMsg( "init failed" );
}
return true;
}
6 changes: 5 additions & 1 deletion src/providers/grass/qgsgrassrasterprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ QgsGrassRasterProvider::QgsGrassRasterProvider( QString const & uri )
{
QgsDebugMsg( "QgsGrassRasterProvider: constructing with uri '" + uri + "'." );

mValid = false;
if ( !QgsGrass::init() )
{
return;
}

// Parse URI, it is the same like using GDAL, i.e. path to raster cellhd, i.e.
// /path/to/gisdbase/location/mapset/cellhd/map
QFileInfo fileInfo( uri );
Expand Down

0 comments on commit 95e9ade

Please sign in to comment.