[GRASS] more tests

This commit is contained in:
Radim Blazek 2015-04-23 19:04:23 +02:00
parent bcb95137f0
commit 1ac4b25289
5 changed files with 475 additions and 129 deletions

3
.gitignore vendored
View File

@ -61,3 +61,6 @@ tests/testdata/raster/band1_int16_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band3_float32_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band3_int16_noct_epsg4326.tif.aux.xml
tests/testdata/checker360by180.asc.aux.xml
tests/testdata/grass/wgs84/test/.gislock
tests/testdata/grass/wgs84/test6/.gislock
tests/testdata/grass/wgs84/test7/.gislock

View File

@ -311,7 +311,7 @@ void GRASS_LIB_EXPORT QgsGrass::init( void )
/*
* Check if given directory contains a GRASS installation
*/
bool QgsGrass::isValidGrassBaseDir( QString const gisBase )
bool QgsGrass::isValidGrassBaseDir( const QString& gisBase )
{
QgsDebugMsg( "isValidGrassBaseDir()" );
// GRASS currently doesn't handle paths with blanks
@ -340,25 +340,25 @@ bool QgsGrass::isValidGrassBaseDir( QString const gisBase )
return false;
}
bool QgsGrass::activeMode( void )
bool QgsGrass::activeMode()
{
init();
return active;
}
QString QgsGrass::getDefaultGisdbase( void )
QString QgsGrass::getDefaultGisdbase()
{
init();
return defaultGisdbase;
}
QString QgsGrass::getDefaultLocation( void )
QString QgsGrass::getDefaultLocation()
{
init();
return defaultLocation;
}
QString QgsGrass::getDefaultMapset( void )
QString QgsGrass::getDefaultMapset()
{
init();
return defaultMapset;
@ -474,12 +474,15 @@ QString GRASS_LIB_EXPORT QgsGrass::errorMessage( void )
return error_message;
}
QString GRASS_LIB_EXPORT QgsGrass::openMapset( QString gisdbase, QString location, QString mapset )
QString GRASS_LIB_EXPORT QgsGrass::openMapset( const QString& gisdbase,
const QString& location, const QString& mapset )
{
QgsDebugMsg( QString( "gisdbase = %1" ).arg( gisdbase.toUtf8().constData() ) );
QgsDebugMsg( QString( "location = %1" ).arg( location.toUtf8().constData() ) );
QgsDebugMsg( QString( "mapset = %1" ).arg( mapset.toUtf8().constData() ) );
closeMapset(); // close currently opened mapset (if any)
QString mapsetPath = gisdbase + "/" + location + "/" + mapset;
// Check if the mapset is in use
@ -505,24 +508,41 @@ QString GRASS_LIB_EXPORT QgsGrass::openMapset( QString gisdbase, QString locatio
#ifndef Q_OS_WIN
QFile lockFile( lock );
QProcess *process = new QProcess();
QProcess process;
QString lockProgram( gisBase + "/etc/lock" );
QStringList lockArguments;
lockArguments << lock << QString::number( pid );
QString lockCommand = lockProgram + " " + lockArguments.join( " " ); // for debug
QgsDebugMsg( "lock command: " + lockCommand );
process->start( lockProgram, QStringList() << lock << QString::number( pid ) );
if ( !process->waitForStarted() )
process.start( lockProgram, lockArguments );
if ( !process.waitForStarted( 5000 ) )
{
delete process;
return QObject::tr( "Cannot start %1/etc/lock" ).arg( gisBase );
return QObject::tr( "Cannot start %1" ).arg( lockCommand );
}
process.waitForFinished( 5000 );
QString processResult = QString( "exitStatus=%1, exitCode=%2, errorCode=%3, error=%4 stdout=%5, stderr=%6" )
.arg( process.exitStatus() ).arg( process.exitCode() )
.arg( process.error() ).arg( process.errorString() )
.arg( process.readAllStandardOutput().data() ).arg( process.readAllStandardError().data() );
QgsDebugMsg( "processResult: " + processResult );
// lock exit code:
// 0 - ok
// 1 - error
// 2 - mapset in use
if ( process.exitCode() == 2 )
{
return QObject::tr( "Mapset is already in use." );
}
process->waitForFinished( -1 );
if ( process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0 )
{
QString message = QObject::tr( "Mapset lock failed" ) + " (" + processResult + ")";
return message;
}
int status = process->exitStatus();
QgsDebugMsg( QString( "status = %1" ).arg( status ) );
delete process;
if ( status > 0 )
return QObject::tr( "Mapset is already in use." );
#endif // Q_OS_WIN
// Create temporary directory
@ -621,6 +641,8 @@ QString GRASS_LIB_EXPORT QgsGrass::openMapset( QString gisdbase, QString locatio
active = true;
// closeMapset() added at the beginning
#if 0
#ifndef Q_OS_WIN
// Close old mapset
if ( mMapsetLock.length() > 0 )
@ -628,6 +650,7 @@ QString GRASS_LIB_EXPORT QgsGrass::openMapset( QString gisdbase, QString locatio
QFile file( mMapsetLock );
file.remove();
}
#endif
#endif
mMapsetLock = lock;
@ -696,21 +719,21 @@ QString QgsGrass::closeMapset()
return NULL;
}
QStringList GRASS_LIB_EXPORT QgsGrass::locations( QString gisbase )
QStringList GRASS_LIB_EXPORT QgsGrass::locations( const QString& gisdbase )
{
QgsDebugMsg( QString( "gisbase = %1" ).arg( gisbase ) );
QgsDebugMsg( QString( "gisdbase = %1" ).arg( gisdbase ) );
QStringList list;
if ( gisbase.isEmpty() )
if ( gisdbase.isEmpty() )
return list;
QDir d = QDir( gisbase );
QDir d = QDir( gisdbase );
d.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
for ( unsigned int i = 0; i < d.count(); i++ )
{
if ( QFile::exists( gisbase + "/" + d[i]
if ( QFile::exists( gisdbase + "/" + d[i]
+ "/PERMANENT/DEFAULT_WIND" ) )
{
list.append( QString( d[i] ) );
@ -719,17 +742,17 @@ QStringList GRASS_LIB_EXPORT QgsGrass::locations( QString gisbase )
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( QString gisbase, QString locationName )
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( const QString& gisdbase, const QString& locationName )
{
QgsDebugMsg( QString( "gisbase = %1 locationName = %2" ).arg( gisbase ).arg( locationName ) );
QgsDebugMsg( QString( "gisbase = %1 locationName = %2" ).arg( gisdbase ).arg( locationName ) );
if ( gisbase.isEmpty() || locationName.isEmpty() )
if ( gisdbase.isEmpty() || locationName.isEmpty() )
return QStringList();
return QgsGrass::mapsets( gisbase + "/" + locationName );
return QgsGrass::mapsets( gisdbase + "/" + locationName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( QString locationPath )
QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( const QString& locationPath )
{
QgsDebugMsg( QString( "locationPath = %1" ).arg( locationPath ) );
@ -751,12 +774,12 @@ QStringList GRASS_LIB_EXPORT QgsGrass::mapsets( QString locationPath )
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString gisbase, QString locationName,
QString mapsetName )
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( const QString& gisdbase, const QString& locationName,
const QString& mapsetName )
{
QgsDebugMsg( "entered." );
if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
if ( gisdbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
return QStringList();
/* TODO: G_list() was added to GRASS 6.1 06-05-24,
@ -785,10 +808,10 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString gisbase, QString locatio
}
*/
return QgsGrass::vectors( gisbase + "/" + locationName + "/" + mapsetName );
return QgsGrass::vectors( gisdbase + "/" + locationName + "/" + mapsetName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString mapsetPath )
QStringList GRASS_LIB_EXPORT QgsGrass::vectors( const QString& mapsetPath )
{
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
@ -812,8 +835,8 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectors( QString mapsetPath )
}
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
QString location, QString mapset, QString mapName )
QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName )
{
GRASS_LOCK
QgsDebugMsg( QString( "gisdbase = %1 location = %2 mapset = %3 mapName = %4" ).arg( gisdbase ).arg( location ).arg( mapset ).arg( mapName ) );
@ -947,12 +970,12 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( QString gisdbase,
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString gisbase, QString locationName,
QString mapsetName )
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( const QString& gisdbase, const QString& locationName,
const QString& mapsetName )
{
QgsDebugMsg( "entered." );
if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
if ( gisdbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
return QStringList();
@ -982,10 +1005,10 @@ QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString gisbase, QString locatio
}
*/
return QgsGrass::rasters( gisbase + "/" + locationName + "/" + mapsetName );
return QgsGrass::rasters( gisdbase + "/" + locationName + "/" + mapsetName );
}
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString mapsetPath )
QStringList GRASS_LIB_EXPORT QgsGrass::rasters( const QString& mapsetPath )
{
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
@ -1004,17 +1027,18 @@ QStringList GRASS_LIB_EXPORT QgsGrass::rasters( QString mapsetPath )
return list;
}
QStringList GRASS_LIB_EXPORT QgsGrass::elements( QString gisbase, QString locationName,
QString mapsetName, QString element )
QStringList GRASS_LIB_EXPORT QgsGrass::elements( const QString& gisdbase, const QString& locationName,
const QString& mapsetName, const QString& element )
{
if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
if ( gisdbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() )
{
return QStringList();
}
return QgsGrass::elements( gisbase + "/" + locationName + "/" + mapsetName,
element );
return QgsGrass::elements( gisdbase + "/" + locationName + "/" + mapsetName, element );
}
QStringList GRASS_LIB_EXPORT QgsGrass::elements( QString mapsetPath, QString element )
QStringList GRASS_LIB_EXPORT QgsGrass::elements( const QString& mapsetPath, const QString& element )
{
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
@ -1033,7 +1057,7 @@ QStringList GRASS_LIB_EXPORT QgsGrass::elements( QString mapsetPath, QString ele
return list;
}
QString GRASS_LIB_EXPORT QgsGrass::regionString( struct Cell_head *window )
QString GRASS_LIB_EXPORT QgsGrass::regionString( const struct Cell_head *window )
{
QString reg;
int fmt;
@ -1070,11 +1094,11 @@ QString GRASS_LIB_EXPORT QgsGrass::regionString( struct Cell_head *window )
return reg;
}
bool GRASS_LIB_EXPORT QgsGrass::region( QString gisbase,
QString location, QString mapset,
bool GRASS_LIB_EXPORT QgsGrass::region( const QString& gisdbase,
const QString& location, const QString& mapset,
struct Cell_head *window )
{
QgsGrass::setLocation( gisbase, location );
QgsGrass::setLocation( gisdbase, location );
#if GRASS_VERSION_MAJOR < 7
if ( G__get_window( window, ( char * ) "", ( char * ) "WIND", mapset.toUtf8().data() ) )
@ -1088,9 +1112,9 @@ bool GRASS_LIB_EXPORT QgsGrass::region( QString gisbase,
return true;
}
bool GRASS_LIB_EXPORT QgsGrass::writeRegion( QString gisbase,
QString location, QString mapset,
struct Cell_head *window )
bool GRASS_LIB_EXPORT QgsGrass::writeRegion( const QString& gisbase,
const QString& location, const QString& mapset,
const struct Cell_head *window )
{
QgsDebugMsg( "entered." );
QgsDebugMsg( QString( "n = %1 s = %2" ).arg( window->north ).arg( window->south ) );
@ -1183,7 +1207,7 @@ void GRASS_LIB_EXPORT QgsGrass::setRegion( struct Cell_head *window, QgsRectangl
window->north = rect.yMaximum();
}
bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisbase,
bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisdbase,
QString location, QString mapset, QString map,
struct Cell_head *window )
{
@ -1191,7 +1215,7 @@ bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisbase,
QgsDebugMsg( QString( "map = %1" ).arg( map ) );
QgsDebugMsg( QString( "mapset = %1" ).arg( mapset ) );
QgsGrass::setLocation( gisbase, location );
QgsGrass::setLocation( gisdbase, location );
if ( type == Raster )
{
@ -1212,7 +1236,7 @@ bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisbase,
else if ( type == Vector )
{
// Get current projection
if ( !region( gisbase, location, mapset, window ) )
if ( !region( gisdbase, location, mapset, window ) )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot read vector map region" ) );
@ -1275,12 +1299,14 @@ bool GRASS_LIB_EXPORT QgsGrass::mapRegion( int type, QString gisbase,
return true;
}
QProcess GRASS_LIB_EXPORT *QgsGrass::startModule( QString gisdbase, QString location,
QString module, QStringList arguments, QTemporaryFile &gisrcFile )
QProcess GRASS_LIB_EXPORT *QgsGrass::startModule( const QString& gisdbase, const QString& location,
const QString& moduleName, const QStringList& arguments,
QTemporaryFile &gisrcFile )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
QProcess *process = new QProcess();
QString module = moduleName;
module += QString::number( QgsGrass::versionMajor() );
#ifdef Q_OS_WIN
module += ".exe";
@ -1316,13 +1342,14 @@ QProcess GRASS_LIB_EXPORT *QgsGrass::startModule( QString gisdbase, QString loca
return process;
}
QByteArray GRASS_LIB_EXPORT QgsGrass::runModule( QString gisdbase, QString location,
QString module, QStringList arguments, int timeOut )
QByteArray GRASS_LIB_EXPORT QgsGrass::runModule( const QString& gisdbase, const QString& location,
const QString& moduleName, const QStringList& arguments,
int timeOut )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2 timeOut = %3" ).arg( gisdbase ).arg( location ).arg( timeOut ) );
QTemporaryFile gisrcFile;
QProcess *process = QgsGrass::startModule( gisdbase, location, module, arguments, gisrcFile );
QProcess *process = QgsGrass::startModule( gisdbase, location, moduleName, arguments, gisrcFile );
if ( !process->waitForFinished( timeOut )
|| ( process->exitCode() != 0 && process->exitCode() != 255 ) )
@ -1330,8 +1357,8 @@ QByteArray GRASS_LIB_EXPORT QgsGrass::runModule( QString gisdbase, QString locat
QgsDebugMsg( "process->exitCode() = " + QString::number( process->exitCode() ) );
throw QgsGrass::Exception( QObject::tr( "Cannot run module" ) + "\n"
+ QObject::tr( "command: %1 %2<br>%3<br>%4" )
.arg( module ).arg( arguments.join( " " ) )
+ QObject::tr( "command: %1 %2\nstdout: %3\nstderr: %4" )
.arg( moduleName ).arg( arguments.join( " " ) )
.arg( process->readAllStandardOutput().constData() )
.arg( process->readAllStandardError().constData() ) );
}
@ -1340,8 +1367,12 @@ QByteArray GRASS_LIB_EXPORT QgsGrass::runModule( QString gisdbase, QString locat
return data;
}
QString GRASS_LIB_EXPORT QgsGrass::getInfo( QString info, QString gisdbase, QString location,
QString mapset, QString map, MapType type, double x, double y, QgsRectangle extent, int sampleRows, int sampleCols, int timeOut )
QString GRASS_LIB_EXPORT QgsGrass::getInfo( const QString& info, const QString& gisdbase,
const QString& location, const QString& mapset,
const QString& map, const MapType type,
double x, double y,
const QgsRectangle& extent, int sampleRows,
int sampleCols, int timeOut )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
@ -1386,7 +1417,8 @@ QString GRASS_LIB_EXPORT QgsGrass::getInfo( QString info, QString gisdbase, QStr
return QString( data );
}
QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crs( QString gisdbase, QString location )
QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crs( const QString& gisdbase, const QString& location,
bool interactive )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem();
@ -1399,14 +1431,17 @@ QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crs( QString gisdbase, Q
}
catch ( QgsGrass::Exception &e )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get projection " ) + "\n" + e.what() );
if ( interactive )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get projection " ) + "\n" + e.what() );
}
}
return crs;
}
QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crsDirect( QString gisdbase, QString location )
QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crsDirect( const QString& gisdbase, const QString& location )
{
QString Wkt;
@ -1447,7 +1482,9 @@ QgsCoordinateReferenceSystem GRASS_LIB_EXPORT QgsGrass::crsDirect( QString gisdb
return srs;
}
QgsRectangle GRASS_LIB_EXPORT QgsGrass::extent( QString gisdbase, QString location, QString mapset, QString map, MapType type )
QgsRectangle GRASS_LIB_EXPORT QgsGrass::extent( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map,
MapType type, bool interactive )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
@ -1463,13 +1500,17 @@ QgsRectangle GRASS_LIB_EXPORT QgsGrass::extent( QString gisdbase, QString locati
}
catch ( QgsGrass::Exception &e )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get raster extent" ) + "\n" + e.what() );
if ( interactive )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get raster extent" ) + "\n" + e.what() );
}
}
return QgsRectangle( 0, 0, 0, 0 );
}
void GRASS_LIB_EXPORT QgsGrass::size( QString gisdbase, QString location, QString mapset, QString map, int *cols, int *rows )
void GRASS_LIB_EXPORT QgsGrass::size( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map, int *cols, int *rows )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
@ -1495,14 +1536,19 @@ void GRASS_LIB_EXPORT QgsGrass::size( QString gisdbase, QString location, QStrin
QgsDebugMsg( QString( "raster size = %1 %2" ).arg( *cols ).arg( *rows ) );
}
QHash<QString, QString> GRASS_LIB_EXPORT QgsGrass::info( QString gisdbase, QString location, QString mapset, QString map, MapType type, QString info, QgsRectangle extent, int sampleRows, int sampleCols, int timeOut )
QHash<QString, QString> GRASS_LIB_EXPORT QgsGrass::info( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map,
MapType type,
const QString& info,
const QgsRectangle& extent,
int sampleRows, int sampleCols,
int timeOut, bool interactive )
{
QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) );
QHash<QString, QString> inf;
try
{
//QString str = QgsGrass::getInfo( "info", gisdbase, location, mapset, map, type );
QString str = QgsGrass::getInfo( info, gisdbase, location, mapset, map, type, 0, 0, extent, sampleRows, sampleCols, timeOut );
QgsDebugMsg( str );
QStringList list = str.split( "\n" );
@ -1520,8 +1566,11 @@ QHash<QString, QString> GRASS_LIB_EXPORT QgsGrass::info( QString gisdbase, QStri
}
catch ( QgsGrass::Exception &e )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get map info" ) + "\n" + e.what() );
if ( interactive )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get map info" ) + "\n" + e.what() );
}
}
return inf;
}

View File

@ -95,16 +95,16 @@ class QgsGrass
* Active mode means that GISRC is set up and GISRC file is available,
* in that case default GISDBASE, LOCATION and MAPSET may be read by GetDefaul*() functions.
* Passive mode means, that GISRC is not available. */
static GRASS_LIB_EXPORT bool activeMode( void );
static GRASS_LIB_EXPORT bool activeMode();
//! Get default GISDBASE, returns GISDBASE name or empty string if not in active mode
static GRASS_LIB_EXPORT QString getDefaultGisdbase( void );
static GRASS_LIB_EXPORT QString getDefaultGisdbase();
//! Get default LOCATION_NAME, returns LOCATION_NAME name or empty string if not in active mode
static GRASS_LIB_EXPORT QString getDefaultLocation( void );
static GRASS_LIB_EXPORT QString getDefaultLocation();
//! Get default MAPSET, returns MAPSET name or empty string if not in active mode
static GRASS_LIB_EXPORT QString getDefaultMapset( void );
static GRASS_LIB_EXPORT QString getDefaultMapset();
//! Init or reset GRASS library
/*!
@ -144,8 +144,8 @@ class QgsGrass
/** \brief Open existing GRASS mapset
* \return NULL string or error message
*/
static GRASS_LIB_EXPORT QString openMapset( QString gisdbase,
QString location, QString mapset );
static GRASS_LIB_EXPORT QString openMapset( const QString& gisdbase,
const QString& location, const QString& mapset );
/** \brief Close mapset if it was opened from QGIS.
* Delete GISRC, lock and temporary directory
@ -154,31 +154,32 @@ class QgsGrass
static GRASS_LIB_EXPORT QString closeMapset();
//! Check if given directory contains a GRASS installation
static GRASS_LIB_EXPORT bool isValidGrassBaseDir( QString const gisBase );
static GRASS_LIB_EXPORT bool isValidGrassBaseDir( const QString& gisBase );
//! Returns list of locations in given gisbase
static QStringList GRASS_LIB_EXPORT locations( QString gisbase );
static QStringList GRASS_LIB_EXPORT locations( const QString& gisdbase );
//! Returns list of mapsets in location
static GRASS_LIB_EXPORT QStringList mapsets( QString gisbase, QString locationName );
static GRASS_LIB_EXPORT QStringList mapsets( QString locationPath );
static GRASS_LIB_EXPORT QStringList mapsets( const QString& gisdbase, const QString& locationName );
static GRASS_LIB_EXPORT QStringList mapsets( const QString& locationPath );
//! List of vectors and rasters
static GRASS_LIB_EXPORT QStringList vectors( QString gisbase, QString locationName,
QString mapsetName );
static GRASS_LIB_EXPORT QStringList vectors( QString mapsetPath );
static GRASS_LIB_EXPORT QStringList vectors( const QString& gisdbase, const QString& locationName,
const QString& mapsetName );
static GRASS_LIB_EXPORT QStringList vectors( const QString& mapsetPath );
static GRASS_LIB_EXPORT QStringList rasters( QString gisbase, QString locationName,
QString mapsetName );
static GRASS_LIB_EXPORT QStringList rasters( QString mapsetPath );
static GRASS_LIB_EXPORT QStringList rasters( const QString& gisdbase, const QString& locationName,
const QString& mapsetNamee );
static GRASS_LIB_EXPORT QStringList rasters( const QString& mapsetPath );
//! Get list of vector layers
static GRASS_LIB_EXPORT QStringList vectorLayers( QString, QString, QString, QString );
static GRASS_LIB_EXPORT QStringList vectorLayers( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName );
//! List of elements
static GRASS_LIB_EXPORT QStringList elements( QString gisbase, QString locationName,
QString mapsetName, QString element );
static GRASS_LIB_EXPORT QStringList elements( QString mapsetPath, QString element );
static GRASS_LIB_EXPORT QStringList elements( const QString& gisdbase, const QString& locationName,
const QString& mapsetName, const QString& element );
static GRASS_LIB_EXPORT QStringList elements( const QString& mapsetPath, const QString& element );
//! Initialize GRASS region
static GRASS_LIB_EXPORT void initRegion( struct Cell_head *window );
@ -186,20 +187,20 @@ class QgsGrass
static GRASS_LIB_EXPORT void setRegion( struct Cell_head *window, QgsRectangle rect );
// ! Get map region
static GRASS_LIB_EXPORT bool mapRegion( int type, QString gisbase,
static GRASS_LIB_EXPORT bool mapRegion( int type, QString gisdbase,
QString location, QString mapset, QString map,
struct Cell_head *window );
// ! String representation of region
static GRASS_LIB_EXPORT QString regionString( struct Cell_head *window );
static GRASS_LIB_EXPORT QString regionString( const struct Cell_head *window );
// ! Read current mapset region
static GRASS_LIB_EXPORT bool region( QString gisbase, QString location, QString mapset,
static GRASS_LIB_EXPORT bool region( const QString& gisdbase, const QString& location, const QString& mapset,
struct Cell_head *window );
// ! Write current mapset region
static GRASS_LIB_EXPORT bool writeRegion( QString gisbase, QString location, QString mapset,
struct Cell_head *window );
static GRASS_LIB_EXPORT bool writeRegion( const QString& gisbase, const QString& location, const QString& mapset,
const struct Cell_head *window );
// ! Set (copy) region extent, resolution is not changed
static GRASS_LIB_EXPORT void copyRegionExtent( struct Cell_head *source,
@ -225,10 +226,14 @@ class QgsGrass
static GRASS_LIB_EXPORT QString gisrcFilePath();
// ! Start a GRASS module in any gisdbase/location
static GRASS_LIB_EXPORT QProcess *startModule( QString gisdbase, QString location, QString module, QStringList arguments, QTemporaryFile &gisrcFile );
static GRASS_LIB_EXPORT QProcess *startModule( const QString& gisdbase, const QString& location,
const QString& moduleName, const QStringList& arguments,
QTemporaryFile &gisrcFile );
// ! Run a GRASS module in any gisdbase/location
static GRASS_LIB_EXPORT QByteArray runModule( QString gisdbase, QString location, QString module, QStringList arguments, int timeOut = 30000 );
static GRASS_LIB_EXPORT QByteArray runModule( const QString& gisdbase, const QString& location,
const QString& moduleName, const QStringList& arguments,
int timeOut = 30000 );
/** \brief Get info string from qgis.g.info module
* @param info info type
@ -243,27 +248,39 @@ class QgsGrass
* @sampleSize sample size for statistics
* @timeOut timeout
*/
static GRASS_LIB_EXPORT QString getInfo( QString info, QString gisdbase,
QString location, QString mapset = "", QString map = "", MapType type = None, double x = 0.0, double y = 0.0, QgsRectangle extent = QgsRectangle(), int sampleRows = 0, int sampleCols = 0, int timeOut = 30000 );
static GRASS_LIB_EXPORT QString getInfo( const QString& info, const QString& gisdbase,
const QString& location, const QString& mapset = "",
const QString& map = "", const MapType type = None,
double x = 0.0, double y = 0.0,
const QgsRectangle& extent = QgsRectangle(), int sampleRows = 0,
int sampleCols = 0, int timeOut = 30000 );
// ! Get location projection
static GRASS_LIB_EXPORT QgsCoordinateReferenceSystem crs( QString gisdbase, QString location );
static GRASS_LIB_EXPORT QgsCoordinateReferenceSystem crs( const QString& gisdbase, const QString& location, bool interactive = true );
// ! Get location projection calling directly GRASS library
static GRASS_LIB_EXPORT QgsCoordinateReferenceSystem crsDirect( QString gisdbase, QString location );
static GRASS_LIB_EXPORT QgsCoordinateReferenceSystem crsDirect( const QString& gisdbase, const QString& location );
// ! Get map extent
static GRASS_LIB_EXPORT QgsRectangle extent( QString gisdbase, QString location,
QString mapset, QString map, MapType type = None );
// @param interactive - show warning dialog on error
static GRASS_LIB_EXPORT QgsRectangle extent( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map,
MapType type = None, bool interactive = true );
// ! Get raster map size
static GRASS_LIB_EXPORT void size( QString gisdbase, QString location,
QString mapset, QString map, int *cols, int *rows );
static GRASS_LIB_EXPORT void size( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map, int *cols, int *rows );
// ! Get raster info, info is either 'info' or 'stats'
// extent and sampleSize are stats options
static GRASS_LIB_EXPORT QHash<QString, QString> info( QString gisdbase, QString location,
QString mapset, QString map, MapType type, QString info = "info", QgsRectangle extent = QgsRectangle(), int sampleRows = 0, int sampleCols = 0, int timeOut = 30000 );
// @param interactive - show warning dialog on error
static GRASS_LIB_EXPORT QHash<QString, QString> info( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& map,
MapType type,
const QString& info = "info",
const QgsRectangle& extent = QgsRectangle(),
int sampleRows = 0, int sampleCols = 0,
int timeOut = 30000, bool interactive = true );
// ! List of Color
static GRASS_LIB_EXPORT QList<QgsGrass::Color> colors( QString gisdbase, QString location,

View File

@ -24,6 +24,23 @@ MACRO (ADD_QGIS_GRASS_TEST grass_build_version testname testsrc)
qgisgrass${grass_build_version}
)
ADD_TEST(qgis_${testname}${grass_build_version} ${CMAKE_BINARY_DIR}/output/bin/qgis_${testname}${grass_build_version})
IF (WIN32)
SET_PROPERTY(TEST qgis_${testname}${grass_build_version} PROPERTY
ENVIRONMENT "PATH=${GRASS_PREFIX${grass_build_version}}/lib;$ENV{PATH}"
)
ELSE (WIN32)
IF (APPLE)
SET_PROPERTY(TEST qgis_${testname}${grass_build_version} PROPERTY
ENVIRONMENT "DYLD_LIBRARY_PATH=${GRASS_PREFIX${grass_build_version}}/lib:$ENV{DYLD_LIBRARY_PATH}"
)
ELSE (APPLE)
# UNIX
SET_PROPERTY(TEST qgis_${testname}${grass_build_version} PROPERTY
ENVIRONMENT "LD_LIBRARY_PATH=${GRASS_PREFIX${grass_build_version}}/lib:$ENV{LD_LIBRARY_PATH}"
)
ENDIF (APPLE)
ENDIF (WIN32)
ENDMACRO (ADD_QGIS_GRASS_TEST)
MACRO (ADD_QGIS_GRASS_TESTS version)

View File

@ -22,8 +22,10 @@
#include <QtTest/QtTest>
#include <qgsapplication.h>
#include <qgscoordinatereferencesystem.h>
#include <qgsgrass.h>
#include <qgsproviderregistry.h>
#include <qgsrasterbandstats.h>
#include <qgsvectordataprovider.h>
extern "C"
@ -48,12 +50,29 @@ class TestQgsGrassProvider: public QObject
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.
void fatalError();
void locations();
void mapsets();
void maps();
void vectorLayers();
void region();
void info();
private:
void reportRow( QString message );
void reportHeader( QString message );
// verify result and report result
void verify( bool ok );
// compare expected and got string and set ok to false if not equal
bool compare( QString expected, QString got, bool& ok );
// lists are considered equal if contains the same values regardless order
// set ok to false if not equal
bool compare( QStringList expected, QStringList got, bool& ok );
// compare with tolerance
bool compare( double expected, double got, bool& ok );
QString mGisdbase;
QString mLocation;
QString mReport;
QString mBuildMapset;
};
@ -61,6 +80,10 @@ void TestQgsGrassProvider::reportRow( QString message )
{
mReport += message + "<br>";
}
void TestQgsGrassProvider::reportHeader( QString message )
{
mReport += "<h2>" + message + "</h2>";
}
//runs before all tests
void TestQgsGrassProvider::initTestCase()
@ -84,8 +107,10 @@ void TestQgsGrassProvider::initTestCase()
//create a raster layer that will be used in all tests...
mGisdbase = QString( TEST_DATA_DIR ) + "/grass";
mLocation = "wgs84";
mBuildMapset = QString( "test%1" ).arg( GRASS_BUILD_VERSION );
reportRow( "mGisdbase: " + mGisdbase );
reportRow( "mLocation: " + mLocation );
reportRow( "mBuildMapset: " + mBuildMapset );
qDebug() << "mGisdbase = " << mGisdbase << " mLocation = " << mLocation;
}
@ -104,15 +129,174 @@ void TestQgsGrassProvider::cleanupTestCase()
//QgsApplication::exitQgis();
}
void TestQgsGrassProvider::verify( bool ok )
{
reportRow( "" );
reportRow( QString( "Test result: " ) + ( ok ? "ok" : "error" ) );
QVERIFY( ok );
}
bool TestQgsGrassProvider::compare( QString expected, QString got, bool &ok )
{
if ( expected != got )
{
ok = false;
return false;
}
return true;
}
bool TestQgsGrassProvider::compare( QStringList expected, QStringList got, bool &ok )
{
QStringList e = expected;
QStringList g = got;
e.sort();
g.sort();
if ( e != g )
{
ok = false;
return false;
}
return true;
}
bool TestQgsGrassProvider::compare( double expected, double got, bool& ok )
{
if ( qAbs( got - expected ) > TINY_VALUE )
{
ok = false;
return false;
}
return true;
}
// G_fatal_error() handling
void TestQgsGrassProvider::fatalError()
{
reportHeader( "TestQgsGrassProvider::fatalError" );
bool ok = true;
QString errorMessage = "test fatal error";
G_TRY
{
G_fatal_error( "%s", errorMessage.toAscii().data() );
ok = false; // should not be reached
reportRow( "G_fatal_error() did not throw exception" );
}
G_CATCH( QgsGrass::Exception &e )
{
reportRow( QString( "Exception thrown and caught correctly" ) );
reportRow( "expected error message: " + errorMessage );
reportRow( "got error message: " + QString( e.what() ) );
compare( errorMessage, e.what(), ok );
}
compare( errorMessage, QgsGrass::errorMessage(), ok );
verify( ok );
}
void TestQgsGrassProvider::locations()
{
reportHeader( "TestQgsGrassProvider::locations" );
bool ok = true;
QStringList expectedLocations;
expectedLocations << "wgs84";
QStringList locations = QgsGrass::locations( mGisdbase );
reportRow( "expectedLocations: " + expectedLocations.join( ", " ) );
reportRow( "locations: " + locations.join( ", " ) );
compare( expectedLocations, locations, ok );
verify( ok );
}
void TestQgsGrassProvider::mapsets()
{
reportHeader( "TestQgsGrassProvider::mapsets" );
bool ok = true;
QStringList expectedMapsets;
expectedMapsets << "PERMANENT" << "test" << "test6" << "test7";
QStringList mapsets = QgsGrass::mapsets( mGisdbase, mLocation );
reportRow( "expectedMapsets: " + expectedMapsets.join( ", " ) );
reportRow( "mapsets: " + mapsets.join( ", " ) );
compare( expectedMapsets, mapsets, ok );
QgsGrass::setLocation( mGisdbase, mLocation ); // for G_is_mapset_in_search_path
foreach ( QString expectedMapset, expectedMapsets )
{
if ( G_is_mapset_in_search_path( expectedMapset.toAscii().data() ) != 1 )
{
reportRow( "mapset " + expectedMapset + " not in search path" );
ok = false;
}
}
// open/close mapset try twice to be sure that lock was not left etc.
for ( int i = 1; i < 3; i++ )
{
reportRow( "" );
reportRow( "Open/close mapset " + mBuildMapset + " for the " + QString::number( i ) + ". time" );
QString error = QgsGrass::openMapset( mGisdbase, mLocation, mBuildMapset );
if ( !error.isEmpty() )
{
reportRow( "QgsGrass::openMapset() failed: " + error );
ok = false;
}
else
{
reportRow( "mapset successfully opened" );
if ( !QgsGrass::activeMode() )
{
reportRow( "QgsGrass::activeMode() returns false after openMapset()" );
ok = false;
}
error = QgsGrass::closeMapset();
if ( !error.isEmpty() )
{
reportRow( "QgsGrass::close() failed: " + error );
ok = false;
}
else
{
reportRow( "mapset successfully closed" );
}
if ( QgsGrass::activeMode() )
{
reportRow( "QgsGrass::activeMode() returns true after closeMapset()" );
ok = false;
}
}
}
verify( ok );
}
void TestQgsGrassProvider::maps()
{
reportHeader( "TestQgsGrassProvider::maps" );
bool ok = true;
QStringList expectedVectors;
expectedVectors << "test";
QStringList vectors = QgsGrass::vectors( mGisdbase, mLocation, mBuildMapset );
reportRow( "expectedVectors: " + expectedVectors.join( ", " ) );
reportRow( "vectors: " + vectors.join( ", " ) );
compare( expectedVectors, vectors, ok );
reportRow( "" );
QStringList expectedRasters;
expectedRasters << "cell" << "dcell" << "fcell";
QStringList rasters = QgsGrass::rasters( mGisdbase, mLocation, "test" );
reportRow( "expectedRasters: " + expectedRasters.join( ", " ) );
reportRow( "rasters: " + rasters.join( ", " ) );
compare( expectedRasters, rasters, ok );
verify( ok );
}
void TestQgsGrassProvider::vectorLayers()
{
QString mapset = QString( "test%1" ).arg( GRASS_BUILD_VERSION );
reportHeader( "TestQgsGrassProvider::vectorLayers" );
QString mapset = mBuildMapset;
QString mapName = "test";
QStringList expectedLayers;
expectedLayers << "1_point" << "2_line" << "3_polygon";
reportRow( "" );
reportRow( "QgsGrass::vectorLayers test" );
reportRow( "mapset: " + mapset );
reportRow( "mapName: " + mapName );
reportRow( "expectedLayers: " + expectedLayers.join( ", " ) );
@ -121,25 +305,101 @@ void TestQgsGrassProvider::vectorLayers()
G_TRY
{
QStringList layers = QgsGrass::vectorLayers( mGisdbase, mLocation, mapset, mapName );
reportRow( "layers:" + layers.join( ", " ) );
foreach ( QString expectedLayer, expectedLayers )
{
if ( !layers.contains( expectedLayer ) )
{
ok = false;
reportRow( "ERROR: expected layer '" + expectedLayer + "' missing" );
}
}
reportRow( "layers: " + layers.join( ", " ) );
compare( expectedLayers, layers, ok );
}
G_CATCH( QgsGrass::Exception &e )
{
ok = false;
reportRow( QString( "ERROR: %1" ).arg( e.what() ) );
}
verify( ok );
}
QVERIFY( ok );
reportRow( "OK" );
void TestQgsGrassProvider::region()
{
reportHeader( "TestQgsGrassProvider::region" );
struct Cell_head window;
struct Cell_head windowCopy;
bool ok = QgsGrass::region( mGisdbase, mLocation, "PERMANENT", &window );
if ( !ok )
{
reportRow( "QgsGrass::region() failed" );
}
else
{
QString expectedRegion = "proj:3;zone:0;north:90N;south:90S;east:180E;west:180W;cols:1000;rows:500;e-w resol:0:21:36;n-s resol:0:21:36;";
QString region = QgsGrass::regionString( &window );
reportRow( "expectedRegion: " + expectedRegion );
reportRow( "region: " + region );
compare( expectedRegion, region, ok );
windowCopy.proj = window.proj;
windowCopy.zone = window.zone;
windowCopy.rows = window.rows;
windowCopy.cols = window.cols;
QgsGrass::copyRegionExtent( &window, &windowCopy );
QgsGrass::copyRegionResolution( &window, &windowCopy );
QString regionCopy = QgsGrass::regionString( &windowCopy );
reportRow( "regionCopy: " + regionCopy );
compare( expectedRegion, regionCopy, ok );
}
verify( ok );
}
void TestQgsGrassProvider::info()
{
// info() -> getInfo() -> runModule() -> startModule()
reportHeader( "TestQgsGrassProvider::info" );
bool ok = true;
QgsRectangle expectedExtent( -5, -5, 5, 5 );
QMap<QString, QgsRasterBandStats> expectedStats;
QgsRasterBandStats es;
es.minimumValue = -20;
es.maximumValue = 20;
expectedStats.insert( "cell", es );
es.minimumValue = -20.25;
es.maximumValue = 20.25;
expectedStats.insert( "dcell", es );
es.minimumValue = -20.25;
es.maximumValue = 20.25;
expectedStats.insert( "fcell", es );
foreach ( QString map, expectedStats.keys() )
{
es = expectedStats.value( map );
// TODO: QgsGrass::info() may open dialog window on error which blocks tests
QHash<QString, QString> info = QgsGrass::info( mGisdbase, mLocation, "test", map, QgsGrass::Raster, "stats",
expectedExtent, 10, 10, 5000, false );
reportRow( "map: " + map );
QgsRasterBandStats s;
s.minimumValue = info["MIN"].toDouble();
s.maximumValue = info["MAX"].toDouble();
reportRow( QString( "expectedStats: min = %1 max = %2" ).arg( es.minimumValue ).arg( es.maximumValue ) ) ;
reportRow( QString( "stats: min = %1 max = %2" ).arg( s.minimumValue ).arg( s.maximumValue ) ) ;
compare( es.minimumValue, s.minimumValue, ok );
compare( es.maximumValue, s.maximumValue, ok );
QgsRectangle extent = QgsGrass::extent( mGisdbase, mLocation, "test", map, QgsGrass::Raster, false );
reportRow( "expectedExtent: " + expectedExtent.toString() );
reportRow( "extent: " + extent.toString() );
if ( extent != expectedExtent )
{
ok = false;
}
}
reportRow( "" );
QgsCoordinateReferenceSystem expectedCrs;
expectedCrs.createFromOgcWmsCrs( "EPSG:4326" );
QgsCoordinateReferenceSystem crs = QgsGrass::crs( mGisdbase, mLocation );
reportRow( "expectedCrs: " + expectedCrs.toWkt() );
reportRow( "crs: " + crs.toWkt() );
if ( crs != expectedCrs )
{
ok = false;
}
verify( ok );
}
QTEST_MAIN( TestQgsGrassProvider )