OpenTREP Logo  0.07.9
C++ Open Travel Request Parsing Library
DbaPlace.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <exception>
6 #include <string>
7 #include <sstream>
8 #include <ctime>
9 // OpenTrep
11 #include <opentrep/bom/Place.hpp>
14 
15 namespace soci {
16 
17  // //////////////////////////////////////////////////////////////////////
18  // TODO: that method is obviously deprecated. Remove it.
19  void type_conversion<OPENTREP::Place>::
20  from_base (values const& iPlaceValues, indicator /* ind */,
21  OPENTREP::Place& ioPlace) {
22  /*
23  select dts.iata_code, dts.location_type, dts.geoname_id,
24  icao_code, faa_code,
25  envelope_id, dts.name as utf_name, asciiname, latitude, longitude,
26  fclass, fcode, page_rank,
27  date(date_from) as date_from, date(date_until) as date_until,
28  comment,
29  country_code, cc2, country_name, continent_name,
30  admin1_code, admin1_UTF8_name, admin1_ASCII_name,
31  admin2_code, admin2_UTF8_name, admin2_ASCII_name,
32  admin3_code, admin4_code,
33  population, elevation, gtopo30,
34  time_zone, gmt_offset, dst_offset, raw_offset,
35  date(moddate) as moddate,
36  state_code, wac, wac_name, wiki_link,
37  alt.lang_code as alt_lang_code, alt.name as alt_name,
38  alt.specifiers as alt_spec,
39  city_iata_code, city_location_type, city_geoname_id,
40  city_UTF8_name, city_ASCII_name
41  from optd_por_public_details dts, optd_por_public_alt_names alt,
42  optd_por_public_served_cities cty
43  where dts.iata_code = alt.iata_code
44  and dts.location_type = alt.location_type
45  and dts.geoname_id = alt.geoname_id
46  and dts.iata_code = cty.iata_code
47  and dts.location_type = cty.location_type
48  and dts.geoname_id = cty.geoname_id
49  order by dts.iata_code, dts.location_type, dts.geoname_id
50  ;
51  */
52 
53  /*
54  * Retrieve the place key (IATA code, location type, Geonames ID).
55  * If ioPlace has already that key, then only the alternate names need
56  * to be added.
57  * Otherwise, all the fields of the Place need to be set.
58  */
59  // First, retrieve the key from the database fields
60  // IATA code
61  const std::string lIataCodeStr =
62  iPlaceValues.get<std::string> ("iata_code", "");
63  const OPENTREP::IATACode_T lIataCode (lIataCodeStr);
64  // Location type ('C' for city only, 'A' for airport only,
65  // 'CA' for airport/city)
66  const std::string lIataTypeStr =
67  iPlaceValues.get<std::string> ("location_type", "");
68  const OPENTREP::IATAType lIataType (lIataTypeStr);
69  // Geonames ID
70  const OPENTREP::GeonamesID_T lGeonameID =
71  iPlaceValues.get<int> ("geoname_id", 0);
72  //
73  const OPENTREP::LocationKey lNewPlaceKey (lIataCode, lIataType, lGeonameID);
74 
75  // Then, retrieve the key from the previous Place object
76  const OPENTREP::LocationKey lPlaceKey = ioPlace.getKey();
77 
78  // Compare the keys
79  if (lNewPlaceKey != lPlaceKey) {
80  // The place keys are different: the current place retrieved from
81  // the database is not the same as the one retrieved previously
82 
83  // Re-set the (STL) sets of terms for the Xapian index, spelling
84  // dictionary, etc.
85  ioPlace.resetIndexSets();
86 
87  // Reset the list of names of the given Place object
88  ioPlace.resetMatrix();
89 
90  // The IATA code will be set to the default value (empty string)
91  // when the column is null
92  ioPlace.setIataCode (lIataCode);
93 
94  // The ICAO code will be set to the default value (empty string)
95  // when the column is null
96  ioPlace.setIataType (lIataTypeStr);
97 
98  // Geonames ID
99  ioPlace.setGeonamesID (lGeonameID);
100 
101  // Envelope ID
102  const OPENTREP::EnvelopeID_T lEnvelopeID =
103  iPlaceValues.get<int> ("envelope_id");
104  ioPlace.setEnvelopeID (lEnvelopeID);
105 
106  // Beginning date of the validity range (empty when indefinitely valid)
107  const std::string lDateFromStr =
108  iPlaceValues.get<std::string> ("date_from", OPENTREP::DEFAULT_DATE_STR);
109  const OPENTREP::Date_T lDateFrom (boost::gregorian::from_string (lDateFromStr));
110  ioPlace.setDateFrom (lDateFrom);
111 
112  // End date of the validity range (empty when indefinitely valid)
113  const std::string lDateUntilStr =
114  iPlaceValues.get<std::string> ("date_until", OPENTREP::DEFAULT_DATE_STR);
115  const OPENTREP::Date_T lDateUntil (boost::gregorian::from_string (lDateUntilStr));
116  ioPlace.setDateEnd (lDateUntil);
117 
118  // The comment will be set to the default value (empty string)
119  // when the column is null
120  ioPlace.setComment (iPlaceValues.get<std::string> ("comment", ""));
121 
122  // The UTF8 name will be set to the default value (empty string)
123  // when the column is null
124  ioPlace.setCommonName (iPlaceValues.get<std::string> ("utf_name", ""));
125 
126  // The ASCII name will be set to the default value (empty string)
127  // when the column is null
128  ioPlace.setAsciiName (iPlaceValues.get<std::string> ("asciiname", ""));
129 
130  // The FAA code will be set to the default value (empty string)
131  // when the column is null
132  ioPlace.setFaaCode (iPlaceValues.get<std::string> ("faa_code", ""));
133 
134  // Feature class
135  const std::string lFeatClass = iPlaceValues.get<std::string>("fclass", "");
136  ioPlace.setFeatureClass (lFeatClass);
137  // Feature code
138  const std::string lFeatCode = iPlaceValues.get<std::string> ("fcode", "");
139  ioPlace.setFeatureCode (lFeatCode);
140 
141  // The state code will be set to the default value (empty string)
142  // when the column is null
143  ioPlace.setStateCode (iPlaceValues.get<std::string> ("state_code", ""));
144  ioPlace.setCountryCode (iPlaceValues.get<std::string> ("country_code"));
145  ioPlace.setCountryName (iPlaceValues.get<std::string> ("country_name"));
146  ioPlace.setAltCountryCode (iPlaceValues.get<std::string> ("cc2", ""));
147  ioPlace.setContinentName (iPlaceValues.get<std::string>("continent_name"));
148  ioPlace.setLatitude (iPlaceValues.get<double> ("latitude"));
149  ioPlace.setLongitude (iPlaceValues.get<double> ("longitude"));
150  ioPlace.setPageRank (iPlaceValues.get<double> ("page_rank",
152 
153  // The administrative level 1 details will be set to the default value
154  // (empty string) when the column is null
155  ioPlace.setAdmin1Code (iPlaceValues.get<std::string> ("admin1_code", ""));
156  ioPlace.setAdmin1UtfName (iPlaceValues.get<std::string> ("admin1_UTF8_name", ""));
157  ioPlace.setAdmin1AsciiName (iPlaceValues.get<std::string> ("admin1_ASCII_name", ""));
158 
159  // The administrative level 1 details will be set to the default value
160  // (empty string) when the column is null
161  ioPlace.setAdmin2Code (iPlaceValues.get<std::string> ("admin2_code", ""));
162  ioPlace.setAdmin2UtfName (iPlaceValues.get<std::string> ("admin2_UTF8_name", ""));
163  ioPlace.setAdmin2AsciiName (iPlaceValues.get<std::string> ("admin2_ASCII_name", ""));
164 
165  // The administrative level 3 code will be set to the default value
166  // (empty string) when the column is null
167  ioPlace.setAdmin3Code (iPlaceValues.get<std::string> ("admin3_code", ""));
168 
169  // The administrative level 4 code will be set to the default value
170  // (empty string) when the column is null
171  ioPlace.setAdmin4Code (iPlaceValues.get<std::string> ("admin4_code", ""));
172 
173  // Population
174  const OPENTREP::Population_T lPopulation =
175  iPlaceValues.get<int> ("population");
176  ioPlace.setPopulation (lPopulation);
177 
178  // Elevation
179  const OPENTREP::Elevation_T lElevation =
180  iPlaceValues.get<int> ("elevation");
181  ioPlace.setElevation (lElevation);
182 
183  // GTopo30
184  const OPENTREP::GTopo30_T lGTopo30 = iPlaceValues.get<int> ("gtopo30");
185  ioPlace.setGTopo30 (lGTopo30);
186 
187  // The time-zone code will be set to the default value (empty string)
188  // when the column is null
189  ioPlace.setTimeZone (iPlaceValues.get<std::string> ("time_zone", ""));
190 
191  // The time off-sets will be set to the default value (empty string)
192  // when the column is null
193  const OPENTREP::GMTOffset_T lGMTOffset =
194  iPlaceValues.get<int> ("gmt_offset", 0.0);
195  ioPlace.setGMTOffset (lGMTOffset);
196  const OPENTREP::DSTOffset_T lDSTOffset =
197  iPlaceValues.get<int> ("dst_offset", 0.0);
198  ioPlace.setDSTOffset (lDSTOffset);
199  const OPENTREP::RawOffset_T lRawOffset =
200  iPlaceValues.get<int> ("raw_offset", 0.0);
201  ioPlace.setRawOffset (lRawOffset);
202 
203  // Modification date
204  const std::string lModDateStr =
205  iPlaceValues.get<std::string> ("moddate", OPENTREP::DEFAULT_DATE_STR);
206  const OPENTREP::Date_T lModDate (boost::gregorian::from_string (lModDateStr));
207  ioPlace.setModificationDate (lModDate);
208 
209  // The US DOT World Area Code (WAC) will be set to the default value (zero)
210  // when the column is null
211  ioPlace.setWAC (iPlaceValues.get<int> ("wac", 0));
212 
213  // The US DOT World Area Code (WAC) name will be set to the default value
214  // (empty string) when the column is null
215  ioPlace.setWACName (iPlaceValues.get<std::string> ("wac_name", ""));
216 
217  // The Wikipedia link will be set to the default value (empty string)
218  // when the column is null
219  ioPlace.setWikiLink (iPlaceValues.get<std::string> ("wiki_link", ""));
220  }
221 
222  // Alternate name
223  const std::string& lLanguageString =
224  iPlaceValues.get<std::string> ("alt_lang_code", "");
225  /*
226  if (lLanguageString.empty() == true) {
227  std::ostringstream errorStr;
228  errorStr << "The language field is empty for the place "
229  << ioPlace.getIataCode();
230  OPENTREP_LOG_ERROR (errorStr.str());
231  throw OPENTREP::LanguageCodeNotDefinedInNameTableException(errorStr.str());
232  }
233  */
234 
235  // TODO: check that that alternate name does not already exist
236  // Convert the language code string (e.g., "en") into
237  // the corresponding enumeration
238  //const OPENTREP::Language::EN_Language lLanguageCode =
239  // OPENTREP::Language::getCode (lLanguageString);
240  const OPENTREP::LanguageCode_T lLanguageStr (lLanguageString);
241 
242  // Add the (UTF8) name only when the language code is standard English
243  //if (lLanguageCode == OPENTREP::Language::en) {
244  // UTF-8 name (which may be null/empty)
245  const std::string& lUtfName =
246  iPlaceValues.get<std::string> ("alt_name", "");
247  ioPlace.addName (lLanguageStr, lUtfName);
248  //}
249 
250  // TODO: check that that city code/name does not already exist, and add it
251  // The city code will be set to the default value (empty string)
252  // when the column is null
253  //ioPlace.setCityCode (iPlaceValues.get<std::string> ("city_iata_code", ""));
254  // The city UTF8 name will be set to the default value (empty string)
255  // when the column is null
256  //ioPlace.setCityUtfName (iPlaceValues.get<std::string>("city_UTF8_name", ""));
257  // The city ASCII name will be set to the default value (empty string)
258  // when the column is null
259  //ioPlace.setCityAsciiName (iPlaceValues.get<std::string> ("city_ASCII_name", ""));
260 
261 
262  // DEBUG
263  /*
264  OPENTREP_LOG_DEBUG ("[Old:" << lPlaceKey << "][New:" << lNewPlaceKey << "] Lang: "
265  << lLanguageString << " => "
266  << OPENTREP::Language::getLongLabel (lLanguageCode)
267  << " -- Details: " << ioPlace);
268  */
269  }
270 
271  // //////////////////////////////////////////////////////////////////////
272  // TODO: that method is obviously deprecated. Remove it.
273  void type_conversion<OPENTREP::Place>::
274  to_base (const OPENTREP::Place& iPlace, values& ioPlaceValues,
275  indicator& ioIndicator) {
276  // IATA code
277  const OPENTREP::IATACode_T& lIataCode = iPlace.getIataCode();
278  const std::string lIataCodeStr (lIataCode);
279  const indicator lIataCodeIndicator = lIataCodeStr.empty() ? i_null : i_ok;
280 
281  // ICAO code
282  const OPENTREP::ICAOCode_T& lIcaoCode = iPlace.getIcaoCode();
283  const std::string lIcaoCodeStr (lIcaoCode);
284  const indicator lIcaoCodeIndicator = lIcaoCodeStr.empty() ? i_null : i_ok;
285 
286  // FAA code
287  const OPENTREP::FAACode_T& lFaaCode = iPlace.getFaaCode();
288  const std::string lFaaCodeStr (lFaaCode);
289  const indicator lFaaCodeIndicator = lFaaCodeStr.empty() ? i_null : i_ok;
290 
291  // City IATA code
292  //const OPENTREP::IATACode_T& lCityCode = iPlace.getCityCode();
293  //const std::string lCityCodeStr (lCityCode);
294  //const indicator lCityCodeIndicator = lCityCodeStr.empty() ? i_null : i_ok;
295 
296  // State code
297  const OPENTREP::StateCode_T& lStateCode = iPlace.getStateCode();
298  const std::string lStateCodeStr (lStateCode);
299  const indicator lStateCodeIndicator = lStateCodeStr.empty() ? i_null : i_ok;
300 
301  // Country code
302  const OPENTREP::CountryCode_T& lCountryCode = iPlace.getCountryCode();
303  const std::string lCountryCodeStr (lCountryCode);
304 
305  // Country name
306  const OPENTREP::CountryName_T& lCountryName = iPlace.getCountryName();
307  const std::string lCountryNameStr (lCountryName);
308 
309  // Continent name
310  const OPENTREP::ContinentName_T& lContinentName = iPlace.getContinentName();
311  const std::string lContinentNameStr (lContinentName);
312 
313  // Time-zone
314  const OPENTREP::TimeZone_T& lTimeZone = iPlace.getTimeZone();
315  const std::string lTimeZoneStr (lTimeZone);
316 
317  // US DOT World Area Code (WAC) name
318  const OPENTREP::WACName_T& lWACName = iPlace.getWACName();
319  const std::string lWACNameStr (lWACName);
320 
321  // Wikipedia link
322  const OPENTREP::WikiLink_T& lWikiLink = iPlace.getWikiLink();
323  const std::string lWikiLinkStr (lWikiLink);
324 
325  //
326  ioPlaceValues.set ("iata_code", lIataCodeStr, lIataCodeIndicator);
327  ioPlaceValues.set ("icao_code", lIcaoCodeStr, lIcaoCodeIndicator);
328  ioPlaceValues.set ("geoname_id", iPlace.getGeonamesID());
329  ioPlaceValues.set ("faa_code", lFaaCodeStr, lFaaCodeIndicator);
330  //ioPlaceValues.set ("city_code", lCityCodeStr, lCityCodeIndicator);
331  ioPlaceValues.set ("state_code", lStateCodeStr, lStateCodeIndicator);
332  ioPlaceValues.set ("country_code", lCountryCodeStr);
333  ioPlaceValues.set ("country_name", lCountryNameStr);
334  ioPlaceValues.set ("continent_name", lContinentNameStr);
335  ioPlaceValues.set ("time_zone", lTimeZoneStr);
336  ioPlaceValues.set ("latitude", iPlace.getLatitude());
337  ioPlaceValues.set ("longitude", iPlace.getLongitude());
338  ioPlaceValues.set ("page_rank", iPlace.getPageRank());
339  ioPlaceValues.set ("wac", iPlace.getWAC());
340  ioPlaceValues.set ("wac_name", lWACNameStr);
341  ioPlaceValues.set ("wiki_link", lWikiLinkStr);
342  ioIndicator = i_ok;
343  }
344 
345 }
346 
347 namespace OPENTREP {
348 
349 }
Class modelling a place/POR (point of reference).
Definition: Place.hpp:29
const WACName_T & getWACName() const
Definition: Place.hpp:214
void setWACName(const std::string &iWACName)
Definition: Place.hpp:688
void setAdmin3Code(const std::string &iAdminCode)
Definition: Place.hpp:807
void setGMTOffset(const GMTOffset_T &iOffset)
Definition: Place.hpp:716
void setAdmin1AsciiName(const std::string &iAdminName)
Definition: Place.hpp:779
void setPopulation(const Population_T &iPopulation)
Definition: Place.hpp:821
const ContinentName_T & getContinentName() const
Definition: Place.hpp:235
const LocationKey & getKey() const
Definition: Place.hpp:59
const TimeZone_T & getTimeZone() const
Definition: Place.hpp:242
const IATACode_T & getIataCode() const
Definition: Place.hpp:66
void setAdmin4Code(const std::string &iAdminCode)
Definition: Place.hpp:814
void setStateCode(const std::string &iStateCode)
Definition: Place.hpp:653
const StateCode_T & getStateCode() const
Definition: Place.hpp:179
void setContinentName(const std::string &iContinentName)
Definition: Place.hpp:702
void setComment(const std::string &iComment)
Definition: Place.hpp:639
void resetIndexSets()
Definition: Place.cpp:220
void setWAC(const WAC_T &iWAC)
Definition: Place.hpp:681
void setAdmin2Code(const std::string &iAdminCode)
Definition: Place.hpp:786
void setAltCountryCode(const std::string &iCountryCode)
Definition: Place.hpp:667
const CountryName_T & getCountryName() const
Definition: Place.hpp:200
void setDSTOffset(const DSTOffset_T &iOffset)
Definition: Place.hpp:723
void setLatitude(const Latitude_T &iLatitude)
Definition: Place.hpp:737
void setGeonamesID(const GeonamesID_T &iGeonamesID)
Definition: Place.hpp:561
void setFaaCode(const std::string &iFaaCode)
Definition: Place.hpp:575
void setAdmin1Code(const std::string &iAdminCode)
Definition: Place.hpp:765
void setAdmin2UtfName(const std::string &iAdminName)
Definition: Place.hpp:793
void setAdmin1UtfName(const std::string &iAdminName)
Definition: Place.hpp:772
void setAsciiName(const std::string &iName)
Definition: Place.hpp:604
void setEnvelopeID(const EnvelopeID_T &iEnvelopeID)
Definition: Place.hpp:618
const CountryCode_T & getCountryCode() const
Definition: Place.hpp:186
void setElevation(const Elevation_T &iElevation)
Definition: Place.hpp:828
const GeonamesID_T & getGeonamesID() const
Definition: Place.hpp:80
void setRawOffset(const RawOffset_T &iOffset)
Definition: Place.hpp:730
void setModificationDate(const Date_T &iModDate)
Definition: Place.hpp:849
const Longitude_T & getLongitude() const
Definition: Place.hpp:277
const WikiLink_T & getWikiLink() const
Definition: Place.hpp:389
void setLongitude(const Longitude_T &iLongitude)
Definition: Place.hpp:744
void setCommonName(const std::string &iName)
Definition: Place.hpp:597
void setIataCode(const std::string &iIataCode)
Definition: Place.hpp:547
void addName(const LanguageCode_T &iLanguageCode, const std::string &iName)
Definition: Place.hpp:946
const ICAOCode_T & getIcaoCode() const
Definition: Place.hpp:94
const FAACode_T & getFaaCode() const
Definition: Place.hpp:101
void setCountryCode(const std::string &iCountryCode)
Definition: Place.hpp:660
void setFeatureCode(const std::string &iFeatCode)
Definition: Place.hpp:758
void setTimeZone(const std::string &iTimeZone)
Definition: Place.hpp:709
void setIataType(const std::string &iIATAType)
Definition: Place.hpp:554
void setCountryName(const std::string &iCountryName)
Definition: Place.hpp:674
const WAC_T & getWAC() const
Definition: Place.hpp:207
void setGTopo30(const GTopo30_T &iGTopo30)
Definition: Place.hpp:835
void setAdmin2AsciiName(const std::string &iAdminName)
Definition: Place.hpp:800
void setWikiLink(const std::string &iWikiLink)
Definition: Place.hpp:856
const Latitude_T & getLatitude() const
Definition: Place.hpp:270
void setDateFrom(const Date_T &iDate)
Definition: Place.hpp:625
void resetMatrix()
Definition: Place.hpp:954
void setFeatureClass(const std::string &iFeatClass)
Definition: Place.hpp:751
void setDateEnd(const Date_T &iDate)
Definition: Place.hpp:632
void setPageRank(const PageRank_T &iPageRank)
Definition: Place.hpp:842
const PageRank_T & getPageRank() const
Definition: Place.hpp:375
const std::string DEFAULT_DATE_STR
float GMTOffset_T
const Percentage_T K_DEFAULT_PAGE_RANK
float RawOffset_T
boost::gregorian::date Date_T
unsigned int EnvelopeID_T
float DSTOffset_T
unsigned int Population_T
unsigned int GeonamesID_T
Enumeration of place/location types with respect to their use for transportation purposes.
Definition: IATAType.hpp:42
Class modelling the primary key of a location/POR (point of reference).
Definition: LocationKey.hpp:29