Skip to content
Permalink
Browse files

[GRASS] catch fatal error when run with different GRASS version

  • Loading branch information
blazek committed Sep 12, 2015
1 parent 97675a9 commit 95e9ade82ec8d885e56b3a0aef87c66c77987dff
@@ -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();

@@ -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;
@@ -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
@@ -518,6 +541,8 @@ void QgsGrass::init( void )
}

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

/*
@@ -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();
@@ -588,7 +609,6 @@ QString QgsGrass::getDefaultLocationPath()

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

@@ -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;
@@ -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 );
@@ -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 '/'
@@ -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;
@@ -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;
}
@@ -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 );

0 comments on commit 95e9ade

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