Skip to content

Commit dcdfe8b

Browse files
committedOct 1, 2015
[GRASS] update all providers on map reload; dynamic cidx count in iterator
1 parent 2819668 commit dcdfe8b

8 files changed

+109
-50
lines changed
 

‎src/providers/grass/qgsgrassfeatureiterator.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ bool QgsGrassFeatureIterator::fetchFeature( QgsFeature& feature )
313313
cat = 0;
314314
type = 0;
315315
lid = 0;
316-
QgsDebugMsgLevel( QString( "mNextLid = %1 mNextCidx = %2 numLines() = %3 mCidxFieldNumCats = %4" )
316+
QgsDebugMsgLevel( QString( "mNextLid = %1 mNextCidx = %2 numLines() = %3 mCidxFieldIndex = %4 cidxFieldNumCats() = %5" )
317317
.arg( mNextLid ).arg( mNextCidx ).arg( mSource->mLayer->map()->numLines() )
318-
.arg( mSource->mCidxFieldNumCats ), 3 );
318+
.arg( mSource->mCidxFieldIndex ).arg( mSource->mLayer->cidxFieldNumCats() ), 3 );
319319
if ( mSource->mEditing )
320320
{
321321
// TODO should be numLines before editing started (?), but another layer
@@ -414,7 +414,7 @@ bool QgsGrassFeatureIterator::fetchFeature( QgsFeature& feature )
414414
}
415415
else // standard layer
416416
{
417-
if ( mNextCidx >= mSource->mCidxFieldNumCats )
417+
if ( mNextCidx >= mSource->mLayer->cidxFieldNumCats() )
418418
{
419419
break;
420420
}
@@ -692,7 +692,6 @@ QgsGrassFeatureSource::QgsGrassFeatureSource( const QgsGrassProvider* p )
692692
, mGrassType( p->mGrassType )
693693
, mQgisType( p->mQgisType )
694694
, mCidxFieldIndex( p->mCidxFieldIndex )
695-
, mCidxFieldNumCats( p->mCidxFieldNumCats )
696695
, mFields( p->fields() )
697696
, mEncoding( p->mEncoding )
698697
, mEditing( p->mEditBuffer )

‎src/providers/grass/qgsgrassfeatureiterator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class GRASS_LIB_EXPORT QgsGrassFeatureSource : public QgsAbstractFeatureSource
5757
QGis::WkbType mQgisType; // WKBPoint, WKBLineString, ...
5858

5959
int mCidxFieldIndex; // !UPDATE! Index for layerField in category index or -1 if no such field
60-
int mCidxFieldNumCats; // !UPDATE! Number of records in field index
6160

6261
QgsFields mFields;
6362
QTextCodec* mEncoding;

‎src/providers/grass/qgsgrassprovider.cpp

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ QgsGrassProvider::QgsGrassProvider( QString uri )
8282
, mLayer( 0 )
8383
, mMapVersion( 0 )
8484
, mCidxFieldIndex( -1 )
85-
, mCidxFieldNumCats( 0 )
8685
, mNumberFeatures( 0 )
8786
, mEditBuffer( 0 )
8887
, mEditLayer( 0 )
@@ -209,40 +208,17 @@ QgsGrassProvider::QgsGrassProvider( QString uri )
209208
break;
210209
}
211210

212-
// the map may be invalid (e.g. wrong uri or open failed)
213-
QgsGrassVectorMap *vectorMap = QgsGrassVectorMapStore::instance()->openMap( mGrassObject );
214-
if ( !vectorMap ) // should not happen
215-
{
216-
QgsDebugMsg( "Cannot open map" );
217-
return;
218-
}
219-
if ( !vectorMap->isValid() ) // may happen
220-
{
221-
QgsDebugMsg( "vectorMap is not valid" );
222-
return;
223-
}
224-
225-
mLayer = vectorMap->openLayer( mLayerField );
226-
227-
if ( !mLayer ) // should not happen
211+
if ( !openLayer() )
228212
{
229213
QgsDebugMsg( "Cannot open layer" );
230214
return;
231215
}
232-
if ( !mLayer->map() || !mLayer->map()->map() ) // should not happen
233-
{
234-
QgsDebugMsg( "map is null" );
235-
return;
236-
}
237216

