/
qgsgrassfeatureiterator.cpp
638 lines (536 loc) · 17.8 KB
/
qgsgrassfeatureiterator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/***************************************************************************
qgsgrassfeatureiterator.cpp
---------------------
begin : Juli 2012
copyright : (C) 2012 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QObject>
#include <QTextCodec>
#include "qgsgrassfeatureiterator.h"
#include "qgsgrassprovider.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
extern "C"
{
#include <grass/version.h>
#if GRASS_VERSION_MAJOR < 7
#include <grass/Vect.h>
#else
#include <grass/vector.h>
#define BOUND_BOX bound_box
#endif
}
#if GRASS_VERSION_MAJOR < 7
#else
void copy_boxlist_and_destroy( struct boxlist *blist, struct ilist * list )
{
Vect_reset_list( list );
for ( int i = 0; i < blist->n_values; i++ )
{
Vect_list_append( list, blist->id[i] );
}
Vect_destroy_boxlist( blist );
}
#define Vect_select_lines_by_box(map, box, type, list) \
{ \
struct boxlist *blist = Vect_new_boxlist(0);\
Vect_select_lines_by_box( (map), (box), (type), blist); \
copy_boxlist_and_destroy( blist, (list) );\
}
#define Vect_select_areas_by_box(map, box, list) \
{ \
struct boxlist *blist = Vect_new_boxlist(0);\
Vect_select_areas_by_box( (map), (box), blist); \
copy_boxlist_and_destroy( blist, (list) );\
}
#endif
QMutex QgsGrassFeatureIterator::sMutex;
QgsGrassFeatureIterator::QgsGrassFeatureIterator( QgsGrassFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
: QgsAbstractFeatureIteratorFromSource<QgsGrassFeatureSource>( source, ownSource, request )
{
sMutex.lock();
// Init structures
mPoints = Vect_new_line_struct();
mCats = Vect_new_cats_struct();
mList = Vect_new_list();
// Create selection array
allocateSelection( mSource->mMap );
resetSelection( 1 );
if ( request.filterType() == QgsFeatureRequest::FilterRect )
{
setSelectionRect( request.filterRect(), request.flags() & QgsFeatureRequest::ExactIntersect );
}
else
{
// TODO: implement fast lookup by feature id
//no filter - use all features
resetSelection( 1 );
}
}
void QgsGrassFeatureIterator::setSelectionRect( const QgsRectangle& rect, bool useIntersect )
{
//apply selection rectangle
resetSelection( 0 );
BOUND_BOX box;
box.N = rect.yMaximum(); box.S = rect.yMinimum();
box.E = rect.xMaximum(); box.W = rect.xMinimum();
box.T = PORT_DOUBLE_MAX; box.B = -PORT_DOUBLE_MAX;
if ( !useIntersect )
{ // select by bounding boxes only
if ( mSource->mLayerType == QgsGrassProvider::POINT || mSource->mLayerType == QgsGrassProvider::CENTROID ||
mSource->mLayerType == QgsGrassProvider::LINE || mSource->mLayerType == QgsGrassProvider::FACE ||
mSource->mLayerType == QgsGrassProvider::BOUNDARY ||
mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
{
Vect_select_lines_by_box( mSource->mMap, &box, mSource->mGrassType, mList );
}
else if ( mSource->mLayerType == QgsGrassProvider::POLYGON )
{
Vect_select_areas_by_box( mSource->mMap, &box, mList );
}
else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
Vect_select_nodes_by_box( mSource->mMap, &box, mList );
}
}
else
{ // check intersection
struct line_pnts *Polygon;
Polygon = Vect_new_line_struct();
// Using z coor -PORT_DOUBLE_MAX/PORT_DOUBLE_MAX we cover 3D, Vect_select_lines_by_polygon is
// using dig_line_box to get the box, it is not perfect, Vect_select_lines_by_polygon
// should clarify better how 2D/3D is treated
Vect_append_point( Polygon, rect.xMinimum(), rect.yMinimum(), -PORT_DOUBLE_MAX );
Vect_append_point( Polygon, rect.xMaximum(), rect.yMinimum(), PORT_DOUBLE_MAX );
Vect_append_point( Polygon, rect.xMaximum(), rect.yMaximum(), 0 );
Vect_append_point( Polygon, rect.xMinimum(), rect.yMaximum(), 0 );
Vect_append_point( Polygon, rect.xMinimum(), rect.yMinimum(), 0 );
if ( mSource->mLayerType == QgsGrassProvider::POINT || mSource->mLayerType == QgsGrassProvider::CENTROID ||
mSource->mLayerType == QgsGrassProvider::LINE || mSource->mLayerType == QgsGrassProvider::FACE ||
mSource->mLayerType == QgsGrassProvider::BOUNDARY ||
mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
{
Vect_select_lines_by_polygon( mSource->mMap, Polygon, 0, NULL, mSource->mGrassType, mList );
}
else if ( mSource->mLayerType == QgsGrassProvider::POLYGON )
{
Vect_select_areas_by_polygon( mSource->mMap, Polygon, 0, NULL, mList );
}
else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
// There is no Vect_select_nodes_by_polygon but for nodes it is the same as by box
Vect_select_nodes_by_box( mSource->mMap, &box, mList );
}
Vect_destroy_line_struct( Polygon );
}
for ( int i = 0; i < mList->n_values; i++ )
{
if ( mList->value[i] <= mSelectionSize )
{
mSelection[mList->value[i]] = 1;
}
else
{
QgsDebugMsg( "Selected element out of range" );
}
}
}
QgsGrassFeatureIterator::~QgsGrassFeatureIterator()
{
close();
}
bool QgsGrassFeatureIterator::fetchFeature( QgsFeature& feature )
{
if ( mClosed )
return false;
feature.setValid( false );
int cat = -1, type = -1, id = -1;
QgsFeatureId featureId = -1;
QgsDebugMsgLevel( "entered.", 3 );
/* TODO: handle editing
if ( P->isEdited() || P->isFrozen() || !P->mValid )
{
close();
return false;
}
*/
// TODO: is this necessary? the same is checked below
if ( !QgsGrassProvider::isTopoType( mSource->mLayerType ) && ( mSource->mCidxFieldIndex < 0 || mNextCidx >= mSource->mCidxFieldNumCats ) )
{
close();
return false; // No features, no features in this layer
}
bool filterById = mRequest.filterType() == QgsFeatureRequest::FilterFid;
// Get next line/area id
int found = 0;
while ( true )
{
QgsDebugMsgLevel( QString( "mNextTopoId = %1" ).arg( mNextTopoId ), 3 );
if ( mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
{
if ( mNextTopoId > Vect_get_num_lines( mSource->mMap ) ) break;
id = mNextTopoId;
type = Vect_read_line( mSource->mMap, 0, 0, mNextTopoId++ );
if ( !( type & mSource->mGrassType ) ) continue;
featureId = id;
}
else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
if ( mNextTopoId > Vect_get_num_nodes( mSource->mMap ) ) break;
id = mNextTopoId;
type = 0;
mNextTopoId++;
featureId = id;
}
else
{
if ( mNextCidx >= mSource->mCidxFieldNumCats ) break;
Vect_cidx_get_cat_by_index( mSource->mMap, mSource->mCidxFieldIndex, mNextCidx++, &cat, &type, &id );
// Warning: selection array is only of type line/area of current layer -> check type first
if ( !( type & mSource->mGrassType ) )
continue;
// The 'id' is a unique id of a GRASS geometry object (point, line, area)
// but it cannot be used as QgsFeatureId because one geometry object may
// represent more features because it may have more categories.
featureId = makeFeatureId( id, cat );
}
if ( filterById && featureId != mRequest.filterFid() )
continue;
// it is correct to use id with mSelection because mSelection is only used
// for geometry selection
if ( !mSelection[id] )
continue;
found = 1;
break;
}
if ( !found )
{
close();
return false; // No more features
}
QgsDebugMsgLevel( QString( "cat = %1 type = %2 id = %3 fatureId = %4" ).arg( cat ).arg( type ).arg( id ).arg( featureId ), 3 );
feature.setFeatureId( featureId );
feature.initAttributes( mSource->mFields.count() );
feature.setFields( mSource->mFields ); // allow name-based attribute lookups
if ( mRequest.flags() & QgsFeatureRequest::NoGeometry )
feature.setGeometry( 0 );
else
setFeatureGeometry( feature, id, type );
if ( ! QgsGrassProvider::isTopoType( mSource->mLayerType ) )
{
if ( mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes )
setFeatureAttributes( cat, &feature, mRequest.subsetOfAttributes() );
else
setFeatureAttributes( cat, &feature );
}
else
{
feature.setAttribute( 0, id );
#if GRASS_VERSION_MAJOR < 7
if ( mSource->mLayerType == QgsGrassProvider::TOPO_POINT || mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
#else
/* No more topo points in GRASS 7 */
if ( mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
#endif
{
feature.setAttribute( 1, QgsGrassProvider::primitiveTypeName( type ) );
int node1, node2;
Vect_get_line_nodes( mSource->mMap, id, &node1, &node2 );
feature.setAttribute( 2, node1 );
if ( mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
{
feature.setAttribute( 3, node2 );
}
}
if ( mSource->mLayerType == QgsGrassProvider::TOPO_LINE )
{
if ( type == GV_BOUNDARY )
{
int left, right;
Vect_get_line_areas( mSource->mMap, id, &left, &right );
feature.setAttribute( 4, left );
feature.setAttribute( 5, right );
}
}
else if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
QString lines;
int nlines = Vect_get_node_n_lines( mSource->mMap, id );
for ( int i = 0; i < nlines; i++ )
{
int line = Vect_get_node_line( mSource->mMap, id, i );
if ( i > 0 ) lines += ",";
lines += QString::number( line );
}
feature.setAttribute( 1, lines );
}
}
feature.setValid( true );
return true;
}
bool QgsGrassFeatureIterator::rewind()
{
if ( mClosed )
return false;
/* TODO: handle editing
if ( P->isEdited() || P->isFrozen() || !P->mValid )
return false;
*/
mNextCidx = 0;
mNextTopoId = 1;
return true;
}
bool QgsGrassFeatureIterator::close()
{
if ( mClosed )
return false;
iteratorClosed();
// finalization
Vect_destroy_line_struct( mPoints );
Vect_destroy_cats_struct( mCats );
Vect_destroy_list( mList );
free( mSelection );
sMutex.unlock();
mClosed = true;
return true;
}
//////////////////
void QgsGrassFeatureIterator::resetSelection( bool sel )
{
QgsDebugMsg( "entered." );
memset( mSelection, ( int ) sel, mSelectionSize );
mNextCidx = 0;
mNextTopoId = 1;
}
void QgsGrassFeatureIterator::allocateSelection( struct Map_info *map )
{
int size;
QgsDebugMsg( "entered." );
int nlines = Vect_get_num_lines( map );
int nareas = Vect_get_num_areas( map );
if ( nlines > nareas )
{
size = nlines + 1;
}
else
{
size = nareas + 1;
}
QgsDebugMsg( QString( "nlines = %1 nareas = %2 size = %3" ).arg( nlines ).arg( nareas ).arg( size ) );
mSelection = ( char * ) malloc( size );
mSelectionSize = size;
}
void QgsGrassFeatureIterator::setFeatureGeometry( QgsFeature& feature, int id, int type )
{
unsigned char *wkb;
int wkbsize;
// TODO int may be 64 bits (memcpy)
if ( type & ( GV_POINTS | GV_LINES | GV_FACE ) || mSource->mLayerType == QgsGrassProvider::TOPO_NODE ) /* points or lines */
{
if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
double x, y, z;
Vect_get_node_coor( mSource->mMap, id, &x, &y, &z );
Vect_reset_line( mPoints );
Vect_append_point( mPoints, x, y, z );
}
else
{
Vect_read_line( mSource->mMap, mPoints, 0, id );
}
int npoints = mPoints->n_points;
if ( mSource->mLayerType == QgsGrassProvider::TOPO_NODE )
{
wkbsize = 1 + 4 + 2 * 8;
}
else if ( type & GV_POINTS )
{
wkbsize = 1 + 4 + 2 * 8;
}
else if ( type & GV_LINES )
{
wkbsize = 1 + 4 + 4 + npoints * 2 * 8;
}
else // GV_FACE
{
wkbsize = 1 + 4 + 4 + 4 + npoints * 2 * 8;
}
wkb = new unsigned char[wkbsize];
unsigned char *wkbp = wkb;
wkbp[0] = ( unsigned char ) QgsApplication::endian();
wkbp += 1;
/* WKB type */
memcpy( wkbp, &mSource->mQgisType, 4 );
wkbp += 4;
/* Number of rings */
if ( type & GV_FACE )
{
int nrings = 1;
memcpy( wkbp, &nrings, 4 );
wkbp += 4;
}
/* number of points */
if ( type & ( GV_LINES | GV_FACE ) )
{
QgsDebugMsg( QString( "set npoints = %1" ).arg( npoints ) );
memcpy( wkbp, &npoints, 4 );
wkbp += 4;
}
for ( int i = 0; i < npoints; i++ )
{
memcpy( wkbp, &( mPoints->x[i] ), 8 );
memcpy( wkbp + 8, &( mPoints->y[i] ), 8 );
wkbp += 16;
}
}
else // GV_AREA
{
Vect_get_area_points( mSource->mMap, id, mPoints );
int npoints = mPoints->n_points;
wkbsize = 1 + 4 + 4 + 4 + npoints * 2 * 8; // size without islands
wkb = new unsigned char[wkbsize];
wkb[0] = ( unsigned char ) QgsApplication::endian();
int offset = 1;
/* WKB type */
memcpy( wkb + offset, &mSource->mQgisType, 4 );
offset += 4;
/* Number of rings */
int nisles = Vect_get_area_num_isles( mSource->mMap, id );
int nrings = 1 + nisles;
memcpy( wkb + offset, &nrings, 4 );
offset += 4;
/* Outer ring */
memcpy( wkb + offset, &npoints, 4 );
offset += 4;
for ( int i = 0; i < npoints; i++ )
{
memcpy( wkb + offset, &( mPoints->x[i] ), 8 );
memcpy( wkb + offset + 8, &( mPoints->y[i] ), 8 );
offset += 16;
}
/* Isles */
for ( int i = 0; i < nisles; i++ )
{
Vect_get_isle_points( mSource->mMap, Vect_get_area_isle( mSource->mMap, id, i ), mPoints );
npoints = mPoints->n_points;
// add space
wkbsize += 4 + npoints * 2 * 8;
wkb = ( unsigned char * ) realloc( wkb, wkbsize );
memcpy( wkb + offset, &npoints, 4 );
offset += 4;
for ( int i = 0; i < npoints; i++ )
{
memcpy( wkb + offset, &( mPoints->x[i] ), 8 );
memcpy( wkb + offset + 8, &( mPoints->y[i] ), 8 );
offset += 16;
}
}
}
feature.setGeometryAndOwnership( wkb, wkbsize );
}
QgsFeatureId QgsGrassFeatureIterator::makeFeatureId( int grassId, int cat )
{
// Because GRASS object id and category are both int and QgsFeatureId is qint64
// we can create unique QgsFeatureId from GRASS id and cat
return ( QgsFeatureId )grassId * 1000000000 + cat;
}
/** Set feature attributes */
void QgsGrassFeatureIterator::setFeatureAttributes( int cat, QgsFeature *feature )
{
#if QGISDEBUG > 3
QgsDebugMsg( QString( "setFeatureAttributes cat = %1" ).arg( cat ) );
#endif
GLAYER& glayer = QgsGrassProvider::mLayers[mSource->mLayerId];
if ( glayer.nColumns > 0 )
{
// find cat
GATT key;
key.cat = cat;
GATT *att = ( GATT * ) bsearch( &key, glayer.attributes, glayer.nAttributes,
sizeof( GATT ), QgsGrassProvider::cmpAtt );
feature->initAttributes( glayer.nColumns );
for ( int i = 0; i < glayer.nColumns; i++ )
{
if ( att != NULL )
{
QByteArray cstr( att->values[i] );
feature->setAttribute( i, QgsGrassProvider::convertValue( glayer.fields[i].type(), mSource->mEncoding->toUnicode( cstr ) ) );
}
else /* it may happen that attributes are missing -> set to empty string */
{
feature->setAttribute( i, QVariant() );
}
}
}
else
{
feature->initAttributes( 1 );
feature->setAttribute( 0, QVariant( cat ) );
}
}
void QgsGrassFeatureIterator::setFeatureAttributes( int cat, QgsFeature *feature, const QgsAttributeList& attlist )
{
#if QGISDEBUG > 3
QgsDebugMsg( QString( "setFeatureAttributes cat = %1" ).arg( cat ) );
#endif
GLAYER& glayer = QgsGrassProvider::mLayers[mSource->mLayerId];
if ( glayer.nColumns > 0 )
{
// find cat
GATT key;
key.cat = cat;
GATT *att = ( GATT * ) bsearch( &key, glayer.attributes, glayer.nAttributes,
sizeof( GATT ), QgsGrassProvider::cmpAtt );
feature->initAttributes( glayer.nColumns );
for ( QgsAttributeList::const_iterator iter = attlist.begin(); iter != attlist.end(); ++iter )
{
if ( att != NULL )
{
QByteArray cstr( att->values[*iter] );
feature->setAttribute( *iter, QgsGrassProvider::convertValue( glayer.fields[*iter].type(), mSource->mEncoding->toUnicode( cstr ) ) );
}
else /* it may happen that attributes are missing -> set to empty string */
{
feature->setAttribute( *iter, QVariant() );
}
}
}
else
{
feature->initAttributes( 1 );
feature->setAttribute( 0, QVariant( cat ) );
}
}
// ------------------
QgsGrassFeatureSource::QgsGrassFeatureSource( const QgsGrassProvider* p )
: mMap( p->mMap )
, mLayerType( p->mLayerType )
, mGrassType( p->mGrassType )
, mLayerId( p->mLayerId )
, mQgisType( p->mQgisType )
, mCidxFieldIndex( p->mCidxFieldIndex )
, mCidxFieldNumCats( p->mCidxFieldNumCats )
, mFields( p->fields() )
, mEncoding( p->mEncoding )
{
int layerId = QgsGrassProvider::openLayer( p->mGisdbase, p->mLocation, p->mMapset, p->mMapName, p->mLayerField );
Q_ASSERT( layerId == mLayerId );
Q_UNUSED( layerId ); //avoid compilier warning
}
QgsGrassFeatureSource::~QgsGrassFeatureSource()
{
QgsGrassProvider::closeLayer( mLayerId );
}
QgsFeatureIterator QgsGrassFeatureSource::getFeatures( const QgsFeatureRequest& request )
{
return QgsFeatureIterator( new QgsGrassFeatureIterator( this, false, request ) );
}