Skip to content
Permalink
Browse files
Extended GPS plugin with a tab for performing conversions of GPX file…
…s, with the help of gpsbabel. Requested in #648.

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7777 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
homann committed Dec 13, 2007
1 parent d61115d commit 8eb726b9c047a1a1729bcf4bb9014cc45e0c756c
@@ -27,7 +27,7 @@
#include "qgsdataprovider.h"
#include "qgsvectordataprovider.h"
#include "qgsgpsplugin.h"

#include "qgslogger.h"

#include <QFileDialog>
#include <QMessageBox>
@@ -137,6 +137,10 @@ void QgsGPSPlugin::run()
bool, bool, QString, QString)),
this, SLOT(importGPSFile(QString, QgsBabelFormat*, bool, bool,
bool, QString, QString)));
connect(myPluginGui, SIGNAL(convertGPSFile(QString, int,
QString, QString)),
this, SLOT(convertGPSFile(QString, int,
QString, QString)));
connect(myPluginGui, SIGNAL(downloadFromGPS(QString, QString, bool, bool,
bool, QString, QString)),
this, SLOT(downloadFromGPS(QString, QString, bool, bool, bool,
@@ -286,6 +290,80 @@ void QgsGPSPlugin::importGPSFile(QString inputFilename, QgsBabelFormat* importer
}


void QgsGPSPlugin::convertGPSFile(QString inputFilename,
int convertType,
QString outputFilename,
QString layerName) {

// what features does the user want to import?

QStringList convertStrings;

switch ( convertType )
{
case 0:
convertStrings << "-x" << "transform,wpt=rte,del"; break;
case 1:
convertStrings << "-x" << "transform,rte=wpt,del"; break;
default:
QgsDebugMsg("Illegal conversion index!");
return;
}

// try to start the gpsbabel process
QStringList babelArgs;
babelArgs << mBabelPath << "-i"<<"gpx"<<"-f"<< inputFilename
<< convertStrings <<"-o"<<"gpx"<<"-F"<< outputFilename;
QgsDebugMsg(QString("Conversion command: ") + babelArgs.join("_"));

Q3Process babelProcess(babelArgs);
if (!babelProcess.start()) {
QMessageBox::warning(NULL, tr("Could not start process"),
tr("Could not start GPSBabel!"));
return;
}

// wait for gpsbabel to finish (or the user to cancel)
Q3ProgressDialog progressDialog(tr("Importing data..."), tr("Cancel"), 0,
NULL, 0, true);
progressDialog.show();
for (int i = 0; babelProcess.isRunning(); ++i) {
QCoreApplication::processEvents();

progressDialog.setProgress(i/64);
if (progressDialog.wasCanceled())
return;
}

// did we get any data?
if (babelProcess.exitStatus() != 0) {
QString babelError(babelProcess.readStderr());
QString errorMsg(tr("Could not convert data from %1!\n\n")
.arg(inputFilename));
errorMsg += babelError;
QMessageBox::warning(NULL, tr("Error converting data"), errorMsg);
return;
}

// add the layer
switch ( convertType )
{
case 0:
emit drawVectorLayer(outputFilename + "?type=waypoint",
layerName, "gpx");
break;
case 1:
emit drawVectorLayer(outputFilename + "?type=route",
layerName, "gpx");
break;
default:
QgsDebugMsg("Illegal conversion index!");
return;
}

emit closeGui();
}

void QgsGPSPlugin::downloadFromGPS(QString device, QString port,
bool downloadWaypoints, bool downloadRoutes,
bool downloadTracks, QString outputFilename,
@@ -63,6 +63,10 @@ public slots:
bool importWaypoints, bool importRoutes,
bool importTracks, QString outputFilename,
QString layerName);
void convertGPSFile(QString inputFilename,
int convertType,
QString outputFilename,
QString layerName);
void downloadFromGPS(QString device, QString port,
bool downloadWaypoints, bool downloadRoutes,
bool downloadTracks, QString outputFilename,
@@ -37,6 +37,7 @@ QgsGPSPluginGui::QgsGPSPluginGui(const BabelMap& importers,
populatePortComboBoxes();
populateULLayerComboBox();
populateIMPBabelFormats();
populateCONVDialog();

connect(pbULEditDevices, SIGNAL(clicked()), this, SLOT(openDeviceEditor()));
connect(pbDLEditDevices, SIGNAL(clicked()), this, SLOT(openDeviceEditor()));
@@ -53,6 +54,12 @@ QgsGPSPluginGui::QgsGPSPluginGui(const BabelMap& importers,
this, SLOT(enableRelevantControls()));
connect(leIMPLayer, SIGNAL(textChanged(const QString&)),
this, SLOT(enableRelevantControls()));
connect(leCONVInput, SIGNAL(textChanged(const QString&)),
this, SLOT(enableRelevantControls()));
connect(leCONVOutput, SIGNAL(textChanged(const QString&)),
this, SLOT(enableRelevantControls()));
connect(leCONVLayer, SIGNAL(textChanged(const QString&)),
this, SLOT(enableRelevantControls()));
connect(leDLOutput, SIGNAL(textChanged(const QString&)),
this, SLOT(enableRelevantControls()));
connect(leDLBasename, SIGNAL(textChanged(const QString&)),
@@ -74,38 +81,14 @@ void QgsGPSPluginGui::on_buttonBox_accepted()
{

// what should we do?
switch (tabWidget->currentPageIndex()) {
switch (tabWidget->currentPageIndex())
{
// add a GPX layer?
case 0:
emit loadGPXFile(leGPXFile->text(), cbGPXWaypoints->isChecked(),
cbGPXRoutes->isChecked(), cbGPXTracks->isChecked());
break;

// or import a download file?
/*
case 666:
//check input file exists
//
if (!QFile::exists ( leInputFile->text() ))
{
QMessageBox::warning( this, "GPS Importer",
"Unable to find the input file.\n"
"Please reselect a valid file." );
return;
}
WayPointToShape * myWayPointToShape = new WayPointToShape(leOutputShapeFile->text(),leInputFile->text());
//
// If you have a produced a raster layer using your plugin, you can ask qgis to
// add it to the view using:
// emit drawRasterLayer(QString("layername"));
// or for a vector layer
// emit drawVectorLayer(QString("pathname"),QString("layername"),QString("provider name (either ogr or postgres"));
//
delete myWayPointToShape;
emit drawVectorLayer(leOutputShapeFile->text(),QString("Waypoints"),QString("ogr"));
break;
*/

// or import other file?
case 1: {
const QString& typeString(cmbDLFeatureType->currentText());
@@ -116,7 +99,6 @@ void QgsGPSPluginGui::on_buttonBox_accepted()
leIMPLayer->text());
break;
}

// or download GPS data from a device?
case 2: {
int featureType = cmbDLFeatureType->currentItem();
@@ -125,13 +107,22 @@ void QgsGPSPluginGui::on_buttonBox_accepted()
leDLOutput->text(), leDLBasename->text());
break;
}

// or upload GPS data to a device?
case 3:
case 3: {
emit uploadToGPS(mGPXLayers[cmbULLayer->currentItem()],
cmbULDevice->currentText(), cmbULPort->currentText());
break;
}
// or convert between waypoints/tracks=
case 4: {
int convertType = cmbCONVType->currentItem();
emit convertGPSFile(leCONVInput->text(),
convertType,
leCONVOutput->text(),
leCONVLayer->text());
break;
}
}
accept();
}

@@ -200,6 +191,16 @@ void QgsGPSPluginGui::enableRelevantControls()
else
pbnOK->setEnabled(true);
}

// convert between waypoint/routes
else if (tabWidget->currentPageIndex() == 4) {

if ((leCONVInput->text() == "") || (leCONVOutput->text() == "") ||
(leCONVLayer->text() == ""))
pbnOK->setEnabled(false);
else
pbnOK->setEnabled(true);
}
}


@@ -400,6 +401,52 @@ void QgsGPSPluginGui::populateIMPBabelFormats() {
cmbDLDevice->setCurrentItem(d);
}

void QgsGPSPluginGui::populateCONVDialog() {
cmbCONVType->insertItem(tr("Route -> Waypoint"));
cmbCONVType->insertItem(tr("Waypoint -> Route"));
QString format = QString("<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\"> p, li { white-space: pre-wrap; } </style></head><body style=\" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Arial'; font-size:12pt;\"><span style=\" font-size:10pt;\">"\
"%1"\
"</span><a href=\"http://gpsbabel.sf.net\"><span style=\" font-size:10pt; text-decoration: underline; color:#0000ff;\">http://gpsbabel.sf.net</span></a><span style=\" font-size:10pt;\">"\
"%2"\
"</span></p><p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Arial'; font-size:10pt;\">"\
"%3"\
"</p></body></html>");
QString text = format
.arg(tr("QGIS can perform conversions of GPX files, by using GPSBabel ("))
.arg(tr(") to perform the conversions. This requires that you have GPSBabel installed where QGIS can find it."))
.arg(tr("Select a GPX input file name, the type of conversion you want to perform, a GPX filename that you want to save the converted file as, and a name for the new layer created from the result."));

teCONVDescription->setHtml(text);
QgsDebugMsg(text);
}

void QgsGPSPluginGui::on_pbnCONVInput_clicked()
{
QString myFileTypeQString;
QString myFilterString=tr("GPS eXchange format (*.gpx)");
QSettings settings;
QString dir = settings.readEntry("/Plugin-GPS/gpxdirectory");
if (dir.isEmpty())
dir = ".";
QString myFileNameQString = QFileDialog::getOpenFileName(
this, //parent dialog
tr("Select GPX file"), //caption
dir, //initial dir
myFilterString, //filters to select
&myFileTypeQString); //the pointer to store selected filter
if (!myFileNameQString.isEmpty())
leCONVInput->setText(myFileNameQString);
}

void QgsGPSPluginGui::on_pbnCONVOutput_clicked() {
QString myFileNameQString =
QFileDialog::getSaveFileName(this, //parent dialog
tr("Choose a filename to save under"),
".", //initial dir
tr("GPS eXchange format (*.gpx)"));
if (!myFileNameQString.isEmpty())
leCONVOutput->setText(myFileNameQString);
}

void QgsGPSPluginGui::openDeviceEditor() {
QgsGPSDeviceDialog* dlg = new QgsGPSDeviceDialog(mDevices);
@@ -51,6 +51,9 @@ public slots:

void on_pbnIMPInput_clicked();
void on_pbnIMPOutput_clicked();

void on_pbnCONVInput_clicked();
void on_pbnCONVOutput_clicked();

void on_pbnDLOutput_clicked();

@@ -60,6 +63,7 @@ public slots:
void populateULLayerComboBox();
void populateIMPBabelFormats();
void populatePortComboBoxes();
void populateCONVDialog();

private slots:

@@ -76,6 +80,10 @@ private slots:
bool importWaypoints, bool importRoutes,
bool importTracks, QString outputFilename,
QString layerName);
void convertGPSFile(QString inputFilename,
int convertType,
QString outputFilename,
QString layerName);
void downloadFromGPS(QString device, QString port, bool downloadWaypoints,
bool downloadRoutes, bool downloadTracks,
QString outputFilename, QString layerName);
Loading

0 comments on commit 8eb726b

Please sign in to comment.