21using namespace Qt::StringLiterals;
23#if QT_CONFIG( process )
39QString QgsConvertGpxFeatureTypeAlgorithm::name()
const
41 return u
"convertgpxfeaturetype"_s;
44QString QgsConvertGpxFeatureTypeAlgorithm::displayName()
const
46 return QObject::tr(
"Convert GPX feature type" );
49QStringList QgsConvertGpxFeatureTypeAlgorithm::tags()
const
51 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes" ).split(
',' );
54QString QgsConvertGpxFeatureTypeAlgorithm::group()
const
56 return QObject::tr(
"GPS" );
59QString QgsConvertGpxFeatureTypeAlgorithm::groupId()
const
64void QgsConvertGpxFeatureTypeAlgorithm::initAlgorithm(
const QVariantMap & )
71 new QgsProcessingParameterEnum( u
"CONVERSION"_s, QObject::tr(
"Conversion" ), { QObject::tr(
"Waypoints from a Route" ), QObject::tr(
"Waypoints from a Track" ), QObject::tr(
"Route from Waypoints" ), QObject::tr(
"Track from Waypoints" ) },
false, 0 )
79QIcon QgsConvertGpxFeatureTypeAlgorithm::icon()
const
84QString QgsConvertGpxFeatureTypeAlgorithm::svgIconPath()
const
89QString QgsConvertGpxFeatureTypeAlgorithm::shortHelpString()
const
91 return QObject::tr(
"This algorithm uses the GPSBabel tool to convert GPX features from one type to another (e.g. converting all waypoint features to a route feature)." );
94QString QgsConvertGpxFeatureTypeAlgorithm::shortDescription()
const
96 return QObject::tr(
"Converts GPX features from one type to another." );
99QgsConvertGpxFeatureTypeAlgorithm *QgsConvertGpxFeatureTypeAlgorithm::createInstance()
const
101 return new QgsConvertGpxFeatureTypeAlgorithm();
107 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
108 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
110 const ConversionType convertType =
static_cast<ConversionType
>( parameterAsEnum( parameters, u
"CONVERSION"_s, context ) );
113 if ( babelPath.isEmpty() )
114 babelPath = u
"gpsbabel"_s;
116 QStringList processArgs;
118 createArgumentLists( inputPath, outputPath, convertType, processArgs, logArgs );
119 feedback->
pushCommandInfo( QObject::tr(
"Conversion command: " ) + babelPath +
' ' + logArgs.join(
' ' ) );
121 QgsBlockingProcess babelProcess( babelPath, processArgs );
122 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) { feedback->
reportError( ba ); } );
123 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) { feedback->
pushDebugInfo( ba ); } );
125 const int res = babelProcess.run( feedback );
128 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
130 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
136 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
138 else if ( babelProcess.processError() == QProcess::FailedToStart )
140 throw QgsProcessingException( QObject::tr(
"Process %1 failed to start. Either %1 is missing, or you may have insufficient permissions to run the program." ).arg( babelPath ) );
147 std::unique_ptr<QgsVectorLayer> layer;
150 switch ( convertType )
152 case QgsConvertGpxFeatureTypeAlgorithm::WaypointsFromRoute:
153 case QgsConvertGpxFeatureTypeAlgorithm::WaypointsFromTrack:
154 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=waypoint", layerName, u
"gpx"_s );
156 case QgsConvertGpxFeatureTypeAlgorithm::RouteFromWaypoints:
157 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=route", layerName, u
"gpx"_s );
159 case QgsConvertGpxFeatureTypeAlgorithm::TrackFromWaypoints:
160 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=track", layerName, u
"gpx"_s );
165 if ( !layer->isValid() )
167 feedback->
reportError( QObject::tr(
"Resulting file is not a valid GPX layer" ) );
171 const QString layerId = layer->id();
172 outputs.insert( u
"OUTPUT_LAYER"_s, layerId );
178 outputs.insert( u
"OUTPUT"_s, outputPath );
182void QgsConvertGpxFeatureTypeAlgorithm::createArgumentLists(
const QString &inputPath,
const QString &outputPath, ConversionType conversion, QStringList &processArgs, QStringList &logArgs )
184 logArgs.reserve( 10 );
185 processArgs.reserve( 10 );
186 for (
const QString &arg : { u
"-i"_s, u
"gpx"_s, u
"-f"_s } )
193 logArgs << u
"\"%1\""_s.arg( inputPath );
194 processArgs << inputPath;
196 QStringList convertStrings;
197 switch ( conversion )
199 case QgsConvertGpxFeatureTypeAlgorithm::WaypointsFromRoute:
200 convertStrings << u
"-x"_s << u
"transform,wpt=rte,del"_s;
202 case QgsConvertGpxFeatureTypeAlgorithm::WaypointsFromTrack:
203 convertStrings << u
"-x"_s << u
"transform,wpt=trk,del"_s;
205 case QgsConvertGpxFeatureTypeAlgorithm::RouteFromWaypoints:
206 convertStrings << u
"-x"_s << u
"transform,rte=wpt,del"_s;
208 case QgsConvertGpxFeatureTypeAlgorithm::TrackFromWaypoints:
209 convertStrings << u
"-x"_s << u
"transform,trk=wpt,del"_s;
212 logArgs << convertStrings;
213 processArgs << convertStrings;
215 for (
const QString &arg : { u
"-o"_s, u
"gpx"_s, u
"-F"_s } )
221 logArgs << u
"\"%1\""_s.arg( outputPath );
222 processArgs << outputPath;
230QString QgsConvertGpsDataAlgorithm::name()
const
232 return u
"convertgpsdata"_s;
235QString QgsConvertGpsDataAlgorithm::displayName()
const
237 return QObject::tr(
"Convert GPS data" );
240QStringList QgsConvertGpsDataAlgorithm::tags()
const
242 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes,gpx,import,export" ).split(
',' );
245QString QgsConvertGpsDataAlgorithm::group()
const
247 return QObject::tr(
"GPS" );
250QString QgsConvertGpsDataAlgorithm::groupId()
const
255void QgsConvertGpsDataAlgorithm::initAlgorithm(
const QVariantMap & )
259 QObject::tr(
"Input file" ),
267 auto formatParam = std::make_unique<QgsProcessingParameterString>( u
"FORMAT"_s, QObject::tr(
"Format" ) );
271 for (
const QString &format : formatNames )
274 std::sort( formats.begin(), formats.end(), [](
const QString &a,
const QString &b ) { return a.compare( b, Qt::CaseInsensitive ) < 0; } );
276 formatParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, formats } } ) } } );
277 addParameter( formatParam.release() );
279 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
286QIcon QgsConvertGpsDataAlgorithm::icon()
const
291QString QgsConvertGpsDataAlgorithm::svgIconPath()
const
296QString QgsConvertGpsDataAlgorithm::shortHelpString()
const
298 return QObject::tr(
"This algorithm uses the GPSBabel tool to convert a GPS data file from a range of formats to the GPX standard format." );
301QString QgsConvertGpsDataAlgorithm::shortDescription()
const
303 return QObject::tr(
"Converts a GPS data file from a range of formats to the GPX standard format." );
306QgsConvertGpsDataAlgorithm *QgsConvertGpsDataAlgorithm::createInstance()
const
308 return new QgsConvertGpsDataAlgorithm();
313 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
314 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
319 if ( babelPath.isEmpty() )
320 babelPath = u
"gpsbabel"_s;
322 const QString formatName = parameterAsString( parameters, u
"FORMAT"_s, context );
332 switch ( featureType )
337 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting waypoints." ).arg( formatName ) );
344 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting routes." ).arg( formatName ) );
351 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting tracks." ).arg( formatName ) );
359 const QStringList processCommand = format->
importCommand( babelPath, featureType, inputPath, outputPath );
360 feedback->
pushCommandInfo( QObject::tr(
"Conversion command: " ) + logCommand.join(
' ' ) );
362 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
363 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) { feedback->
reportError( ba ); } );
364 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) { feedback->
pushDebugInfo( ba ); } );
366 const int res = babelProcess.run( feedback );
369 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
371 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
377 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
379 else if ( babelProcess.processError() == QProcess::FailedToStart )
381 throw QgsProcessingException( QObject::tr(
"Process %1 failed to start. Either %1 is missing, or you may have insufficient permissions to run the program." ).arg( babelPath ) );
388 std::unique_ptr<QgsVectorLayer> layer;
391 switch ( featureType )
394 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=waypoint", layerName, u
"gpx"_s );
397 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=route", layerName, u
"gpx"_s );
400 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=track", layerName, u
"gpx"_s );
405 if ( !layer->isValid() )
407 feedback->
reportError( QObject::tr(
"Resulting file is not a valid GPX layer" ) );
411 const QString layerId = layer->id();
412 outputs.insert( u
"OUTPUT_LAYER"_s, layerId );
418 outputs.insert( u
"OUTPUT"_s, outputPath );
426QString QgsDownloadGpsDataAlgorithm::name()
const
428 return u
"downloadgpsdata"_s;
431QString QgsDownloadGpsDataAlgorithm::displayName()
const
433 return QObject::tr(
"Download GPS data from device" );
436QStringList QgsDownloadGpsDataAlgorithm::tags()
const
438 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes,gpx,import,export,export,device,serial" ).split(
',' );
441QString QgsDownloadGpsDataAlgorithm::group()
const
443 return QObject::tr(
"GPS" );
446QString QgsDownloadGpsDataAlgorithm::groupId()
const
451void QgsDownloadGpsDataAlgorithm::initAlgorithm(
const QVariantMap & )
453 auto deviceParam = std::make_unique<QgsProcessingParameterString>( u
"DEVICE"_s, QObject::tr(
"Device" ) );
456 std::sort( deviceNames.begin(), deviceNames.end(), [](
const QString &a,
const QString &b ) { return a.compare( b, Qt::CaseInsensitive ) < 0; } );
458 deviceParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, deviceNames } } ) } } );
459 addParameter( deviceParam.release() );
463 auto portParam = std::make_unique<QgsProcessingParameterString>( u
"PORT"_s, QObject::tr(
"Port" ) );
466 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
468 std::sort( ports.begin(), ports.end(), [](
const QString &a,
const QString &b ) { return a.compare( b, Qt::CaseInsensitive ) < 0; } );
470 portParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, ports } } ) } } );
471 addParameter( portParam.release() );
473 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
480QIcon QgsDownloadGpsDataAlgorithm::icon()
const
485QString QgsDownloadGpsDataAlgorithm::svgIconPath()
const
490QString QgsDownloadGpsDataAlgorithm::shortHelpString()
const
492 return QObject::tr(
"This algorithm uses the GPSBabel tool to download data from a GPS device into the GPX standard format." );
495QString QgsDownloadGpsDataAlgorithm::shortDescription()
const
497 return QObject::tr(
"Downloads data from a GPS device into the GPX standard format." );
500QgsDownloadGpsDataAlgorithm *QgsDownloadGpsDataAlgorithm::createInstance()
const
502 return new QgsDownloadGpsDataAlgorithm();
507 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
511 if ( babelPath.isEmpty() )
512 babelPath = u
"gpsbabel"_s;
514 const QString deviceName = parameterAsString( parameters, u
"DEVICE"_s, context );
521 const QString portName = parameterAsString( parameters, u
"PORT"_s, context );
524 QStringList validPorts;
525 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
527 if ( it->first.compare( portName, Qt::CaseInsensitive ) == 0 || it->second.compare( portName, Qt::CaseInsensitive ) == 0 )
529 inputPort = it->first;
531 validPorts << it->first;
533 if ( inputPort.isEmpty() )
535 throw QgsProcessingException( QObject::tr(
"Unknown port “%1”. Valid ports are: %2" ).arg( portName, validPorts.join(
", "_L1 ) ) );
538 switch ( featureType )
543 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting waypoints." ).arg( deviceName ) );
550 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting routes." ).arg( deviceName ) );
557 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting tracks." ).arg( deviceName ) );
565 const QStringList processCommand = format->
importCommand( babelPath, featureType, inputPort, outputPath );
566 feedback->
pushCommandInfo( QObject::tr(
"Download command: " ) + logCommand.join(
' ' ) );
568 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
569 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) { feedback->
reportError( ba ); } );
570 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) { feedback->
pushDebugInfo( ba ); } );
572 const int res = babelProcess.run( feedback );
575 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
577 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
583 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
585 else if ( babelProcess.processError() == QProcess::FailedToStart )
587 throw QgsProcessingException( QObject::tr(
"Process %1 failed to start. Either %1 is missing, or you may have insufficient permissions to run the program." ).arg( babelPath ) );
594 std::unique_ptr<QgsVectorLayer> layer;
597 switch ( featureType )
600 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=waypoint", layerName, u
"gpx"_s );
603 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=route", layerName, u
"gpx"_s );
606 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=track", layerName, u
"gpx"_s );
611 if ( !layer->isValid() )
613 feedback->
reportError( QObject::tr(
"Resulting file is not a valid GPX layer" ) );
617 const QString layerId = layer->id();
618 outputs.insert( u
"OUTPUT_LAYER"_s, layerId );
624 outputs.insert( u
"OUTPUT"_s, outputPath );
633QString QgsUploadGpsDataAlgorithm::name()
const
635 return u
"uploadgpsdata"_s;
638QString QgsUploadGpsDataAlgorithm::displayName()
const
640 return QObject::tr(
"Upload GPS data to device" );
643QStringList QgsUploadGpsDataAlgorithm::tags()
const
645 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes,gpx,import,export,export,device,serial" ).split(
',' );
648QString QgsUploadGpsDataAlgorithm::group()
const
650 return QObject::tr(
"GPS" );
653QString QgsUploadGpsDataAlgorithm::groupId()
const
658void QgsUploadGpsDataAlgorithm::initAlgorithm(
const QVariantMap & )
664 auto deviceParam = std::make_unique<QgsProcessingParameterString>( u
"DEVICE"_s, QObject::tr(
"Device" ) );
667 std::sort( deviceNames.begin(), deviceNames.end(), [](
const QString &a,
const QString &b ) { return a.compare( b, Qt::CaseInsensitive ) < 0; } );
669 deviceParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, deviceNames } } ) } } );
670 addParameter( deviceParam.release() );
673 auto portParam = std::make_unique<QgsProcessingParameterString>( u
"PORT"_s, QObject::tr(
"Port" ) );
676 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
678 std::sort( ports.begin(), ports.end(), [](
const QString &a,
const QString &b ) { return a.compare( b, Qt::CaseInsensitive ) < 0; } );
680 portParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, ports } } ) } } );
681 addParameter( portParam.release() );
683 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
686QIcon QgsUploadGpsDataAlgorithm::icon()
const
691QString QgsUploadGpsDataAlgorithm::svgIconPath()
const
696QString QgsUploadGpsDataAlgorithm::shortHelpString()
const
698 return QObject::tr(
"This algorithm uses the GPSBabel tool to upload data to a GPS device from the GPX standard format." );
701QString QgsUploadGpsDataAlgorithm::shortDescription()
const
703 return QObject::tr(
"Uploads data to a GPS device from the GPX standard format." );
706QgsUploadGpsDataAlgorithm *QgsUploadGpsDataAlgorithm::createInstance()
const
708 return new QgsUploadGpsDataAlgorithm();
713 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
717 if ( babelPath.isEmpty() )
718 babelPath = u
"gpsbabel"_s;
720 const QString deviceName = parameterAsString( parameters, u
"DEVICE"_s, context );
727 const QString portName = parameterAsString( parameters, u
"PORT"_s, context );
730 QStringList validPorts;
731 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
733 if ( it->first.compare( portName, Qt::CaseInsensitive ) == 0 || it->second.compare( portName, Qt::CaseInsensitive ) == 0 )
735 outputPort = it->first;
737 validPorts << it->first;
739 if ( outputPort.isEmpty() )
741 throw QgsProcessingException( QObject::tr(
"Unknown port “%1”. Valid ports are: %2" ).arg( portName, validPorts.join(
", "_L1 ) ) );
745 switch ( featureType )
750 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support waypoints." ).arg( deviceName ) );
757 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support routes." ).arg( deviceName ) );
764 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support tracks." ).arg( deviceName ) );
772 const QStringList processCommand = format->
exportCommand( babelPath, featureType, inputPath, outputPort );
773 feedback->
pushCommandInfo( QObject::tr(
"Upload command: " ) + logCommand.join(
' ' ) );
775 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
776 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) { feedback->
reportError( ba ); } );
777 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) { feedback->
pushDebugInfo( ba ); } );
779 const int res = babelProcess.run( feedback );
782 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
784 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
790 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
792 else if ( babelProcess.processError() == QProcess::FailedToStart )
794 throw QgsProcessingException( QObject::tr(
"Process %1 failed to start. Either %1 is missing, or you may have insufficient permissions to run the program." ).arg( babelPath ) );
@ File
Parameter is a single file.
@ QuoteFilePaths
File paths should be enclosed in quotations and escaped.
GpsFeatureType
GPS feature types.
@ Tracks
Format supports tracks.
@ Waypoints
Format supports waypoints.
@ Routes
Format supports routes.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsBabelFormatRegistry * gpsBabelFormatRegistry()
Returns the application's GPSBabel format registry, used for managing GPSBabel formats.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
bool isCanceled() const
Tells whether the operation has been canceled already.
static QList< QPair< QString, QString > > availablePorts()
QgsMapLayer * addMapLayer(QgsMapLayer *layer, bool takeOwnership=true)
Add a layer to the store.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A vector layer output for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
@ Vector
Vector layer type.
static QString suggestLayerNameFromFilePath(const QString &path)
Suggests a suitable layer name given only a file path.
static const QgsSettingsEntryString * settingsGpsBabelPath
Settings entry path to GPSBabel executable.