238217
loadMapInfo();
239218
setTopoFields();
240219

241-
mLayer->map()->version();
242-
243220
connect( mLayer->map(), SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
244221

245-
246222
// TODO: types according to database
247223
mNativeTypes
248224
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), "integer", QVariant::Int, 1, 10 )
@@ -266,6 +242,38 @@ int QgsGrassProvider::capabilities() const
266242
return 0;
267243
}
268244

245+
bool QgsGrassProvider::openLayer()
246+
{
247+
QgsDebugMsg( "entered" );
248+
// the map may be invalid (e.g. wrong uri or open failed)
249+
QgsGrassVectorMap *vectorMap = QgsGrassVectorMapStore::instance()->openMap( mGrassObject );
250+
if ( !vectorMap ) // should not happen
251+
{
252+
QgsDebugMsg( "Cannot open map" );
253+
return false;
254+
}
255+
if ( !vectorMap->isValid() ) // may happen
256+
{
257+
QgsDebugMsg( "vectorMap is not valid" );
258+
return false;
259+
}
260+
261+
mLayer = vectorMap->openLayer( mLayerField );
262+
263+
if ( !mLayer ) // should not happen
264+
{
265+
QgsDebugMsg( "Cannot open layer" );
266+
return false;
267+
}
268+
if ( !mLayer->map() || !mLayer->map()->map() ) // should not happen
269+
{
270+
QgsDebugMsg( "map is null" );
271+
return false;
272+
}
273+
mMapVersion = mLayer->map()->version();
274+
return true;
275+
}
276+
269277
void QgsGrassProvider::loadMapInfo()
270278
{
271279
// Getting the total number of features in the layer
@@ -291,38 +299,38 @@ void QgsGrassProvider::loadMapInfo()
291299
if ( mCidxFieldIndex >= 0 )
292300
{
293301
mNumberFeatures = Vect_cidx_get_type_count( map(), mLayerField, mGrassType );
294-
mCidxFieldNumCats = Vect_cidx_get_num_cats_by_index( map(), mCidxFieldIndex );
295302
}
296303
}
297304
else
298305
{
299306
// TODO nofield layers
300307
mNumberFeatures = 0;
301-
mCidxFieldNumCats = 0;
302308
}
303309
}
304-
QgsDebugMsg( QString( "mNumberFeatures = %1 mCidxFieldIndex = %2 mCidxFieldNumCats = %3" ).arg( mNumberFeatures ).arg( mCidxFieldIndex ).arg( mCidxFieldNumCats ) );
305-
310+
QgsDebugMsg( QString( "mNumberFeatures = %1 mCidxFieldIndex = %2" ).arg( mNumberFeatures ).arg( mCidxFieldIndex ) );
306311
}
307312

308-
void QgsGrassProvider::update( void )
313+
void QgsGrassProvider::update()
309314
{
310315
QgsDebugMsg( "entered" );
311-
// TODO
312-
#if 0
316+
313317
mValid = false;
314318

315-
if ( !map()s[mLayers[mLayerId].mapId].valid )
319+
if ( mLayer )
320+
{
321+
mLayer->close();
322+
mLayer = 0;
323+
}
324+
325+
if ( !openLayer() )
326+
{
327+
QgsDebugMsg( "Cannot open layer" );
316328
return;
329+
}
317330

318-
// Getting the total number of features in the layer
319-
// It may happen that the field disappeares from the map (deleted features, new map without that field)
320331
loadMapInfo();
321332

322-
map()Version = map()s[mLayers[mLayerId].mapId].version;
323-
324333
mValid = true;
325-
#endif
326334
}
327335

