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 & )
68 addParameter(
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 ) );
75QIcon QgsConvertGpxFeatureTypeAlgorithm::icon()
const
80QString QgsConvertGpxFeatureTypeAlgorithm::svgIconPath()
const
85QString QgsConvertGpxFeatureTypeAlgorithm::shortHelpString()
const
87 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)." );
90QString QgsConvertGpxFeatureTypeAlgorithm::shortDescription()
const
92 return QObject::tr(
"Converts GPX features from one type to another." );
95QgsConvertGpxFeatureTypeAlgorithm *QgsConvertGpxFeatureTypeAlgorithm::createInstance()
const
97 return new QgsConvertGpxFeatureTypeAlgorithm();
103 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
104 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
106 const ConversionType convertType =
static_cast<ConversionType
>( parameterAsEnum( parameters, u
"CONVERSION"_s, context ) );
109 if ( babelPath.isEmpty() )
110 babelPath = u
"gpsbabel"_s;
112 QStringList processArgs;
114 createArgumentLists( inputPath, outputPath, convertType, processArgs, logArgs );
115 feedback->
pushCommandInfo( QObject::tr(
"Conversion command: " ) + babelPath +
' ' + logArgs.join(
' ' ) );
117 QgsBlockingProcess babelProcess( babelPath, processArgs );
118 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) {
121 babelProcess.setStdOutHandler( [feedback](
const QByteArray &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 auto formatParam = std::make_unique<QgsProcessingParameterString>( u
"FORMAT"_s, QObject::tr(
"Format" ) );
263 for (
const QString &format : formatNames )
266 std::sort( formats.begin(), formats.end(), [](
const QString &a,
const QString &b ) {
267 return a.compare( b, Qt::CaseInsensitive ) < 0;
270 formatParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, formats } } ) }
272 addParameter( formatParam.release() );
274 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
281QIcon QgsConvertGpsDataAlgorithm::icon()
const
286QString QgsConvertGpsDataAlgorithm::svgIconPath()
const
291QString QgsConvertGpsDataAlgorithm::shortHelpString()
const
293 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." );
296QString QgsConvertGpsDataAlgorithm::shortDescription()
const
298 return QObject::tr(
"Converts a GPS data file from a range of formats to the GPX standard format." );
301QgsConvertGpsDataAlgorithm *QgsConvertGpsDataAlgorithm::createInstance()
const
303 return new QgsConvertGpsDataAlgorithm();
308 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
309 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
314 if ( babelPath.isEmpty() )
315 babelPath = u
"gpsbabel"_s;
317 const QString formatName = parameterAsString( parameters, u
"FORMAT"_s, context );
328 switch ( featureType )
333 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting waypoints." )
334 .arg( formatName ) );
341 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting routes." )
342 .arg( formatName ) );
349 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting tracks." )
350 .arg( formatName ) );
358 const QStringList processCommand = format->
importCommand( babelPath, featureType, inputPath, outputPath );
359 feedback->
pushCommandInfo( QObject::tr(
"Conversion command: " ) + logCommand.join(
' ' ) );
361 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
362 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) {
365 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) {
369 const int res = babelProcess.run( feedback );
372 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
374 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
380 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
382 else if ( babelProcess.processError() == QProcess::FailedToStart )
384 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 ) );
391 std::unique_ptr<QgsVectorLayer> layer;
394 switch ( featureType )
397 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=waypoint", layerName, u
"gpx"_s );
400 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=route", layerName, u
"gpx"_s );
403 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=track", layerName, u
"gpx"_s );
408 if ( !layer->isValid() )
410 feedback->
reportError( QObject::tr(
"Resulting file is not a valid GPX layer" ) );
414 const QString layerId = layer->id();
415 outputs.insert( u
"OUTPUT_LAYER"_s, layerId );
421 outputs.insert( u
"OUTPUT"_s, outputPath );
429QString QgsDownloadGpsDataAlgorithm::name()
const
431 return u
"downloadgpsdata"_s;
434QString QgsDownloadGpsDataAlgorithm::displayName()
const
436 return QObject::tr(
"Download GPS data from device" );
439QStringList QgsDownloadGpsDataAlgorithm::tags()
const
441 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes,gpx,import,export,export,device,serial" ).split(
',' );
444QString QgsDownloadGpsDataAlgorithm::group()
const
446 return QObject::tr(
"GPS" );
449QString QgsDownloadGpsDataAlgorithm::groupId()
const
454void QgsDownloadGpsDataAlgorithm::initAlgorithm(
const QVariantMap & )
456 auto deviceParam = std::make_unique<QgsProcessingParameterString>( u
"DEVICE"_s, QObject::tr(
"Device" ) );
459 std::sort( deviceNames.begin(), deviceNames.end(), [](
const QString &a,
const QString &b ) {
460 return a.compare( b, Qt::CaseInsensitive ) < 0;
463 deviceParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, deviceNames } } ) }
465 addParameter( deviceParam.release() );
469 auto portParam = std::make_unique<QgsProcessingParameterString>( u
"PORT"_s, QObject::tr(
"Port" ) );
472 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
474 std::sort( ports.begin(), ports.end(), [](
const QString &a,
const QString &b ) {
475 return a.compare( b, Qt::CaseInsensitive ) < 0;
478 portParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, ports } } ) }
480 addParameter( portParam.release() );
482 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
489QIcon QgsDownloadGpsDataAlgorithm::icon()
const
494QString QgsDownloadGpsDataAlgorithm::svgIconPath()
const
499QString QgsDownloadGpsDataAlgorithm::shortHelpString()
const
501 return QObject::tr(
"This algorithm uses the GPSBabel tool to download data from a GPS device into the GPX standard format." );
504QString QgsDownloadGpsDataAlgorithm::shortDescription()
const
506 return QObject::tr(
"Downloads data from a GPS device into the GPX standard format." );
509QgsDownloadGpsDataAlgorithm *QgsDownloadGpsDataAlgorithm::createInstance()
const
511 return new QgsDownloadGpsDataAlgorithm();
516 const QString outputPath = parameterAsString( parameters, u
"OUTPUT"_s, context );
520 if ( babelPath.isEmpty() )
521 babelPath = u
"gpsbabel"_s;
523 const QString deviceName = parameterAsString( parameters, u
"DEVICE"_s, context );
531 const QString portName = parameterAsString( parameters, u
"PORT"_s, context );
534 QStringList validPorts;
535 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
537 if ( it->first.compare( portName, Qt::CaseInsensitive ) == 0 || it->second.compare( portName, Qt::CaseInsensitive ) == 0 )
539 inputPort = it->first;
541 validPorts << it->first;
543 if ( inputPort.isEmpty() )
546 .arg( portName, validPorts.join(
", "_L1 ) ) );
549 switch ( featureType )
554 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting waypoints." )
555 .arg( deviceName ) );
562 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting routes." )
563 .arg( deviceName ) );
570 throw QgsProcessingException( QObject::tr(
"The GPSBabel format “%1” does not support converting tracks." )
571 .arg( deviceName ) );
579 const QStringList processCommand = format->
importCommand( babelPath, featureType, inputPort, outputPath );
580 feedback->
pushCommandInfo( QObject::tr(
"Download command: " ) + logCommand.join(
' ' ) );
582 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
583 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) {
586 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) {
590 const int res = babelProcess.run( feedback );
593 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
595 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
601 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
603 else if ( babelProcess.processError() == QProcess::FailedToStart )
605 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 ) );
612 std::unique_ptr<QgsVectorLayer> layer;
615 switch ( featureType )
618 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=waypoint", layerName, u
"gpx"_s );
621 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=route", layerName, u
"gpx"_s );
624 layer = std::make_unique<QgsVectorLayer>( outputPath +
"?type=track", layerName, u
"gpx"_s );
629 if ( !layer->isValid() )
631 feedback->
reportError( QObject::tr(
"Resulting file is not a valid GPX layer" ) );
635 const QString layerId = layer->id();
636 outputs.insert( u
"OUTPUT_LAYER"_s, layerId );
642 outputs.insert( u
"OUTPUT"_s, outputPath );
651QString QgsUploadGpsDataAlgorithm::name()
const
653 return u
"uploadgpsdata"_s;
656QString QgsUploadGpsDataAlgorithm::displayName()
const
658 return QObject::tr(
"Upload GPS data to device" );
661QStringList QgsUploadGpsDataAlgorithm::tags()
const
663 return QObject::tr(
"gps,tools,babel,tracks,waypoints,routes,gpx,import,export,export,device,serial" ).split(
',' );
666QString QgsUploadGpsDataAlgorithm::group()
const
668 return QObject::tr(
"GPS" );
671QString QgsUploadGpsDataAlgorithm::groupId()
const
676void QgsUploadGpsDataAlgorithm::initAlgorithm(
const QVariantMap & )
680 auto deviceParam = std::make_unique<QgsProcessingParameterString>( u
"DEVICE"_s, QObject::tr(
"Device" ) );
683 std::sort( deviceNames.begin(), deviceNames.end(), [](
const QString &a,
const QString &b ) {
684 return a.compare( b, Qt::CaseInsensitive ) < 0;
687 deviceParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, deviceNames } } ) }
689 addParameter( deviceParam.release() );
692 auto portParam = std::make_unique<QgsProcessingParameterString>( u
"PORT"_s, QObject::tr(
"Port" ) );
695 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
697 std::sort( ports.begin(), ports.end(), [](
const QString &a,
const QString &b ) {
698 return a.compare( b, Qt::CaseInsensitive ) < 0;
701 portParam->setMetadata( { { u
"widget_wrapper"_s, QVariantMap( { { u
"value_hints"_s, ports } } ) }
703 addParameter( portParam.release() );
705 addParameter(
new QgsProcessingParameterEnum( u
"FEATURE_TYPE"_s, QObject::tr(
"Feature type" ), { QObject::tr(
"Waypoints" ), QObject::tr(
"Routes" ), QObject::tr(
"Tracks" ) },
false, 0 ) );
708QIcon QgsUploadGpsDataAlgorithm::icon()
const
713QString QgsUploadGpsDataAlgorithm::svgIconPath()
const
718QString QgsUploadGpsDataAlgorithm::shortHelpString()
const
720 return QObject::tr(
"This algorithm uses the GPSBabel tool to upload data to a GPS device from the GPX standard format." );
723QString QgsUploadGpsDataAlgorithm::shortDescription()
const
725 return QObject::tr(
"Uploads data to a GPS device from the GPX standard format." );
728QgsUploadGpsDataAlgorithm *QgsUploadGpsDataAlgorithm::createInstance()
const
730 return new QgsUploadGpsDataAlgorithm();
735 const QString inputPath = parameterAsString( parameters, u
"INPUT"_s, context );
739 if ( babelPath.isEmpty() )
740 babelPath = u
"gpsbabel"_s;
742 const QString deviceName = parameterAsString( parameters, u
"DEVICE"_s, context );
750 const QString portName = parameterAsString( parameters, u
"PORT"_s, context );
753 QStringList validPorts;
754 for (
auto it = devices.constBegin(); it != devices.constEnd(); ++it )
756 if ( it->first.compare( portName, Qt::CaseInsensitive ) == 0 || it->second.compare( portName, Qt::CaseInsensitive ) == 0 )
758 outputPort = it->first;
760 validPorts << it->first;
762 if ( outputPort.isEmpty() )
765 .arg( portName, validPorts.join(
", "_L1 ) ) );
769 switch ( featureType )
775 .arg( deviceName ) );
783 .arg( deviceName ) );
791 .arg( deviceName ) );
799 const QStringList processCommand = format->
exportCommand( babelPath, featureType, inputPath, outputPort );
800 feedback->
pushCommandInfo( QObject::tr(
"Upload command: " ) + logCommand.join(
' ' ) );
802 QgsBlockingProcess babelProcess( processCommand.value( 0 ), processCommand.mid( 1 ) );
803 babelProcess.setStdErrHandler( [feedback](
const QByteArray &ba ) {
806 babelProcess.setStdOutHandler( [feedback](
const QByteArray &ba ) {
810 const int res = babelProcess.run( feedback );
813 feedback->
pushInfo( QObject::tr(
"Process was canceled and did not complete" ) );
815 else if ( !feedback->
isCanceled() && babelProcess.exitStatus() == QProcess::CrashExit )
821 feedback->
pushInfo( QObject::tr(
"Process completed successfully" ) );
823 else if ( babelProcess.processError() == QProcess::FailedToStart )
825 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.