📄 qgsgpsplugin.cpp
字号:
/*************************************************************************** qgsgpsplugin.cpp - GPS related tools ------------------- Date : Jan 21, 2004 Copyright : (C) 2004 by Tim Sutton Email : tim@linfiniti.com***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//* $Id: qgsgpsplugin.cpp 7984 2008-01-16 20:07:55Z wonder $ */// includes#include "qgisinterface.h"#include "qgisgui.h"#include "qgsmaplayerregistry.h"#include "qgsmaplayer.h"#include "qgsvectorlayer.h"#include "qgsdataprovider.h"#include "qgsvectordataprovider.h"#include "qgsgpsplugin.h"#include "qgslogger.h"#include <QFileDialog>#include <QMessageBox>#include <QAction>#include <Q3Process>#include <Q3ProgressDialog>#include <QSettings>#include <QStringList>//non qt includes#include <cassert>#include <fstream>#include <iostream>//the gui subclass#include "qgsgpsplugingui.h"// xpm for creating the toolbar icon#include "icon.xpm"static const char * const ident_ = "$Id: qgsgpsplugin.cpp 7984 2008-01-16 20:07:55Z wonder $";static const QString name_ = QObject::tr("GPS Tools");static const QString description_ = QObject::tr("Tools for loading and importing GPS data");static const QString version_ = QObject::tr("Version 0.1");static const QgisPlugin::PLUGINTYPE type_ = QgisPlugin::UI;/** * Constructor for the plugin. The plugin is passed a pointer to the main app * and an interface object that provides access to exposed functions in QGIS. * @param qgis Pointer to the QGIS main window * @param _qI Pointer to the QGIS interface object */QgsGPSPlugin::QgsGPSPlugin(QgisInterface * theQgisInterFace): QgisPlugin(name_,description_,version_,type_), mQGisInterface(theQgisInterFace){ setupBabel();}QgsGPSPlugin::~QgsGPSPlugin(){ // delete all our babel formats BabelMap::iterator iter; for (iter = mImporters.begin(); iter != mImporters.end(); ++iter) delete iter->second; std::map<QString, QgsGPSDevice*>::iterator iter2; for (iter2 = mDevices.begin(); iter2 != mDevices.end(); ++iter2) delete iter2->second;}/* * Initialize the GUI interface for the plugin */void QgsGPSPlugin::initGui(){ // add an action to the toolbar mQActionPointer = new QAction(QIcon(icon), tr("&Gps Tools"), this); mCreateGPXAction = new QAction(QIcon(icon), tr("&Create new GPX layer"), this); mQActionPointer->setWhatsThis(tr("Creates a new GPX layer and displays it on the map canvas")); mCreateGPXAction->setWhatsThis(tr("Creates a new GPX layer and displays it on the map canvas")); connect(mQActionPointer, SIGNAL(activated()), this, SLOT(run())); connect(mCreateGPXAction, SIGNAL(activated()), this, SLOT(createGPX())); mQGisInterface->addToolBarIcon(mQActionPointer); mQGisInterface->addPluginMenu(tr("&Gps"), mQActionPointer); mQGisInterface->addPluginMenu(tr("&Gps"), mCreateGPXAction);}//method defined in interfacevoid QgsGPSPlugin::help(){ //implement me!}// Slot called when the buffer menu item is activatedvoid QgsGPSPlugin::run(){ // find all GPX layers std::vector<QgsVectorLayer*> gpxLayers; QMap<QString, QgsMapLayer*>::const_iterator iter; QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance(); for (iter = registry->mapLayers().begin(); iter != registry->mapLayers().end(); ++iter) { if (iter.value()->type() == QgsMapLayer::VECTOR) { QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter.value()); if (vLayer->providerType() == "gpx") gpxLayers.push_back(vLayer); } } QgsGPSPluginGui *myPluginGui = new QgsGPSPluginGui(mImporters, mDevices, gpxLayers, mQGisInterface->getMainWindow(), QgisGui::ModalDialogFlags); myPluginGui->setAttribute(Qt::WA_DeleteOnClose); //listen for when the layer has been made so we can draw it connect(myPluginGui, SIGNAL(drawVectorLayer(QString,QString,QString)), this, SLOT(drawVectorLayer(QString,QString,QString))); connect(myPluginGui, SIGNAL(loadGPXFile(QString, bool, bool, bool)), this, SLOT(loadGPXFile(QString, bool, bool, bool))); connect(myPluginGui, SIGNAL(importGPSFile(QString, QgsBabelFormat*, bool, 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, QString, QString))); connect(myPluginGui, SIGNAL(uploadToGPS(QgsVectorLayer*, QString, QString)), this, SLOT(uploadToGPS(QgsVectorLayer*, QString, QString))); connect(this, SIGNAL(closeGui()), myPluginGui, SLOT(close())); myPluginGui->show();}void QgsGPSPlugin::createGPX() { QString fileName = QFileDialog::getSaveFileName(mQGisInterface->getMainWindow(), tr("Save new GPX file as..."), "." , tr("GPS eXchange file (*.gpx)")); if (!fileName.isEmpty()) { QFileInfo fileInfo(fileName); std::ofstream ofs((const char*)fileName); if (!ofs) { QMessageBox::warning(NULL, tr("Could not create file"), tr("Unable to create a GPX file with the given name. ")+ tr("Try again with another name or in another ")+ tr("directory.")); return; } ofs<<"<gpx></gpx>"<<std::endl; emit drawVectorLayer(fileName + "?type=track", fileInfo.baseName() + ", tracks", "gpx"); emit drawVectorLayer(fileName + "?type=route", fileInfo.baseName() + ", routes", "gpx"); emit drawVectorLayer(fileName + "?type=waypoint", fileInfo.baseName() + ", waypoints", "gpx"); }}void QgsGPSPlugin::drawVectorLayer(QString thePathNameQString, QString theBaseNameQString, QString theProviderQString){ mQGisInterface->addVectorLayer(thePathNameQString, theBaseNameQString, theProviderQString);}// Unload the plugin by cleaning up the GUIvoid QgsGPSPlugin::unload(){ // remove the GUI mQGisInterface->removePluginMenu(tr("&Gps"),mQActionPointer); mQGisInterface->removePluginMenu(tr("&Gps"),mCreateGPXAction); mQGisInterface->removeToolBarIcon(mQActionPointer); delete mQActionPointer;}void QgsGPSPlugin::loadGPXFile(QString filename, bool loadWaypoints, bool loadRoutes, bool loadTracks) { //check if input file is readable QFileInfo fileInfo(filename); if (!fileInfo.isReadable()) { QMessageBox::warning(NULL, tr("GPX Loader"), tr("Unable to read the selected file.\n")+ tr("Please reselect a valid file.") ); return; } // remember the directory QSettings settings; settings.writeEntry("/Plugin-GPS/gpxdirectory", fileInfo.dirPath()); // add the requested layers if (loadTracks) emit drawVectorLayer(filename + "?type=track", fileInfo.baseName() + ", tracks", "gpx"); if (loadRoutes) emit drawVectorLayer(filename + "?type=route", fileInfo.baseName() + ", routes", "gpx"); if (loadWaypoints) emit drawVectorLayer(filename + "?type=waypoint", fileInfo.baseName() + ", waypoints", "gpx"); emit closeGui();}void QgsGPSPlugin::importGPSFile(QString inputFilename, QgsBabelFormat* importer, bool importWaypoints, bool importRoutes, bool importTracks, QString outputFilename, QString layerName) { // what features does the user want to import? QString typeArg; if (importWaypoints) typeArg = "-w"; else if (importRoutes) typeArg = "-r"; else if (importTracks) typeArg = "-t"; // try to start the gpsbabel process QStringList babelArgs = importer->importCommand(mBabelPath, typeArg, inputFilename, outputFilename); QgsDebugMsg(QString("Import 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 import data from %1!\n\n") .arg(inputFilename)); errorMsg += babelError; QMessageBox::warning(NULL, tr("Error importing data"), errorMsg); return; } // add the layer if (importTracks) emit drawVectorLayer(outputFilename + "?type=track", layerName, "gpx"); if (importRoutes) emit drawVectorLayer(outputFilename + "?type=route", layerName, "gpx"); if (importWaypoints) emit drawVectorLayer(outputFilename + "?type=waypoint", layerName, "gpx"); emit closeGui();}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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -