Skip to content

Commit

Permalink
[GRASS] vectorLayers exception, hopefully catches the crash G7/Win
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed May 28, 2015
1 parent 131ea80 commit c84e823
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 76 deletions.
17 changes: 13 additions & 4 deletions src/plugins/grass/qgsgrassmodule.cpp
Expand Up @@ -1906,10 +1906,19 @@ void QgsGrassModule::viewOutput()
}
else
{
QStringList layers = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
QgsGrass::getDefaultMapset(), map );
QStringList layers;
try
{
layers = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
QgsGrass::getDefaultMapset(), map );
}
catch ( QgsGrass::Exception &e )
{
QgsDebugMsg( e.what() );
continue;
}

// check whether there are 1_* layers
// if so, 0_* layers won't be added
Expand Down
15 changes: 12 additions & 3 deletions src/plugins/grass/qgsgrassselect.cpp
Expand Up @@ -345,9 +345,18 @@ void QgsGrassSelect::setLayers()
if ( emap->count() < 1 )
return;

QStringList layers = QgsGrass::vectorLayers( egisdbase->text(),
elocation->currentText(), emapset->currentText(),
emap->currentText().toUtf8() );
QStringList layers;
try
{
layers = QgsGrass::vectorLayers( egisdbase->text(),
elocation->currentText(), emapset->currentText(),
emap->currentText().toUtf8() );
}
catch ( QgsGrass::Exception &e )
{
QgsDebugMsg( e.what() );
return;
}

int idx = 0;
int sel = -1;
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/grass/qgsgrassutils.cpp
Expand Up @@ -38,7 +38,16 @@ QString QgsGrassUtils::vectorLayerName( QString map, QString layer,
void QgsGrassUtils::addVectorLayers( QgisInterface *iface,
QString gisbase, QString location, QString mapset, QString map )
{
QStringList layers = QgsGrass::vectorLayers( gisbase, location, mapset, map );
QStringList layers;
try
{
layers = QgsGrass::vectorLayers( gisbase, location, mapset, map );
}
catch ( QgsGrass::Exception &e )
{
QgsDebugMsg( e.what() );
return;
}

for ( int i = 0; i < layers.count(); i++ )
{
Expand Down
137 changes: 72 additions & 65 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -1018,10 +1018,9 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( const QString& gisdbase, co
}
G_CATCH( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) );
GRASS_UNLOCK
return list;
throw e;
}

// TODO: Handle errors as exceptions. Do not open QMessageBox here! This method is also used in browser
Expand All @@ -1038,94 +1037,102 @@ QStringList GRASS_LIB_EXPORT QgsGrass::vectorLayers( const QString& gisdbase, co
Vect_close( &map );
#endif
GRASS_UNLOCK
return list;
throw QgsGrass::Exception( QObject::tr( "Cannot open vector on level 2" ) );
}
else if ( level < 1 )
{
QgsDebugMsg( "Cannot open vector" );
// Do not open QMessageBox here!
//QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) );
GRASS_UNLOCK
return list;
throw QgsGrass::Exception( QObject::tr( "Cannot open vector" ) );
}

QgsDebugMsg( "GRASS vector successfully opened" );


// Get layers
int ncidx = Vect_cidx_get_num_fields( &map );

for ( int i = 0; i < ncidx; i++ )
G_TRY
{
int field = Vect_cidx_get_field_number( &map, i );
QString fs;
fs.sprintf( "%d", field );

QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) );
// Get layers
int ncidx = Vect_cidx_get_num_fields( &map );

/* Points */
int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT );
if ( npoints > 0 )
for ( int i = 0; i < ncidx; i++ )
{
QString l = fs + "_point";
list.append( l );
}
int field = Vect_cidx_get_field_number( &map, i );
QString fs;
fs.sprintf( "%d", field );

/* Lines */
/* Lines without category appears in layer 0, but not boundaries */
int tp;
if ( field == 0 )
tp = GV_LINE;
else
tp = GV_LINE | GV_BOUNDARY;
QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) );

