25
25
#include < cstring>
26
26
27
27
28
- QgsGPXFeatureIterator::QgsGPXFeatureIterator ( QgsGPXProvider* p, const QgsFeatureRequest& request )
29
- : QgsAbstractFeatureIterator( request )
30
- , P( p )
28
+ QgsGPXFeatureIterator::QgsGPXFeatureIterator ( QgsGPXFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
29
+ : QgsAbstractFeatureIteratorFromSource( source, ownSource, request )
31
30
{
32
- P->mActiveIterators << this ;
33
31
rewind ();
34
32
}
35
33
@@ -49,12 +47,12 @@ bool QgsGPXFeatureIterator::rewind()
49
47
}
50
48
else
51
49
{
52
- if ( P ->mFeatureType == QgsGPXProvider::WaypointType )
53
- mWptIter = P ->data ->waypointsBegin ();
54
- else if ( P ->mFeatureType == QgsGPXProvider::RouteType )
55
- mRteIter = P ->data ->routesBegin ();
56
- else if ( P ->mFeatureType == QgsGPXProvider::TrackType )
57
- mTrkIter = P ->data ->tracksBegin ();
50
+ if ( mSource ->mFeatureType == QgsGPXProvider::WaypointType )
51
+ mWptIter = mSource ->data ->waypointsBegin ();
52
+ else if ( mSource ->mFeatureType == QgsGPXProvider::RouteType )
53
+ mRteIter = mSource ->data ->routesBegin ();
54
+ else if ( mSource ->mFeatureType == QgsGPXProvider::TrackType )
55
+ mTrkIter = mSource ->data ->tracksBegin ();
58
56
}
59
57
60
58
return true ;
@@ -65,7 +63,7 @@ bool QgsGPXFeatureIterator::close()
65
63
if ( mClosed )
66
64
return false ;
67
65
68
- P-> mActiveIterators . remove ( this );
66
+ iteratorClosed ( );
69
67
70
68
mClosed = true ;
71
69
return true ;
@@ -85,11 +83,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
85
83
return res;
86
84
}
87
85
88
- if ( P ->mFeatureType == QgsGPXProvider::WaypointType )
86
+ if ( mSource ->mFeatureType == QgsGPXProvider::WaypointType )
89
87
{
90
88
// go through the list of waypoints and return the first one that is in
91
89
// the bounds rectangle
92
- for ( ; mWptIter != P ->data ->waypointsEnd (); ++mWptIter )
90
+ for ( ; mWptIter != mSource ->data ->waypointsEnd (); ++mWptIter )
93
91
{
94
92
if ( readWaypoint ( *mWptIter , feature ) )
95
93
{
@@ -98,11 +96,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
98
96
}
99
97
}
100
98
}
101
- else if ( P ->mFeatureType == QgsGPXProvider::RouteType )
99
+ else if ( mSource ->mFeatureType == QgsGPXProvider::RouteType )
102
100
{
103
101
// go through the routes and return the first one that is in the bounds
104
102
// rectangle
105
- for ( ; mRteIter != P ->data ->routesEnd (); ++mRteIter )
103
+ for ( ; mRteIter != mSource ->data ->routesEnd (); ++mRteIter )
106
104
{
107
105
if ( readRoute ( *mRteIter , feature ) )
108
106
{
@@ -111,11 +109,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
111
109
}
112
110
}
113
111
}
114
- else if ( P ->mFeatureType == QgsGPXProvider::TrackType )
112
+ else if ( mSource ->mFeatureType == QgsGPXProvider::TrackType )
115
113
{
116
114
// go through the tracks and return the first one that is in the bounds
117
115
// rectangle
118
- for ( ; mTrkIter != P ->data ->tracksEnd (); ++mTrkIter )
116
+ for ( ; mTrkIter != mSource ->data ->tracksEnd (); ++mTrkIter )
119
117
{
120
118
if ( readTrack ( *mTrkIter , feature ) )
121
119
{
@@ -138,9 +136,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
138
136
mFetchedFid = true ;
139
137
QgsFeatureId fid = mRequest .filterFid ();
140
138
141
- if ( P ->mFeatureType == QgsGPXProvider::WaypointType )
139
+ if ( mSource ->mFeatureType == QgsGPXProvider::WaypointType )
142
140
{
143
- for ( QgsGPSData::WaypointIterator it = P ->data ->waypointsBegin () ; it != P ->data ->waypointsEnd (); ++it )
141
+ for ( QgsGPSData::WaypointIterator it = mSource ->data ->waypointsBegin () ; it != mSource ->data ->waypointsEnd (); ++it )
144
142
{
145
143
if ( it->id == fid )
146
144
{
@@ -149,9 +147,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
149
147
}
150
148
}
151
149
}
152
- else if ( P ->mFeatureType == QgsGPXProvider::RouteType )
150
+ else if ( mSource ->mFeatureType == QgsGPXProvider::RouteType )
153
151
{
154
- for ( QgsGPSData::RouteIterator it = P ->data ->routesBegin () ; it != P ->data ->routesEnd (); ++it )
152
+ for ( QgsGPSData::RouteIterator it = mSource ->data ->routesBegin () ; it != mSource ->data ->routesEnd (); ++it )
155
153
{
156
154
if ( it->id == fid )
157
155
{
@@ -160,9 +158,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
160
158
}
161
159
}
162
160
}
163
- else if ( P ->mFeatureType == QgsGPXProvider::TrackType )
161
+ else if ( mSource ->mFeatureType == QgsGPXProvider::TrackType )
164
162
{
165
- for ( QgsGPSData::TrackIterator it = P ->data ->tracksBegin () ; it != P ->data ->tracksEnd (); ++it )
163
+ for ( QgsGPSData::TrackIterator it = mSource ->data ->tracksBegin () ; it != mSource ->data ->tracksEnd (); ++it )
166
164
{
167
165
if ( it->id == fid )
168
166
{
@@ -192,8 +190,8 @@ bool QgsGPXFeatureIterator::readWaypoint( const QgsWaypoint& wpt, QgsFeature& fe
192
190
}
193
191
feature.setFeatureId ( wpt.id );
194
192
feature.setValid ( true );
195
- feature.setFields ( &P-> attributeFields ); // allow name-based attribute lookups
196
- feature.initAttributes ( P-> attributeFields .count () );
193
+ feature.setFields ( &mSource -> mFields ); // allow name-based attribute lookups
194
+ feature.initAttributes ( mSource -> mFields .count () );
197
195
198
196
readAttributes ( feature, wpt );
199
197
@@ -232,8 +230,8 @@ bool QgsGPXFeatureIterator::readRoute( const QgsRoute& rte, QgsFeature& feature
232
230
}
233
231
feature.setFeatureId ( rte.id );
234
232
feature.setValid ( true );
235
- feature.setFields ( &P-> attributeFields ); // allow name-based attribute lookups
236
- feature.initAttributes ( P-> attributeFields .count () );
233
+ feature.setFields ( &mSource -> mFields ); // allow name-based attribute lookups
234
+ feature.initAttributes ( mSource -> mFields .count () );
237
235
238
236
readAttributes ( feature, rte );
239
237
@@ -271,8 +269,8 @@ bool QgsGPXFeatureIterator::readTrack( const QgsTrack& trk, QgsFeature& feature
271
269
}
272
270
feature.setFeatureId ( trk.id );
273
271
feature.setValid ( true );
274
- feature.setFields ( &P-> attributeFields ); // allow name-based attribute lookups
275
- feature.initAttributes ( P-> attributeFields .count () );
272
+ feature.setFields ( &mSource -> mFields ); // allow name-based attribute lookups
273
+ feature.initAttributes ( mSource -> mFields .count () );
276
274
277
275
readAttributes ( feature, trk );
278
276
@@ -283,9 +281,9 @@ bool QgsGPXFeatureIterator::readTrack( const QgsTrack& trk, QgsFeature& feature
283
281
void QgsGPXFeatureIterator::readAttributes ( QgsFeature& feature, const QgsWaypoint& wpt )
284
282
{
285
283
// add attributes if they are wanted
286
- for ( int i = 0 ; i < P-> attributeFields .count (); ++i )
284
+ for ( int i = 0 ; i < mSource -> mFields .count (); ++i )
287
285
{
288
- switch ( P ->indexToAttr [i] )
286
+ switch ( mSource ->indexToAttr [i] )
289
287
{
290
288
case QgsGPXProvider::NameAttr:
291
289
feature.setAttribute ( i, QVariant ( wpt.name ) );
@@ -319,9 +317,9 @@ void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsWaypoi
319
317
void QgsGPXFeatureIterator::readAttributes ( QgsFeature& feature, const QgsRoute& rte )
320
318
{
321
319
// add attributes if they are wanted
322
- for ( int i = 0 ; i < P-> attributeFields .count (); ++i )
320
+ for ( int i = 0 ; i < mSource -> mFields .count (); ++i )
323
321
{
324
- switch ( P ->indexToAttr [i] )
322
+ switch ( mSource ->indexToAttr [i] )
325
323
{
326
324
case QgsGPXProvider::NameAttr:
327
325
feature.setAttribute ( i, QVariant ( rte.name ) );
@@ -353,9 +351,9 @@ void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsRoute&
353
351
void QgsGPXFeatureIterator::readAttributes ( QgsFeature& feature, const QgsTrack& trk )
354
352
{
355
353
// add attributes if they are wanted
356
- for ( int i = 0 ; i < P-> attributeFields .count (); ++i )
354
+ for ( int i = 0 ; i < mSource -> mFields .count (); ++i )
357
355
{
358
- switch ( P ->indexToAttr [i] )
356
+ switch ( mSource ->indexToAttr [i] )
359
357
{
360
358
case QgsGPXProvider::NameAttr:
361
359
feature.setAttribute ( i, QVariant ( trk.name ) );
@@ -469,3 +467,25 @@ QgsGeometry* QgsGPXFeatureIterator::readTrackGeometry( const QgsTrack& trk )
469
467
theGeometry->fromWkb (( unsigned char * )geo, 9 + 16 * totalPoints );
470
468
return theGeometry;
471
469
}
470
+
471
+
472
+ // ------------
473
+
474
+ QgsGPXFeatureSource::QgsGPXFeatureSource ( const QgsGPXProvider* p )
475
+ : mFileName( p->mFileName )
476
+ , mFeatureType( p->mFeatureType )
477
+ , indexToAttr( p->indexToAttr )
478
+ , mFields( p->attributeFields )
479
+ {
480
+ data = QgsGPSData::getData ( mFileName );
481
+ }
482
+
483
+ QgsGPXFeatureSource::~QgsGPXFeatureSource ()
484
+ {
485
+ QgsGPSData::releaseData ( mFileName );
486
+ }
487
+
488
+ QgsFeatureIterator QgsGPXFeatureSource::getFeatures ( const QgsFeatureRequest& request )
489
+ {
490
+ return QgsFeatureIterator ( new QgsGPXFeatureIterator ( this , false , request ) );
491
+ }
0 commit comments