OpenTREP Logo  0.07.7
C++ Open Travel Request Parsing Library
BomJSONExport.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <ostream>
7 // Boost ForEach
8 //#include <boost/foreach.hpp>
9 // OpenTREP
10 #include <opentrep/Location.hpp>
11 #include <opentrep/CityDetails.hpp>
13 
14 namespace OPENTREP {
15 
16  // ////////////////////////////////////////////////////////////////////
18  jsonExportLocationList (std::ostream& oStream,
19  const LocationList_T& iLocationList) {
20 
21  // Create empty Boost.Property_Tree objects
22  bpt::ptree lPT;
23  bpt::ptree lPTLocationList;
24 
25  for (LocationList_T::const_iterator itLocation = iLocationList.begin();
26  itLocation != iLocationList.end(); ++itLocation) {
27  const Location& lLocation = *itLocation;
28  //
29  bpt::ptree lPTLocation;
30  jsonExportLocation (lPTLocation, lLocation);
31 
32  // List of extra matching locations (those with the same matching
33  // weight/percentage)
34  const LocationList_T& lExtraLocationList= lLocation.getExtraLocationList();
35  if (lExtraLocationList.empty() == false) {
36  //
37  bpt::ptree lPTExtraLocationList;
38 
39  NbOfMatches_T idxExtra = 0;
40  for (LocationList_T::const_iterator itLoc = lExtraLocationList.begin();
41  itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
42  const Location& lExtraLocation = *itLoc;
43  //
44  bpt::ptree lPTExtraLocation;
45  jsonExportLocation (lPTExtraLocation, lExtraLocation);
46 
47  // Put the current location tree in the location list
48  lPTExtraLocationList.push_back (std::make_pair("", lPTExtraLocation));
49  }
50 
51  // Add the extra location list to the Boost.Property_Tree representing
52  // the current Location instance.
53  lPTLocation.add_child ("extras", lPTExtraLocationList);
54  }
55 
56  // List of alternate matching locations (those with a lower matching
57  // weight/percentage)
58  const LocationList_T& lAltLocationList =
59  lLocation.getAlternateLocationList();
60  if (lAltLocationList.empty() == false) {
61  //
62  bpt::ptree lPTAltLocationList;
63 
64  NbOfMatches_T idxAlter = 0;
65  for (LocationList_T::const_iterator itLoc = lAltLocationList.begin();
66  itLoc != lAltLocationList.end(); ++itLoc, ++idxAlter) {
67  const Location& lAltLocation = *itLoc;
68  //
69  bpt::ptree lPTAltLocation;
70  jsonExportLocation (lPTAltLocation, lAltLocation);
71 
72  // Put the current location tree in the location list
73  lPTAltLocationList.push_back (std::make_pair ("", lPTAltLocation));
74  }
75 
76  // Add the extra location list to the Boost.Property_Tree representing
77  // the current Location instance.
78  lPTLocation.add_child ("alternates", lPTAltLocationList);
79  }
80 
81  // Add the current location property tree to the location list
82  lPTLocationList.push_back (std::make_pair ("", lPTLocation));
83  }
84 
85  // Add the location list to the root of the Boost.Property_Tree
86  lPT.add_child ("locations", lPTLocationList);
87 
88  // Write the property tree into a JSON string
89  write_json (oStream, lPT);
90  }
91 
92  // ////////////////////////////////////////////////////////////////////
93  void BomJSONExport::jsonExportLocation (bpt::ptree& ioPTLocation,
94  const Location& iLocation) {
95  // Fill all the fields of the JSON instance
96  ioPTLocation.put ("iata_code", iLocation.getIataCode());
97  ioPTLocation.put ("icao_code", iLocation.getIcaoCode());
98  ioPTLocation.put ("geonames_id", iLocation.getGeonamesID());
99  ioPTLocation.put ("faa_code", iLocation.getFaaCode());
100  ioPTLocation.put ("state_code", iLocation.getStateCode());
101  ioPTLocation.put ("country_code", iLocation.getCountryCode());
102  ioPTLocation.put ("country_name", iLocation.getCountryName());
103  ioPTLocation.put ("alt_country_code", iLocation.getAltCountryCode());
104  ioPTLocation.put ("continent_name", iLocation.getContinentName());
105  ioPTLocation.put ("adm1_code", iLocation.getAdmin1Code());
106  ioPTLocation.put ("adm1_name_utf", iLocation.getAdmin1UtfName());
107  ioPTLocation.put ("adm1_name_ascii", iLocation.getAdmin1AsciiName());
108  ioPTLocation.put ("adm2_code", iLocation.getAdmin2Code());
109  ioPTLocation.put ("adm2_name_utf", iLocation.getAdmin2UtfName());
110  ioPTLocation.put ("adm2_name_ascii", iLocation.getAdmin2AsciiName());
111  ioPTLocation.put ("adm3_code", iLocation.getAdmin3Code());
112  ioPTLocation.put ("adm4_code", iLocation.getAdmin4Code());
113  ioPTLocation.put ("tvl_por_list", iLocation.getTvlPORListString());
114  ioPTLocation.put ("time_zone", iLocation.getTimeZone());
115  ioPTLocation.put ("lat", iLocation.getLatitude());
116  ioPTLocation.put ("lon", iLocation.getLongitude());
117  ioPTLocation.put ("page_rank", iLocation.getPageRank());
118  ioPTLocation.put ("wac", iLocation.getWAC());
119  ioPTLocation.put ("wac_name", iLocation.getWACName());
120  ioPTLocation.put ("wiki_link", iLocation.getWikiLink());
121  ioPTLocation.put ("currency_code", iLocation.getCurrencyCode());
122  ioPTLocation.put ("original_keywords", iLocation.getOriginalKeywords());
123  ioPTLocation.put ("corrected_keywords", iLocation.getCorrectedKeywords());
124  ioPTLocation.put ("matching_percentage", iLocation.getPercentage());
125  ioPTLocation.put ("edit_distance", iLocation.getEditDistance());
126  ioPTLocation.put ("allowable_distance",iLocation.getAllowableEditDistance());
127 
131  bpt::ptree ptUNLOCodeList;
132  // Retrieve the list of UN/LOCODE codes
133  const UNLOCodeList_T& lUNCodeList = iLocation.getUNLOCodeList();
134  for (UNLOCodeList_T::const_iterator itUNLOCode = lUNCodeList.begin();
135  itUNLOCode != lUNCodeList.end(); ++itUNLOCode) {
136  // Retrieve the UN/LOCODE code
137  const UNLOCode_T& lUNLOCode = *itUNLOCode;
138 
139  // Add the item into the UN/LOCODE list Boost.Property_Tree
140  ptUNLOCodeList.put ("unlocode_code", lUNLOCode);
141  }
142 
143  // Add the UN/LOCODE Boost.Property_Tree to the main tree
144  ioPTLocation.add_child ("unlocode_codes", ptUNLOCodeList);
145 
149  bpt::ptree ptCityList;
150  // Retrieve the list of served cities
151  const CityDetailsList_T& lCityList = iLocation.getCityList();
152  for (CityDetailsList_T::const_iterator itCity = lCityList.begin();
153  itCity != lCityList.end(); ++itCity) {
154  // Retrieve the details, ie, IATA code, Geonames ID and names
155  const CityDetails& lCityDetails = *itCity;
156  bpt::ptree ptCityDetails;
157  ptCityDetails.put ("iata_code", lCityDetails.getIataCode());
158  ptCityDetails.put ("geonames_id", lCityDetails.getGeonamesID());
159  ptCityDetails.put ("name_utf", lCityDetails.getUtfName());
160  ptCityDetails.put ("name_ascii", lCityDetails.getAsciiName());
161 
162  // Add the item into the city list Boost.Property_Tree
163  ptCityList.push_back (std::make_pair ("city_details", ptCityDetails));
164  }
165 
166  // Add the city detail list to the location Boost.Property_Tree
167  ioPTLocation.add_child ("cities", ptCityList);
168 
172  bpt::ptree ptLocationNameList;
173  // Retrieve the place names in all the available languages
174  const NameMatrix& lNameMatrixFull = iLocation.getNameMatrix();
175  const NameMatrix_T& lNameMatrix = lNameMatrixFull.getNameMatrix();
176  for (NameMatrix_T::const_iterator itNameList = lNameMatrix.begin();
177  itNameList != lNameMatrix.end(); ++itNameList) {
178  // Retrieve the language code and locale
179  // const Language::EN_Language& lLanguage = itNameList->first;
180  const Names& lNames = itNameList->second;
181 
182  // For a given language, retrieve the list of place names
183  const NameList_T& lNameList = lNames.getNameList();
184 
185  for (NameList_T::const_iterator itName = lNameList.begin();
186  itName != lNameList.end(); ++itName) {
187  const std::string& lName = *itName;
188 
189  if (lName.empty() == false) {
190  bpt::ptree ptLocationName;
191  ptLocationName.put ("name", lName);
192  ptLocationNameList.push_back (std::make_pair ("", ptLocationName));
193  }
194  }
195  }
196 
197  // Add the name list to the Boost.Property_Tree representing the location
198  ioPTLocation.add_child ("names", ptLocationNameList);
199  }
200 
201 }
static void jsonExportLocationList(std::ostream &, const LocationList_T &)
static void jsonExportLocation(bpt::ptree &, const Location &)
std::map< LanguageCode_T, Names > NameMatrix_T
Definition: Names.hpp:149
std::list< std::string > NameList_T
Definition: Names.hpp:20
std::list< CityDetails > CityDetailsList_T
A list of cities, for instance the list of cities served by a travel-/transport-related POR (point of...
std::list< Location > LocationList_T
std::list< UNLOCode_T > UNLOCodeList_T
unsigned short NbOfMatches_T
Class modelling the elementary details of a city.
Definition: CityDetails.hpp:30
const IATACode_T & getIataCode() const
Definition: CityDetails.hpp:36
const CityASCIIName_T & getAsciiName() const
Definition: CityDetails.hpp:58
const CityUTFName_T & getUtfName() const
Definition: CityDetails.hpp:50
const GeonamesID_T & getGeonamesID() const
Definition: CityDetails.hpp:43
Structure modelling a (geographical) location.
Definition: Location.hpp:25
const WikiLink_T & getWikiLink() const
Definition: Location.hpp:368
const ICAOCode_T & getIcaoCode() const
Definition: Location.hpp:66
const Admin1UTFName_T & getAdmin1UtfName() const
Definition: Location.hpp:284
const LocationList_T & getExtraLocationList() const
Definition: Location.hpp:446
const AltCountryCode_T & getAltCountryCode() const
Definition: Location.hpp:172
const std::string & getOriginalKeywords() const
Definition: Location.hpp:410
const IATACode_T & getIataCode() const
Definition: Location.hpp:38
const Admin2Code_T & getAdmin2Code() const
Definition: Location.hpp:298
const NameMatrix & getNameMatrix() const
Definition: Location.hpp:389
const ContinentName_T & getContinentName() const
Definition: Location.hpp:214
const Admin2UTFName_T & getAdmin2UtfName() const
Definition: Location.hpp:305
const TimeZone_T & getTimeZone() const
Definition: Location.hpp:221
const MatchingPercentage_T & getPercentage() const
Definition: Location.hpp:424
const Admin3Code_T & getAdmin3Code() const
Definition: Location.hpp:319
const WAC_T & getWAC() const
Definition: Location.hpp:186
const Longitude_T & getLongitude() const
Definition: Location.hpp:256
const GeonamesID_T & getGeonamesID() const
Definition: Location.hpp:52
const std::string & getCorrectedKeywords() const
Definition: Location.hpp:417
const NbOfErrors_T & getEditDistance() const
Definition: Location.hpp:431
const UNLOCodeList_T & getUNLOCodeList() const
Definition: Location.hpp:80
const CityDetailsList_T & getCityList() const
Definition: Location.hpp:151
const Admin2ASCIIName_T & getAdmin2AsciiName() const
Definition: Location.hpp:312
const CurrencyCode_T & getCurrencyCode() const
Definition: Location.hpp:200
const WACName_T & getWACName() const
Definition: Location.hpp:193
const CountryName_T & getCountryName() const
Definition: Location.hpp:179
const LocationList_T & getAlternateLocationList() const
Definition: Location.hpp:453
const NbOfErrors_T & getAllowableEditDistance() const
Definition: Location.hpp:439
const Admin1Code_T & getAdmin1Code() const
Definition: Location.hpp:277
const FAACode_T & getFaaCode() const
Definition: Location.hpp:73
const CountryCode_T & getCountryCode() const
Definition: Location.hpp:165
const StateCode_T & getStateCode() const
Definition: Location.hpp:158
const PageRank_T & getPageRank() const
Definition: Location.hpp:354
const Admin4Code_T & getAdmin4Code() const
Definition: Location.hpp:326
const TvlPORListString_T & getTvlPORListString() const
Definition: Location.hpp:116
const Admin1ASCIIName_T & getAdmin1AsciiName() const
Definition: Location.hpp:291
const Latitude_T & getLatitude() const
Definition: Location.hpp:249
const NameMatrix_T & getNameMatrix() const
Definition: NameMatrix.hpp:44
const NameList_T & getNameList() const
Definition: Names.hpp:60