| 1 | // gpsparser.cc | 
|---|
| 2 | // Eric Aubourg         CEA/DAPNIA/SPP   juillet 1999 | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 | #include <string.h> | 
|---|
| 6 | #include <stdio.h> | 
|---|
| 7 |  | 
|---|
| 8 | #include "gpsparser.h" | 
|---|
| 9 |  | 
|---|
| 10 | GPSParser::GPSParser() { | 
|---|
| 11 | *gpsString = 0; | 
|---|
| 12 | stringOk = false; | 
|---|
| 13 | gpsutc = 0; | 
|---|
| 14 | gpslat = 0; | 
|---|
| 15 | gpslong = 0; | 
|---|
| 16 | quality = 0; | 
|---|
| 17 | nsat = 0; | 
|---|
| 18 | hdop = 0; | 
|---|
| 19 | alt = 0; | 
|---|
| 20 | gsep = 0; | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | GPSParser::~GPSParser() { | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | bool GPSParser::hasTime() { | 
|---|
| 27 | return (stringOk && gpsutc>0); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | bool GPSParser::hasGPSTime() { | 
|---|
| 31 | return (stringOk && gpsutc>0 && quality>0 && nsat>=3); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | bool GPSParser::hasPosition() { | 
|---|
| 35 | return (stringOk && quality>0 && nsat>=3); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | bool GPSParser::hasAltitude() { | 
|---|
| 39 | return (stringOk && quality>0 && nsat>=4); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | double GPSParser::getUTC() { | 
|---|
| 43 | if (!stringOk) return -99999; | 
|---|
| 44 | return gpsutc; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | double GPSParser::getLatitude() { | 
|---|
| 48 | if (!stringOk) return -99999; | 
|---|
| 49 | if (quality == 0 || nsat<3) return -99999; | 
|---|
| 50 | return gpslat; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | double GPSParser::getLongitude() { | 
|---|
| 54 | if (!stringOk) return -99999; | 
|---|
| 55 | if (quality == 0 || nsat<3) return -99999; | 
|---|
| 56 | return gpslong; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | double GPSParser::getAltitude() { | 
|---|
| 60 | if (!stringOk) return -99999; | 
|---|
| 61 | if (quality == 0 || nsat<4) return -99999; | 
|---|
| 62 | return alt; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | // GPGGA string : | 
|---|
| 66 | // $hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,, | 
|---|
| 67 | // $104020,3740.712,N,00400.781,W,0,00,01.0,0003,M,-050,M,, | 
|---|
| 68 |  | 
|---|
| 69 | #include <fstream.h> | 
|---|
| 70 | #include <iomanip.h> | 
|---|
| 71 | //ofstream gpsdump("gpsdump"); | 
|---|
| 72 | //ofstream gpsdata("gpsdata"); | 
|---|
| 73 | //double outc = 0; double jr = 0; | 
|---|
| 74 |  | 
|---|
| 75 | void GPSParser::ProcessBlock(block_type_gps* blk) { | 
|---|
| 76 | strncpy(gpsString, blk->gps, 80); | 
|---|
| 77 | char* p = gpsString; | 
|---|
| 78 | //  gpsdump << p ; if (p[strlen(p)-1] != '\n') gpsdump << '\n'; | 
|---|
| 79 | char* fence = p+80; | 
|---|
| 80 | stringOk = false; | 
|---|
| 81 | if (*p!='$') return; | 
|---|
| 82 | p++; | 
|---|
| 83 |  | 
|---|
| 84 | // UTC | 
|---|
| 85 | if (*p < '0' || *p > '9') return; | 
|---|
| 86 | double t; | 
|---|
| 87 | sscanf(p, "%lg", &t); | 
|---|
| 88 | int h = int(t/10000); t -= h*10000; | 
|---|
| 89 | int m = int(t/100);   t -= m*100; | 
|---|
| 90 | gpsutc = h*3600. + m*60. + t; | 
|---|
| 91 | p += 6; | 
|---|
| 92 | if (*p != ',') return; | 
|---|
| 93 | p++; | 
|---|
| 94 | if (p>=fence) return; | 
|---|
| 95 |  | 
|---|
| 96 | // LATITUDE | 
|---|
| 97 | if (*p < '0' || *p > '9') return; | 
|---|
| 98 | sscanf(p, "%lg", &t); | 
|---|
| 99 | int d = int(t/100); t -= d*100; | 
|---|
| 100 | t = d + t/60; | 
|---|
| 101 | p += 8; | 
|---|
| 102 | if (*p != ',') return; | 
|---|
| 103 | p++; | 
|---|
| 104 | if (*p == 'S') t = -t; | 
|---|
| 105 | gpslat = t; | 
|---|
| 106 | p++; | 
|---|
| 107 | if (*p != ',') return; | 
|---|
| 108 | p++; | 
|---|
| 109 | if (p>=fence) return; | 
|---|
| 110 |  | 
|---|
| 111 | // LONGITUDE | 
|---|
| 112 | if (*p < '0' || *p > '9') return; | 
|---|
| 113 | sscanf(p, "%lg", &t); | 
|---|
| 114 | d = int(t/100); t -= d*100; | 
|---|
| 115 | t = d + t/60; | 
|---|
| 116 | p += 9; | 
|---|
| 117 | if (*p != ',') return; | 
|---|
| 118 | p++; | 
|---|
| 119 | if (*p == 'W') t = -t; | 
|---|
| 120 | gpslong = t; | 
|---|
| 121 | p++; | 
|---|
| 122 | if (*p != ',') return; | 
|---|
| 123 | p++; | 
|---|
| 124 | if (p>=fence) return; | 
|---|
| 125 |  | 
|---|
| 126 | // QUALITY | 
|---|
| 127 | quality = *p - '0'; | 
|---|
| 128 | if (quality < 0 || quality > 9) return; | 
|---|
| 129 | p++; | 
|---|
| 130 | if (*p != ',') return; | 
|---|
| 131 | p++; | 
|---|
| 132 | if (p>=fence) return; | 
|---|
| 133 |  | 
|---|
| 134 | // NSAT | 
|---|
| 135 | if (*p < '0' || *p > '9') return; | 
|---|
| 136 | sscanf(p, "%d", &nsat); | 
|---|
| 137 | p += 2; | 
|---|
| 138 | if (*p != ',') return; | 
|---|
| 139 | p++; | 
|---|
| 140 |  | 
|---|
| 141 | // HDOP | 
|---|
| 142 | if (*p < '0' || *p > '9') return; | 
|---|
| 143 | sscanf(p, "%lg", &hdop); | 
|---|
| 144 | while (*p != ',' && p<fence) p++; | 
|---|
| 145 | if (*p != ',') return; | 
|---|
| 146 | p++; | 
|---|
| 147 |  | 
|---|
| 148 | // Altitude | 
|---|
| 149 | if (*p < '0' || *p > '9') return; | 
|---|
| 150 | sscanf(p, "%lg", &alt); | 
|---|
| 151 | while (*p != ',' && p<fence) p++; | 
|---|
| 152 | if (*p != ',') return; | 
|---|
| 153 | p++; | 
|---|
| 154 | if (*p != 'M') return; | 
|---|
| 155 | p++; | 
|---|
| 156 | if (*p != ',') return; | 
|---|
| 157 | p++; | 
|---|
| 158 |  | 
|---|
| 159 | // Geoid separation | 
|---|
| 160 | if (*p < '0' || *p > '9') return; | 
|---|
| 161 | sscanf(p, "%lg", &gsep); | 
|---|
| 162 | while (*p != ',' && p<fence) p++; | 
|---|
| 163 | if (*p != ',') return; | 
|---|
| 164 | p++; | 
|---|
| 165 | if (*p != 'M') return; | 
|---|
| 166 |  | 
|---|
| 167 | //  if (gpsutc < outc) jr += 86400; | 
|---|
| 168 | //  outc = gpsutc; | 
|---|
| 169 | //  gpsdata << setprecision(10) << gpsutc + jr << " " << gpslat << " " << gpslong << " " << quality << " " << nsat << '\n'; | 
|---|
| 170 |  | 
|---|
| 171 | stringOk = true; | 
|---|
| 172 | } | 
|---|