int nlines = Vect_cidx_get_type_count( &map, field, tp );
if ( nlines > 0 )
{
QString l = fs + "_line";
list.append( l );
}
/* Points */
int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT );
if ( npoints > 0 )
{
QString l = fs + "_point";
list.append( l );
}

/* Faces */
int nfaces = Vect_cidx_get_type_count( &map, field, GV_FACE );
if ( nfaces > 0 )
{
QString l = fs + "_face";
list.append( l );
}
/* Lines */
/* Lines without category appears in layer 0, but not boundaries */
int tp;
if ( field == 0 )
tp = GV_LINE;
else
tp = GV_LINE | GV_BOUNDARY;

/* Polygons */
int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA );
if ( nareas > 0 )
{
QString l = fs + "_polygon";
list.append( l );
int nlines = Vect_cidx_get_type_count( &map, field, tp );
if ( nlines > 0 )
{
QString l = fs + "_line";
list.append( l );
}

/* Faces */
int nfaces = Vect_cidx_get_type_count( &map, field, GV_FACE );
if ( nfaces > 0 )
{
QString l = fs + "_face";
list.append( l );
}

/* Polygons */
int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA );
if ( nareas > 0 )
{
QString l = fs + "_polygon";
list.append( l );
}
}
}

// TODO: add option in GUI to set listTopoLayers
QSettings settings;
bool listTopoLayers = settings.value( "/GRASS/listTopoLayers", false ).toBool();
if ( listTopoLayers )
{
// add topology layers
if ( Vect_get_num_primitives( &map, GV_POINTS ) > 0 )
// TODO: add option in GUI to set listTopoLayers
QSettings settings;
bool listTopoLayers = settings.value( "/GRASS/listTopoLayers", false ).toBool();
if ( listTopoLayers )
{
// add topology layers
if ( Vect_get_num_primitives( &map, GV_POINTS ) > 0 )
{
#if GRASS_VERSION_MAJOR < 7 /* no more point in GRASS 7 topo */
list.append( "topo_point" );
list.append( "topo_point" );
#endif
}
if ( Vect_get_num_primitives( &map, GV_LINES ) > 0 )
{
list.append( "topo_line" );
}
if ( Vect_get_num_nodes( &map ) > 0 )
{
list.append( "topo_node" );
}
}
if ( Vect_get_num_primitives( &map, GV_LINES ) > 0 )
{
list.append( "topo_line" );
}
if ( Vect_get_num_nodes( &map ) > 0 )
{
list.append( "topo_node" );
}
}

Vect_close( &map );
Vect_close( &map );
}
G_CATCH( QgsGrass::Exception &e )
{
QgsDebugMsg( QString( "Cannot get vector layers: %1" ).arg( e.what() ) );
GRASS_UNLOCK
throw e;
}

GRASS_UNLOCK
return list;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/grass/qgsgrass.h
Expand Up @@ -230,7 +230,7 @@ class QgsGrass
const QString& mapsetName );
static GRASS_LIB_EXPORT QStringList groups( const QString& mapsetPath );

//! Get list of vector layers
//! 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
13 changes: 11 additions & 2 deletions src/providers/grass/qgsgrassprovidermodule.cpp
Expand Up @@ -121,9 +121,18 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()

foreach ( QString name, vectorNames )
{
QStringList layerNames = QgsGrass::vectorLayers( mGisdbase, mLocation, mName, name );

QString mapPath = mPath + "/vector/" + name;
QStringList layerNames;
try
{
layerNames = QgsGrass::vectorLayers( mGisdbase, mLocation, mName, name );
}
catch ( QgsGrass::Exception &e )
{
QgsErrorItem * errorItem = new QgsErrorItem( this, name + " : " + e.what(), mapPath );
items.append( errorItem );
continue;
}

QgsGrassObject vectorObject( mGisdbase, mLocation, mName, name, QgsGrassObject::Vector );
QgsGrassVectorItem *map = 0;
Expand Down

0 comments on commit c84e823

Please sign in to comment.