source: ntrip/trunk/BNC/bncutils.cpp@ 2002

Last change on this file since 2002 was 1942, checked in by mervart, 15 years ago

* empty log message *

File size: 5.3 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncutils
30 *
31 * Purpose: Auxiliary Functions
32 *
33 * Author: L. Mervart
34 *
35 * Created: 30-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <ctime>
43#include <math.h>
44
45#include <QRegExp>
46#include <QStringList>
47#include <QDateTime>
48
49#include "bncutils.h"
50#include "bncapp.h"
51
52using namespace std;
53
54//
55////////////////////////////////////////////////////////////////////////////
56void expandEnvVar(QString& str) {
57
58 QRegExp rx("(\\$\\{.+\\})");
59
60 if (rx.indexIn(str) != -1) {
61 QStringListIterator it(rx.capturedTexts());
62 if (it.hasNext()) {
63 QString rxStr = it.next();
64 QString envVar = rxStr.mid(2,rxStr.length()-3);
65 str.replace(rxStr, qgetenv(envVar.toAscii()));
66 }
67 }
68
69}
70
71//
72////////////////////////////////////////////////////////////////////////////
73QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
74
75 static const QDate zeroEpoch(1980, 1, 6);
76
77 QDate date(zeroEpoch);
78 QTime time(0,0,0,0);
79
80 int weekDays = int(GPSWeeks) / 86400;
81 date = date.addDays( GPSWeek * 7 + weekDays );
82 time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
83
84 return QDateTime(date,time);
85}
86
87//
88////////////////////////////////////////////////////////////////////////////
89void currentGPSWeeks(int& week, double& sec) {
90
91 QDateTime currDateTimeGPS;
92
93 if ( ((bncApp*) qApp)->_currentDateAndTimeGPS ) {
94 currDateTimeGPS = *(((bncApp*) qApp)->_currentDateAndTimeGPS);
95 }
96 else {
97 currDateTimeGPS = QDateTime::currentDateTime().toUTC();
98 QDate hlp = currDateTimeGPS.date();
99 currDateTimeGPS = currDateTimeGPS.addSecs(gnumleap(hlp.year(),
100 hlp.month(), hlp.day()));
101 }
102
103 QDate currDateGPS = currDateTimeGPS.date();
104 QTime currTimeGPS = currDateTimeGPS.time();
105
106 week = int( (double(currDateGPS.toJulianDay()) - 2444244.5) / 7 );
107
108 sec = (currDateGPS.dayOfWeek() % 7) * 24.0 * 3600.0 +
109 currTimeGPS.hour() * 3600.0 +
110 currTimeGPS.minute() * 60.0 +
111 currTimeGPS.second() +
112 currTimeGPS.msec() / 1000.0;
113}
114
115//
116////////////////////////////////////////////////////////////////////////////
117QDateTime currentDateAndTimeGPS() {
118 int GPSWeek;
119 double GPSWeeks;
120 currentGPSWeeks(GPSWeek, GPSWeeks);
121 return dateAndTimeFromGPSweek(GPSWeek, GPSWeeks);
122}
123
124//
125////////////////////////////////////////////////////////////////////////////
126QByteArray ggaString(const QByteArray& latitude,
127 const QByteArray& longitude,
128 const QByteArray& height) {
129
130 double lat = strtod(latitude,NULL);
131 double lon = strtod(longitude,NULL);
132 double hei = strtod(height,NULL);
133
134 const char* flagN="N";
135 const char* flagE="E";
136 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
137 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
138 if (lon < -180.) {lon=(lon+360.); flagE="E";}
139 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
140 QTime ttime(QDateTime::currentDateTime().toUTC().time());
141 int lat_deg = (int)lat;
142 double lat_min=(lat-lat_deg)*60.;
143 int lon_deg = (int)lon;
144 double lon_min=(lon-lon_deg)*60.;
145 int hh = 0 , mm = 0;
146 double ss = 0.0;
147 hh=ttime.hour();
148 mm=ttime.minute();
149 ss=(double)ttime.second()+0.001*ttime.msec();
150 QString gga;
151 gga += "GPGGA,";
152 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
153 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
154 gga += flagN;
155 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
156 gga += flagE + QString(",1,05,1.00");
157 gga += QString(",%1,").arg(hei, 2, 'f', 1);
158 gga += QString("M,10.000,M,,");
159 int xori;
160 char XOR = 0;
161 char *Buff =gga.toAscii().data();
162 int iLen = strlen(Buff);
163 for (xori = 0; xori < iLen; xori++) {
164 XOR ^= (char)Buff[xori];
165 }
166 gga = "$" + gga + QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
167
168 return gga.toAscii();
169}
Note: See TracBrowser for help on using the repository browser.