Skip to content
Permalink
Browse files

GPX: added support for featureSource()

  • Loading branch information
wonder-sk committed Jan 11, 2014
1 parent 8d21cbf commit 557cc6ffe72f2b47996b344ee288faf82b1dc329
@@ -25,11 +25,9 @@
#include <cstring>


QgsGPXFeatureIterator::QgsGPXFeatureIterator( QgsGPXProvider* p, const QgsFeatureRequest& request )
: QgsAbstractFeatureIterator( request )
, P( p )
QgsGPXFeatureIterator::QgsGPXFeatureIterator( QgsGPXFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
: QgsAbstractFeatureIteratorFromSource( source, ownSource, request )
{
P->mActiveIterators << this;
rewind();
}

@@ -49,12 +47,12 @@ bool QgsGPXFeatureIterator::rewind()
}
else
{
if ( P->mFeatureType == QgsGPXProvider::WaypointType )
mWptIter = P->data->waypointsBegin();
else if ( P->mFeatureType == QgsGPXProvider::RouteType )
mRteIter = P->data->routesBegin();
else if ( P->mFeatureType == QgsGPXProvider::TrackType )
mTrkIter = P->data->tracksBegin();
if ( mSource->mFeatureType == QgsGPXProvider::WaypointType )
mWptIter = mSource->data->waypointsBegin();
else if ( mSource->mFeatureType == QgsGPXProvider::RouteType )
mRteIter = mSource->data->routesBegin();
else if ( mSource->mFeatureType == QgsGPXProvider::TrackType )
mTrkIter = mSource->data->tracksBegin();
}

return true;
@@ -65,7 +63,7 @@ bool QgsGPXFeatureIterator::close()
if ( mClosed )
return false;

P->mActiveIterators.remove( this );
iteratorClosed();

mClosed = true;
return true;
@@ -85,11 +83,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
return res;
}

if ( P->mFeatureType == QgsGPXProvider::WaypointType )
if ( mSource->mFeatureType == QgsGPXProvider::WaypointType )
{
// go through the list of waypoints and return the first one that is in
// the bounds rectangle
for ( ; mWptIter != P->data->waypointsEnd(); ++mWptIter )
for ( ; mWptIter != mSource->data->waypointsEnd(); ++mWptIter )
{
if ( readWaypoint( *mWptIter, feature ) )
{
@@ -98,11 +96,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
}
}
}
else if ( P->mFeatureType == QgsGPXProvider::RouteType )
else if ( mSource->mFeatureType == QgsGPXProvider::RouteType )
{
// go through the routes and return the first one that is in the bounds
// rectangle
for ( ; mRteIter != P->data->routesEnd(); ++mRteIter )
for ( ; mRteIter != mSource->data->routesEnd(); ++mRteIter )
{
if ( readRoute( *mRteIter, feature ) )
{
@@ -111,11 +109,11 @@ bool QgsGPXFeatureIterator::fetchFeature( QgsFeature& feature )
}
}
}
else if ( P->mFeatureType == QgsGPXProvider::TrackType )
else if ( mSource->mFeatureType == QgsGPXProvider::TrackType )
{
// go through the tracks and return the first one that is in the bounds
// rectangle
for ( ; mTrkIter != P->data->tracksEnd(); ++mTrkIter )
for ( ; mTrkIter != mSource->data->tracksEnd(); ++mTrkIter )
{
if ( readTrack( *mTrkIter, feature ) )
{
@@ -138,9 +136,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
mFetchedFid = true;
QgsFeatureId fid = mRequest.filterFid();

if ( P->mFeatureType == QgsGPXProvider::WaypointType )
if ( mSource->mFeatureType == QgsGPXProvider::WaypointType )
{
for ( QgsGPSData::WaypointIterator it = P->data->waypointsBegin() ; it != P->data->waypointsEnd(); ++it )
for ( QgsGPSData::WaypointIterator it = mSource->data->waypointsBegin() ; it != mSource->data->waypointsEnd(); ++it )
{
if ( it->id == fid )
{
@@ -149,9 +147,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
}
}
}
else if ( P->mFeatureType == QgsGPXProvider::RouteType )
else if ( mSource->mFeatureType == QgsGPXProvider::RouteType )
{
for ( QgsGPSData::RouteIterator it = P->data->routesBegin() ; it != P->data->routesEnd(); ++it )
for ( QgsGPSData::RouteIterator it = mSource->data->routesBegin() ; it != mSource->data->routesEnd(); ++it )
{
if ( it->id == fid )
{
@@ -160,9 +158,9 @@ bool QgsGPXFeatureIterator::readFid( QgsFeature& feature )
}
}
}
else if ( P->mFeatureType == QgsGPXProvider::TrackType )
else if ( mSource->mFeatureType == QgsGPXProvider::TrackType )
{
for ( QgsGPSData::TrackIterator it = P->data->tracksBegin() ; it != P->data->tracksEnd(); ++it )
for ( QgsGPSData::TrackIterator it = mSource->data->tracksBegin() ; it != mSource->data->tracksEnd(); ++it )
{
if ( it->id == fid )
{
@@ -192,8 +190,8 @@ bool QgsGPXFeatureIterator::readWaypoint( const QgsWaypoint& wpt, QgsFeature& fe
}
feature.setFeatureId( wpt.id );
feature.setValid( true );
feature.setFields( &P->attributeFields ); // allow name-based attribute lookups
feature.initAttributes( P->attributeFields.count() );
feature.setFields( &mSource->mFields ); // allow name-based attribute lookups
feature.initAttributes( mSource->mFields.count() );

readAttributes( feature, wpt );

@@ -232,8 +230,8 @@ bool QgsGPXFeatureIterator::readRoute( const QgsRoute& rte, QgsFeature& feature
}
feature.setFeatureId( rte.id );
feature.setValid( true );
feature.setFields( &P->attributeFields ); // allow name-based attribute lookups
feature.initAttributes( P->attributeFields.count() );
feature.setFields( &mSource->mFields ); // allow name-based attribute lookups
feature.initAttributes( mSource->mFields.count() );

readAttributes( feature, rte );

@@ -271,8 +269,8 @@ bool QgsGPXFeatureIterator::readTrack( const QgsTrack& trk, QgsFeature& feature
}
feature.setFeatureId( trk.id );
feature.setValid( true );
feature.setFields( &P->attributeFields ); // allow name-based attribute lookups
feature.initAttributes( P->attributeFields.count() );
feature.setFields( &mSource->mFields ); // allow name-based attribute lookups
feature.initAttributes( mSource->mFields.count() );

readAttributes( feature, trk );

@@ -283,9 +281,9 @@ bool QgsGPXFeatureIterator::readTrack( const QgsTrack& trk, QgsFeature& feature
void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsWaypoint& wpt )
{
// add attributes if they are wanted
for ( int i = 0; i < P->attributeFields.count(); ++i )
for ( int i = 0; i < mSource->mFields.count(); ++i )
{
switch ( P->indexToAttr[i] )
switch ( mSource->indexToAttr[i] )
{
case QgsGPXProvider::NameAttr:
feature.setAttribute( i, QVariant( wpt.name ) );
@@ -319,9 +317,9 @@ void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsWaypoi
void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsRoute& rte )
{
// add attributes if they are wanted
for ( int i = 0; i < P->attributeFields.count(); ++i )
for ( int i = 0; i < mSource->mFields.count(); ++i )
{
switch ( P->indexToAttr[i] )
switch ( mSource->indexToAttr[i] )
{
case QgsGPXProvider::NameAttr:
feature.setAttribute( i, QVariant( rte.name ) );
@@ -353,9 +351,9 @@ void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsRoute&
void QgsGPXFeatureIterator::readAttributes( QgsFeature& feature, const QgsTrack& trk )
{
// add attributes if they are wanted
for ( int i = 0; i < P->attributeFields.count(); ++i )
for ( int i = 0; i < mSource->mFields.count(); ++i )
{
switch ( P->indexToAttr[i] )
switch ( mSource->indexToAttr[i] )
{
case QgsGPXProvider::NameAttr:
feature.setAttribute( i, QVariant( trk.name ) );
@@ -469,3 +467,25 @@ QgsGeometry* QgsGPXFeatureIterator::readTrackGeometry( const QgsTrack& trk )
theGeometry->fromWkb(( unsigned char * )geo, 9 + 16 * totalPoints );
return theGeometry;
}


// ------------

QgsGPXFeatureSource::QgsGPXFeatureSource( const QgsGPXProvider* p )
: mFileName( p->mFileName )
, mFeatureType( p->mFeatureType )
, indexToAttr( p->indexToAttr )
, mFields( p->attributeFields )
{
data = QgsGPSData::getData( mFileName );
}

QgsGPXFeatureSource::~QgsGPXFeatureSource()
{
QgsGPSData::releaseData( mFileName );
}

QgsFeatureIterator QgsGPXFeatureSource::getFeatures( const QgsFeatureRequest& request )
{
return QgsFeatureIterator( new QgsGPXFeatureIterator( this, false, request ) );
}
@@ -18,13 +18,34 @@
#include "qgsfeatureiterator.h"

#include "gpsdata.h"
#include "qgsgpxprovider.h"

class QgsGPXProvider;

class QgsGPXFeatureIterator : public QgsAbstractFeatureIterator

class QgsGPXFeatureSource : public QgsAbstractFeatureSource
{
public:
QgsGPXFeatureSource( const QgsGPXProvider* p );
~QgsGPXFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request );

protected:
QString mFileName;
QgsGPXProvider::DataType mFeatureType;
QgsGPSData* data;
QVector<int> indexToAttr;
QgsFields mFields;

friend class QgsGPXFeatureIterator;
};


class QgsGPXFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsGPXFeatureSource>
{
public:
QgsGPXFeatureIterator( QgsGPXProvider* p, const QgsFeatureRequest& request );
QgsGPXFeatureIterator( QgsGPXFeatureSource* source, bool ownSource, const QgsFeatureRequest& request );

~QgsGPXFeatureIterator();

@@ -54,7 +75,6 @@ class QgsGPXFeatureIterator : public QgsAbstractFeatureIterator
void readAttributes( QgsFeature& feature, const QgsTrack& trk );

protected:
QgsGPXProvider* P;

//! Current waypoint iterator
QgsGPSData::WaypointIterator mWptIter;
@@ -113,16 +113,14 @@ QgsGPXProvider::QgsGPXProvider( QString uri )

QgsGPXProvider::~QgsGPXProvider()
{
while ( !mActiveIterators.empty() )
{
QgsGPXFeatureIterator *it = *mActiveIterators.begin();
QgsDebugMsg( "closing active iterator" );
it->close();
}

QgsGPSData::releaseData( mFileName );
}

QgsAbstractFeatureSource* QgsGPXProvider::featureSource() const
{
return new QgsGPXFeatureSource( this );
}


QString QgsGPXProvider::storageType() const
{
@@ -190,7 +188,7 @@ bool QgsGPXProvider::isValid()

QgsFeatureIterator QgsGPXProvider::getFeatures( const QgsFeatureRequest& request )
{
return QgsFeatureIterator( new QgsGPXFeatureIterator( this, request ) );
return QgsFeatureIterator( new QgsGPXFeatureIterator( new QgsGPXFeatureSource( this ), true, request ) );
}


@@ -17,6 +17,8 @@
* *
***************************************************************************/

#ifndef QGSGPXPROVIDER_H
#define QGSGPXPROVIDER_H

#include "qgsvectordataprovider.h"
#include "gpsdata.h"
@@ -47,6 +49,8 @@ class QgsGPXProvider : public QgsVectorDataProvider

/* Functions inherited from QgsVectorDataProvider */

virtual QgsAbstractFeatureSource* featureSource() const;

/**
* Returns the permanent storage type for this layer as a friendly name.
*/
@@ -127,8 +131,6 @@ class QgsGPXProvider : public QgsVectorDataProvider
bool addFeature( QgsFeature& f );


private:

enum DataType
{
WaypointType = 1,
@@ -144,6 +146,8 @@ class QgsGPXProvider : public QgsVectorDataProvider
CmtAttr, DscAttr, SrcAttr, URLAttr, URLNameAttr
};

private:

QgsGPSData* data;

//! Fields
@@ -172,6 +176,7 @@ class QgsGPXProvider : public QgsVectorDataProvider
};
wkbPoint mWKBpt;

friend class QgsGPXFeatureIterator;
QSet< QgsGPXFeatureIterator * > mActiveIterators;
friend class QgsGPXFeatureSource;
};

#endif

0 comments on commit 557cc6f

Please sign in to comment.
You can’t perform that action at this time.