Functions
gpdistance/main.cpp File Reference
#include <math.h>
#include <QGpCoreTools.h>
#include "gpdistanceVersion.h"
#include "gpdistanceInstallPath.h"

Functions

ApplicationHelphelp ()
int main (int argc, char **argv)
 PACKAGE_INFO (gpdistance, GPDISTANCE)

Function Documentation

int main ( int  argc,
char **  argv 
)

References QGpCoreTools::Point2D::azimuthTo(), QGpCoreTools::Point::distanceTo(), QGpCoreTools::endl(), help(), QGpCoreTools::LineParser::setString(), sOut(), QGpCoreTools::LineParser::toDouble(), QGpCoreTools::LineParser::toString(), and QGpCoreTools::tr().

{
  CoreApplication a(argc, argv, help);

  // Options
  QString coordinateFile;
  bool doAll=false;
  // Check arguments
  int i, j=1;
  for(i=1; i<argc; i++) {
    QByteArray arg=argv[i];
    if(arg[0]=='-') {
      if(arg=="-c") {
        CoreApplication::checkOptionArg(i, argc, argv);
        coordinateFile=argv[i];
      } else if(arg=="-all") {
        doAll=true;
      } else {
        App::stream() << tr("gpdistance: bad option %1, see -help").arg(argv[i]) << endl;
        return 2;
      }
    } else {
      argv[j++]=argv[i];
    }
  }
  if(j < argc) {
    argv[j]=0;
    argc=j;
  }

  if(coordinateFile.isEmpty()) {
    App::stream() << tr("gpdistance: missing coordinate file (option -c).") << endl;
    return 2;
  }
  QFile fCoord(coordinateFile);
  if( ! fCoord.open(QIODevice::ReadOnly) ) {
    App::stream() << tr("gpdistance: cannot read file %1.").arg(coordinateFile) << endl;
    return 2;
  }
  QTextStream sCoord(&fCoord);
  LineParser p;
  QMap<QString,Point> points;
  bool ok=true;
  int iLine=0;
  while( ! sCoord.atEnd()) {
    QString l=sCoord.readLine();
    iLine++;
    if(!l.isEmpty() && l[0]!='#') {
      p.setString(l);
      double x=p.toDouble(0, ok);
      double y=p.toDouble(1, ok);
      double z=p.toDouble(2, ok);
      QString stationName=p.toString(3, ok);
      if( !ok) {
        App::stream() << tr("gpdistance: error reading file %1 at line %2").arg(coordinateFile).arg(iLine) << endl;
        return 2;
      }
      points.insert(stationName, Point(x, y, z) );
    }
  }

  QTextStream sOut(stdout);
  if(doAll) {
    for(QMap<QString,Point>::iterator it1=points.begin();it1!=points.end();it1++) {
      for(QMap<QString,Point>::iterator it2=points.begin();it2!=points.end();it2++) {
        if(it1==it2) break;
        Point& p1=it1.value();
        Point& p2=it2.value();
        sOut << it1.key() << " " << it2.key() << " " << p1.distanceTo(p2) << " " << Angle::mathToGeographic(p1.azimuthTo(p2)) << endl;
      }
    }
  } else {
    while(!feof(stdin)) {
      QString l=File::readLine(true);
      if(l=="exit" || l=="quit") break;
      if(!l.isEmpty() && l[0]!='#') {
        p.setString(l);
        QString station1=p.toString(0, ok);
        QString station2=p.toString(1, ok);
        if( !ok) {
          App::stream() << tr("gpdistance: error reading station_name_1 station_name_2") << endl;
          return 2;
        }
        QMap<QString,Point>::iterator it;
        it=points.find(station1);
        if(it==points.end()) {
          App::stream() << tr("gpdistance: unknown station name: %1").arg(station1) << endl;
          return 2;
        }
        Point p1=it.value();
        it=points.find(station2);
        if(it==points.end()) {
          App::stream() << tr("gpdistance: unknown station name: %1").arg(station2) << endl;
          return 2;
        }
        Point p2=it.value();
        sOut << station1 << " " << station2 << " " << p1.distanceTo(p2) << " " << Angle::mathToGeographic(p1.azimuthTo(p2)) << endl;
      }
    }
  }
  return 0;
}
PACKAGE_INFO ( gpdistance  ,
GPDISTANCE   
)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines