Skip to content

Commit 52ea60a

Browse files
committedJul 31, 2021
Implement encode/decodeUri for Gpx provider
1 parent 963d322 commit 52ea60a

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed
 

‎src/providers/gpx/qgsgpxprovider.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,21 @@ QgsGPXProvider::QgsGPXProvider( const QString &uri, const ProviderOptions &optio
7373
// we always use UTF-8
7474
setEncoding( QStringLiteral( "utf8" ) );
7575

76-
// get the file name and the type parameter from the URI
77-
int fileNameEnd = uri.indexOf( '?' );
78-
if ( fileNameEnd == -1 || uri.mid( fileNameEnd + 1, 5 ) != QLatin1String( "type=" ) )
76+
const QVariantMap uriParts = decodeUri( uri );
77+
const QString typeStr = uriParts.value( QStringLiteral( "layerName" ) ).toString();
78+
if ( typeStr.isEmpty() )
7979
{
8080
QgsLogger::warning( tr( "Bad URI - you need to specify the feature type." ) );
8181
return;
8282
}
83-
QString typeStr = uri.mid( fileNameEnd + 6 );
84-
mFeatureType = ( typeStr == QLatin1String( "waypoint" ) ? WaypointType :
85-
( typeStr == QLatin1String( "route" ) ? RouteType : TrackType ) );
83+
if ( typeStr.compare( QLatin1String( "waypoint" ), Qt::CaseInsensitive ) == 0 )
84+
mFeatureType = WaypointType;
85+
else if ( typeStr.compare( QLatin1String( "route" ), Qt::CaseInsensitive ) == 0 )
86+
mFeatureType = RouteType;
87+
else
88+
mFeatureType = TrackType;
89+
90+
mFileName = uriParts.value( QStringLiteral( "path" ) ).toString();
8691

8792
// set up the attributes and the geometry type depending on the feature type
8893
for ( int i = 0; i < ATTR_COUNT; ++i )
@@ -95,8 +100,6 @@ QgsGPXProvider::QgsGPXProvider( const QString &uri, const ProviderOptions &optio
95100
}
96101
}
97102

98-
mFileName = uri.left( fileNameEnd );
99-
100103
// parse the file
101104
data = QgsGpsData::getData( mFileName );
102105
if ( !data )
@@ -105,7 +108,6 @@ QgsGPXProvider::QgsGPXProvider( const QString &uri, const ProviderOptions &optio
105108
mValid = true;
106109
}
107110

108-
109111
QgsGPXProvider::~QgsGPXProvider()
110112
{
111113
QgsGpsData::releaseData( mFileName );
@@ -116,7 +118,6 @@ QgsAbstractFeatureSource *QgsGPXProvider::featureSource() const
116118
return new QgsGPXFeatureSource( this );
117119
}
118120

119-
120121
QString QgsGPXProvider::storageType() const
121122
{
122123
return tr( "GPS eXchange file" );
@@ -171,13 +172,11 @@ bool QgsGPXProvider::isValid() const
171172
return mValid;
172173
}
173174

174-
175175
QgsFeatureIterator QgsGPXProvider::getFeatures( const QgsFeatureRequest &request ) const
176176
{
177177
return QgsFeatureIterator( new QgsGPXFeatureIterator( new QgsGPXFeatureSource( this ), true, request ) );
178178
}
179179

180-
181180
bool QgsGPXProvider::addFeatures( QgsFeatureList &flist, Flags flags )
182181
{
183182
if ( !data )
@@ -371,7 +370,6 @@ bool QgsGPXProvider::addFeature( QgsFeature &f, Flags )
371370
return success;
372371
}
373372

374-
375373
bool QgsGPXProvider::deleteFeatures( const QgsFeatureIds &id )
376374
{
377375
if ( !data )
@@ -393,7 +391,6 @@ bool QgsGPXProvider::deleteFeatures( const QgsFeatureIds &id )
393391
return true;
394392
}
395393

396-
397394
bool QgsGPXProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_map )
398395
{
399396
if ( !data )
@@ -446,7 +443,6 @@ bool QgsGPXProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
446443
return true;
447444
}
448445

449-
450446
void QgsGPXProvider::changeAttributeValues( QgsGpsObject &obj, const QgsAttributeMap &attrs )
451447
{
452448

@@ -512,37 +508,52 @@ void QgsGPXProvider::changeAttributeValues( QgsGpsObject &obj, const QgsAttribut
512508

513509
}
514510

515-
516511
QVariant QgsGPXProvider::defaultValue( int fieldId ) const
517512
{
518513
if ( fieldId == SrcAttr )
519514
return tr( "Digitized in QGIS" );
520515
return QVariant();
521516
}
522517

523-
524518
QString QgsGPXProvider::name() const
525519
{
526520
return GPX_KEY;
527-
} // QgsGPXProvider::name()
528-
529-
521+
}
530522

531523
QString QgsGPXProvider::description() const
532524
{
533525
return GPX_DESCRIPTION;
534-
} // QgsGPXProvider::description()
526+
}
535527

536528
QgsCoordinateReferenceSystem QgsGPXProvider::crs() const
537529
{
538530
return QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
539531
}
540532

541-
QgsDataProvider *QgsGpxProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
533+
QString QgsGPXProvider::encodeUri( const QVariantMap &parts )
542534
{
543-
return new QgsGPXProvider( uri, options, flags );
535+
if ( parts.value( QStringLiteral( "layerName" ) ).toString().isEmpty() )
536+
return parts.value( QStringLiteral( "path" ) ).toString();
537+
else
538+
return QStringLiteral( "%1?type=%2" ).arg( parts.value( QStringLiteral( "path" ) ).toString(),
539+
parts.value( QStringLiteral( "layerName" ) ).toString() );
544540
}
545541

542+
QVariantMap QgsGPXProvider::decodeUri( const QString &uri )
543+
{
544+
QVariantMap res;
545+
const int fileNameEnd = uri.indexOf( '?' );
546+
if ( fileNameEnd != -1 && uri.mid( fileNameEnd + 1, 5 ) == QLatin1String( "type=" ) )
547+
{
548+
res.insert( QStringLiteral( "layerName" ), uri.mid( fileNameEnd + 6 ) );
549+
res.insert( QStringLiteral( "path" ), uri.left( fileNameEnd ) );
550+
}
551+
else
552+
{
553+
res.insert( QStringLiteral( "path" ), uri );
554+
}
555+
return res;
556+
}
546557

547558
QgsGpxProviderMetadata::QgsGpxProviderMetadata():
548559
QgsProviderMetadata( GPX_KEY, GPX_DESCRIPTION )
@@ -553,3 +564,23 @@ QGISEXTERN QgsProviderMetadata *providerMetadataFactory()
553564
{
554565
return new QgsGpxProviderMetadata();
555566
}
567+
568+
QgsDataProvider *QgsGpxProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
569+
{
570+
return new QgsGPXProvider( uri, options, flags );
571+
}
572+
573+
QgsProviderMetadata::ProviderCapabilities QgsGpxProviderMetadata::providerCapabilities() const
574+
{
575+
return QgsProviderMetadata::ProviderCapability::FileBasedUris;
576+
}
577+
578+
QString QgsGpxProviderMetadata::encodeUri( const QVariantMap &parts ) const
579+
{
580+
return QgsGPXProvider::encodeUri( parts );
581+
}
582+
583+
QVariantMap QgsGpxProviderMetadata::decodeUri( const QString &uri ) const
584+
{
585+
return QgsGPXProvider::decodeUri( uri );
586+
}

‎src/providers/gpx/qgsgpxprovider.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class QgsGPXProvider final: public QgsVectorDataProvider
7777

7878
bool addFeature( QgsFeature &f, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
7979

80+
static QString encodeUri( const QVariantMap &parts );
81+
static QVariantMap decodeUri( const QString &uri );
8082

8183
enum DataType
8284
{
@@ -121,6 +123,9 @@ class QgsGpxProviderMetadata final: public QgsProviderMetadata
121123
public:
122124
QgsGpxProviderMetadata();
123125
QgsDataProvider *createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() ) override;
126+
QgsProviderMetadata::ProviderCapabilities providerCapabilities() const override;
127+
QString encodeUri( const QVariantMap &parts ) const override;
128+
QVariantMap decodeUri( const QString &uri ) const override;
124129
};
125130

126131
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.