328336
QgsGrassProvider::~QgsGrassProvider()
@@ -486,6 +494,7 @@ int QgsGrassProvider::grassLayerType( QString name )
486494
void QgsGrassProvider::onDataChanged()
487495
{
488496
QgsDebugMsg( "entered" );
497+
update();
489498
emit dataChanged();
490499
}
491500

‎src/providers/grass/qgsgrassprovider.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider
403403
private:
404404
struct Map_info * map();
405405
void setMapset();
406+
bool openLayer();
406407

407408
QgsGrassObject mGrassObject;
408409
// field part of layer or -1 if no field specified
@@ -420,8 +421,6 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider
420421

421422
// Index for layerField in category index or -1 if no such field
422423
int mCidxFieldIndex;
423-
// Number of records in field index
424-
int mCidxFieldNumCats;
425424

426425
bool mValid;
427426
long mNumberFeatures;

‎src/providers/grass/qgsgrassvectormap.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ bool QgsGrassVectorMap::startEdit()
289289
}
290290

291291
mValid = true;
292+
printDebug();
293+
292294
QgsGrass::unlock();
293295
unlockOpenClose();
294296
emit dataChanged();
@@ -527,6 +529,32 @@ QString QgsGrassVectorMap::toString()
527529
return mGrassObject.mapsetPath() + "/" + mGrassObject.name();
528530
}
529531

532+
void QgsGrassVectorMap::printDebug()
533+
{
534+
QgsDebugMsg( "entered" );
535+
if ( !mValid || !mMap )
536+
{
537+
QgsDebugMsg( "map not valid" );
538+
return;
539+
}
540+
G_TRY
541+
{
542+
int ncidx = Vect_cidx_get_num_fields( mMap );
543+
QgsDebugMsg( QString( "ncidx = %1" ).arg( ncidx ) );
544+
545+
for ( int i = 0; i < ncidx; i++ )
546+
{
547+
int layer = Vect_cidx_get_field_number( mMap, i );
548+
int ncats = Vect_cidx_get_num_cats_by_index( mMap, i );
549+
QgsDebugMsg( QString( "i = %1 layer = %2 ncats = %3" ).arg( i ).arg( layer ).arg( ncats ) );
550+
}
551+
}
552+
G_CATCH( QgsGrass::Exception &e )
553+
{
554+
QgsDebugMsg( "Cannot read info from map: " + QString( e.what() ) );
555+
}
556+
}
557+
530558
void QgsGrassVectorMap::lockOpenClose()
531559
{
532560
QgsDebugMsg( "lockOpenClose" );
@@ -626,6 +654,10 @@ QgsAbstractGeometryV2 * QgsGrassVectorMap::areaGeometry( int id )
626654
QgsPolygonV2 * polygon = new QgsPolygonV2();
627655

628656
struct line_pnts *points = Vect_new_line_struct();
657+
QgsDebugMsgLevel( QString( "points= %1" ).arg(( long )points ), 3 );
658+
// Vect_get_area_points and Vect_get_isle_pointsis using static variable -> lock
659+
// TODO: Faster to lock the whole feature iterator? Maybe only for areas?
660+
QgsGrass::lock();
629661
Vect_get_area_points( mMap, id, points );
630662

631663
QList<QgsPointV2> pointList;
@@ -653,6 +685,7 @@ QgsAbstractGeometryV2 * QgsGrassVectorMap::areaGeometry( int id )
653685
ring->setPoints( pointList );
654686
polygon->addInteriorRing( ring );
655687
}
688+
QgsGrass::unlock();
656689
Vect_destroy_line_struct( points );
657690
return polygon;
658691
}

‎src/providers/grass/qgsgrassvectormap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class GRASS_LIB_EXPORT QgsGrassVectorMap : public QObject
138138

139139
static QString topoSymbolFieldName() { return "topo_symbol" ; }
140140

