Skip to content

Commit

Permalink
[GRASS] better handle corrupted/missing topo; allow to delete corrupt…
Browse files Browse the repository at this point in the history
…ed map
  • Loading branch information
blazek committed Jun 23, 2015
1 parent fd4b38c commit da39ab3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,30 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectors( const QString& mapsetPath )
}
return list;
}

bool GRASS_LIB_EXPORT QgsGrass::topoVersion( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName, int &major, int &minor )
{
QString path = gisdbase + "/" + location + "/" + mapset + "/vector/" + mapName + "/topo";
QFile file( path );
if ( !file.exists( path ) || file.size() < 5 )
{
return false;
}
if ( !file.open( QIODevice::ReadOnly ) )
{
return false;
}
QDataStream stream( &file );
quint8 maj, min;
stream >> maj;
stream >> min;
file.close();
major = maj;
minor = min;
return true;
}

QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName )
{
Expand Down
4 changes: 4 additions & 0 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ class QgsGrass
const QString& mapsetName );
static GRASS_LIB_EXPORT QStringList groups( const QString& mapsetPath );

//! Get topo file version 6, 7 or 0 if topo file does not exist
static GRASS_LIB_EXPORT bool topoVersion( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName, int &major, int &minor );

//! Get list of vector layers, throws QgsGrass::Exception
static GRASS_LIB_EXPORT QStringList vectorLayers( const QString& gisdbase, const QString& location,
const QString& mapset, const QString& mapName );
Expand Down
57 changes: 49 additions & 8 deletions src/providers/grass/qgsgrassprovidermodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,62 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
continue;
}

QgsGrassObject vectorObject( mGisdbase, mLocation, mName, name, QgsGrassObject::Vector );
QString mapPath = mPath + "/vector/" + name;
QStringList layerNames;
QgsGrassVectorItem *map = 0;

// test topo version before getting layers, because GRASS 7 Vect_open_old is calling G_fatal_error
// if topo version does not match GRASS lib version
int topoMajor = 0;
int topoMinor = 0;
bool gotTopoVersion = QgsGrass::topoVersion( mGisdbase, mLocation, mName, name, topoMajor, topoMinor );
QgsDebugMsg( QString( "name = %1 topoMajor = %2 topoMinor = %3" ).arg( name ).arg( topoMajor ).arg( topoMinor ) );
QString topoError;
if ( !gotTopoVersion )
{
topoError = tr( "topology missing" );
}
// GRASS 5-6: topoMajor = 5 topoMinor = 0
// GRASS 7: topoMajor = 5 topoMinor = 1
else if ( topoMajor != 5 )
{
topoError = tr( "topology version not supported" );
}
else if ( topoMinor == 0 && GRASS_VERSION_MAJOR == 7 )
{
topoError = tr( "topology version 6" );
}
else if ( topoMinor == 1 && GRASS_VERSION_MAJOR < 7 )
{
topoError = tr( "topology version 7" );
}

if ( !topoError.isEmpty() )
{
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + topoError, false );
items.append( map );
continue;
}

try
{
layerNames = QgsGrass::vectorLayers( mGisdbase, mLocation, mName, name );
}
catch ( QgsGrass::Exception &e )
{
QgsErrorItem * errorItem = new QgsErrorItem( this, name + " : " + e.what(), mapPath );
items.append( errorItem );
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + e.what(), false );
items.append( map );
continue;
}

QgsGrassObject vectorObject( mGisdbase, mLocation, mName, name, QgsGrassObject::Vector );
QgsGrassVectorItem *map = 0;
if ( layerNames.size() == 0 )
{
// TODO: differentiate if it is layer with no layers or without topo (throw exception from QgsGrass::vectorLayers)
// TODO: refresh (remove) error if topo was build
QgsErrorItem * errorItem = new QgsErrorItem( this, name, mapPath );
items.append( errorItem );
//QgsErrorItem * errorItem = new QgsErrorItem( this, name, mapPath );
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + tr( "empty" ), false );
items.append( map );
continue;
}
else if ( layerNames.size() > 1 )
Expand Down Expand Up @@ -637,12 +672,18 @@ void QgsGrassObjectItem::deleteGrassObject()

//----------------------- QgsGrassVectorItem ------------------------------

QgsGrassVectorItem::QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path ) :
QgsDataCollectionItem( parent, grassObject.name(), path )
QgsGrassVectorItem::QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path, QString labelName, bool valid ) :
QgsDataCollectionItem( parent, labelName.isEmpty() ? grassObject.name() : labelName, path )
, QgsGrassObjectItemBase( grassObject )
, mValid( valid )
{
QgsDebugMsg( "name = " + grassObject.name() + " path = " + path );
setCapabilities( QgsDataItem::NoCapabilities ); // disable fertility
if ( !mValid )
{
setState( Populated );
setIconName( "/mIconDelete.png" );
}
}

QList<QAction*> QgsGrassVectorItem::actions()
Expand Down
4 changes: 3 additions & 1 deletion src/providers/grass/qgsgrassprovidermodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class QgsGrassVectorItem : public QgsDataCollectionItem, public QgsGrassObjectIt
{
Q_OBJECT
public:
QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path );
// labelName - name to be displayed in tree if it should be different from grassObject.name() (e.g. invalid vector)
QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path, QString labelName = QString::null, bool valid = true );
~QgsGrassVectorItem() {}

virtual QList<QAction*> actions() override;
Expand All @@ -124,6 +125,7 @@ class QgsGrassVectorItem : public QgsDataCollectionItem, public QgsGrassObjectIt

private:
QgsGrassObject mVector;
bool mValid;
};

class QgsGrassVectorLayerItem : public QgsGrassObjectItem
Expand Down

0 comments on commit da39ab3

Please sign in to comment.