SStationListSQLManager.cc
Go to the documentation of this file.
1 #include <map>
2 #include <boost/lexical_cast.hpp>
3 #include <sdet/SStationListSQLManager.h>
4 #include <sdet/SManagerRegister.h>
5 #include <utl/UTCDateTime.h>
6 
7 using namespace std;
8 using namespace sdet;
9 using namespace det;
10 using namespace utl;
11 using namespace boost;
12 
13 
14 REGISTER_S_MANAGER("SStationListSQLManager", SStationListSQLManager);
15 
16 
17 namespace sdet {
18 
19  struct UTMData {
20  string fEllipsoid;
21  int fZone = 0;
22  string fBand;
23  };
24 
25 
26  string
27  Utf8To7Bit(const string& str)
28  {
29  ostringstream res;
30 
31  for (string::const_iterator it = str.begin(); it != str.end(); ++it) {
32  const int i = (unsigned char)(*it);
33  switch (i) {
34  // ntilde; appears in two ways: 0xf1 and 0xc3b1
35  case 195:
36  ++it;
37  if (it != str.end() && int((unsigned char)(*it)) == 177)
38  res << "~n";
39  break;
40  case 241:
41  res << "~n";
42  break;
43  default:
44  // drop unprintable chars
45  if (32 <= i && i < 127)
46  res << *it;
47  break;
48  }
49  }
50 
51  return res.str();
52  }
53 
54 
55  void
56  SStationListSQLManager::Init(const string& configLink)
57  {
58  // first call shadowed VManager::Init()
59  VSQLManager::Init(configLink);
60 
61  typedef map<unsigned int, UTMData> UTMDataMap;
62 
63  UTMDataMap utmMap;
64 
65  ostringstream query;
66  query
67  << "SELECT utm_data_id, ellipsoid, zone, band "
68  "FROM utm_data "
69  "ORDER BY utm_data.last_modified DESC";
70 
71  if (Query(query.str(), QueryInfoMessage("all utm_data", "utm_data")) == eNotFound ||
72  NumRows() < 1) {
73  const string err = "Can not find UTM data!";
74  throw DataNotFoundInDBException(err);
75  }
76 
77  {
78  // insert only earliest entry
79  tuple<int, string, int, string> utmId_ellipsoid_zone_band;
80  UTMData utm;
81  while (FetchNextRowMany(utmId_ellipsoid_zone_band) == eFound) {
82  const int& utmId = utmId_ellipsoid_zone_band.get<0>();
83  if (utmMap.find(utmId) == utmMap.end()) {
84  utm.fEllipsoid = utmId_ellipsoid_zone_band.get<1>();
85  utm.fZone = utmId_ellipsoid_zone_band.get<2>();
86  utm.fBand = utmId_ellipsoid_zone_band.get<3>();
87  utmMap[utmId] = utm;
88  }
89  }
90  }
91 
92  query.str("");
93  query
94  << "SELECT "
95  "utm_data_id, "
96  "station_id, "
97  "northing, "
98  "easting, "
99  "altitude, "
100  "name, "
101  "commission, "
102  "decommission, "
103  "inGrid, "
104  "in_pair, " // -> fGroupId
105  "axis_1, "
106  "axis_2 "
107  "FROM station_list "
108  "ORDER BY station_id ASC, last_modified DESC";
109 
110  if (Query(query.str(), QueryInfoMessage("all station_list", "station_list")) == eNotFound ||
111  NumRows() < 1) {
112  const string err = "No station_list SQL data found!";
113  throw DataNotFoundInDBException(err);
114  }
115 
116  // this query has to be parsed low-level since boost::tuple supports only up to
117  // 10 types in template parameters
118  MYSQL_ROW row;
119  while ((row = FetchRow())) {
120 
121  if (NumFields() != 12) {
122  const string err = "Not enogh fields while getting whole station_list";
123  throw DataNotFoundInDBException(err);
124  }
125 
126  int column = 0;
127 
128  const unsigned int utmId = lexical_cast<unsigned int>(row[column++]);
129 
131 
132  station.fId = lexical_cast<int>(row[column++]);
133 
134  if (fStationManager.HasStationData(station.fId))
135  continue;
136 
137  const UTMDataMap::const_iterator utmIt = utmMap.find(utmId);
138  if (utmIt == utmMap.end()) {
139  const string err = "Unknown UTM id!";
140  throw DataNotFoundInDBException(err);
141  }
142 
143  const UTMData& utm = utmIt->second;
144 
145  station.fEllipsoid = utm.fEllipsoid;
146  station.fZone = utm.fZone;
147  station.fBand = utm.fBand;
148 
149  station.fNorthing = lexical_cast<double>(row[column++]);
150  station.fEasting = lexical_cast<double>(row[column++]);
151  station.fAltitude = lexical_cast<double>(row[column++]);
152  station.fName = Utf8To7Bit(lexical_cast<string>(row[column++]));
153 
154  station.fCommissionTime =
155  lexical_cast<string>(UTCDateTime(TimeStamp(lexical_cast<int>(row[column++]))));
156 
157  station.fDecommissionTime =
158  lexical_cast<string>(UTCDateTime(TimeStamp(lexical_cast<int>(row[column++]))));
159 
160  station.fInGrid = lexical_cast<int>(row[column++]);
161  station.fGroupId = lexical_cast<int>(row[column++]);
162  station.fAxis1 = lexical_cast<int>(row[column++]);
163  station.fAxis2 = lexical_cast<int>(row[column++]);
164 
165  fStationManager.ConditionalAddStationData(station);
166 
167  }
168 
169  FreeResult();
170 
171  // close SQL to save one connection
172  Close();
173  }
174 
175 }
Manager for SD description in SQL station lists.
void Init()
Initialise the registry.
string Utf8To7Bit(const string &str)
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
Exception to use in case requested data not found in the database with detailed printout.
#define REGISTER_S_MANAGER(_name_, _Type_)

, generated on Tue Sep 26 2023.