141+
void printDebug();
142+
141143
signals:
142144
/** Ask all iterators to cancel iteration when possible. Connected to iterators with
143145
* Qt::DirectConnection (non blocking) */

‎src/providers/grass/qgsgrassvectormaplayer.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
4040
: mField( field )
4141
, mValid( false )
4242
, mMap( map )
43+
, mCidxFieldIndex( -1 )
4344
, mFieldInfo( 0 )
4445
, mDriver( 0 )
4546
, mHasTable( false )
@@ -51,6 +52,7 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
5152
void QgsGrassVectorMapLayer::clear()
5253
{
5354
QgsDebugMsg( "entered" );
55+
mCidxFieldIndex = -1;
5456
mTableFields.clear();
5557
mFields.clear();
5658
mAttributeFields.clear();
@@ -62,6 +64,15 @@ void QgsGrassVectorMapLayer::clear()
6264
mFieldInfo = 0;
6365
}
6466

67+
int QgsGrassVectorMapLayer::cidxFieldNumCats()
68+
{
69+
if ( !mMap->map() || mCidxFieldIndex < 0 )
70+
{
71+
return 0;
72+
}
73+
return Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
74+
}
75+
6576
void QgsGrassVectorMapLayer::load()
6677
{
6778
QgsDebugMsg( "entered" );
@@ -78,6 +89,9 @@ void QgsGrassVectorMapLayer::load()
7889
return;
7990
}
8091

92+
mCidxFieldIndex = Vect_cidx_get_field_index( mMap->map(), mField );
93+
QgsDebugMsg( QString( "mCidxFieldIndex = %1 cidxFieldNumCats() = %2" ).arg( cidxFieldNumCats() ) );
94+
8195
mFieldInfo = Vect_get_field( mMap->map(), mField ); // should work also with field = 0
8296

8397
if ( !mFieldInfo )
@@ -275,19 +289,18 @@ void QgsGrassVectorMapLayer::load()
275289
mTableFields.append( QgsField( "cat", QVariant::Int, "integer" ) );
276290
QPair<double, double> minMax( 0, 0 );
277291

278-
int cidx = Vect_cidx_get_field_index( mMap->map(), mField );
279-
if ( cidx >= 0 )
292+
if ( mCidxFieldIndex >= 0 )
280293
{
281294
int ncats, cat, type, id;
282295

283-
ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), cidx );
296+
ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
284297

285298
if ( ncats > 0 )
286299
{
287-
Vect_cidx_get_cat_by_index( mMap->map(), cidx, 0, &cat, &type, &id );
300+
Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, 0, &cat, &type, &id );
288301
minMax.first = cat;
289302

290-
Vect_cidx_get_cat_by_index( mMap->map(), cidx, ncats - 1, &cat, &type, &id );
303+
Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, ncats - 1, &cat, &type, &id );
291304
minMax.second = cat;
292305
}
293306
}

‎src/providers/grass/qgsgrassvectormaplayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
5151
bool isValid() const { return mValid; }
5252
QgsGrassVectorMap *map() { return mMap; }
5353

54+
/** Current number of cats in cat index, changing during editing */
55+
int cidxFieldNumCats();
56+
5457
/** Original fields before editing started + topo field if edited.
5558
* Does not reflect add/delete column.
5659
* Original fields must be returned by provider fields() */
@@ -68,6 +71,7 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
6871
int keyColumn() { return mKeyColumn; }
6972
QString keyColumnName() { return mFieldInfo ? mFieldInfo->key : QString(); }
7073
QList< QPair<double, double> > minMax() { return mMinMax; }
74+
7175
int userCount() { return mUsers; }
7276
void addUser();
7377
void removeUser();
@@ -144,6 +148,7 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
144148
int mField;
145149
bool mValid;
146150
QgsGrassVectorMap *mMap;
151+
int mCidxFieldIndex;
147152
struct field_info *mFieldInfo;
148153
dbDriver *mDriver;
149154

0 commit comments

Comments
 (0)
Please sign in to comment.