OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
12 
13 namespace OPENTREP {
14 
15  // ////////////////////////////////////////////////////////////////////
16  void BomJSONExport::
17  jsonExportLocationList (std::ostream& oStream,
18  const LocationList_T& iLocationList) {
19 
20  // Create empty Boost.Property_Tree objects
21  bpt::ptree lPT;
22  bpt::ptree lPTLocationList;
23 
24  for (LocationList_T::const_iterator itLocation = iLocationList.begin();
25  itLocation != iLocationList.end(); ++itLocation) {
26  const Location& lLocation = *itLocation;
27  //
28  bpt::ptree lPTLocation;
29  jsonExportLocation (lPTLocation, lLocation);
30 
31  // List of extra matching locations (those with the same matching
32  // weight/percentage)
33  const LocationList_T& lExtraLocationList= lLocation.getExtraLocationList();
34  if (lExtraLocationList.empty() == false) {
35  //
36  bpt::ptree lPTExtraLocationList;
37 
38  NbOfMatches_T idxExtra = 0;
39  for (LocationList_T::const_iterator itLoc = lExtraLocationList.begin();
40  itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
41  const Location& lExtraLocation = *itLoc;
42  //
43  bpt::ptree lPTExtraLocation;
44  jsonExportLocation (lPTExtraLocation, lExtraLocation);
45 
46  // Put the current location tree in the location list
47  lPTExtraLocationList.push_back (std::make_pair("", lPTExtraLocation));
48  }
49 
50  // Add the extra location list to the Boost.Property_Tree representing
51  // the current Location instance.
52  lPTLocation.add_child ("extras", lPTExtraLocationList);
53  }
54 
55  // List of alternate matching locations (those with a lower matching
56  // weight/percentage)
57  const LocationList_T& lAltLocationList =
58  lLocation.getAlternateLocationList();
59  if (lAltLocationList.empty() == false) {
60  //
61  bpt::ptree lPTAltLocationList;
62 
63  NbOfMatches_T idxAlter = 0;
64  for (LocationList_T::const_iterator itLoc = lAltLocationList.begin();
65  itLoc != lAltLocationList.end(); ++itLoc, ++idxAlter) {
66  const Location& lAltLocation = *itLoc;
67  //
68  bpt::ptree lPTAltLocation;
69  jsonExportLocation (lPTAltLocation, lAltLocation);
70 
71  // Put the current location tree in the location list
72  lPTAltLocationList.push_back (std::make_pair ("", lPTAltLocation));
73  }
74 
75  // Add the extra location list to the Boost.Property_Tree representing
76  // the current Location instance.
77  lPTLocation.add_child ("alternates", lPTAltLocationList);
78  }
79 
80  // Add the current location property tree to the location list
81  lPTLocationList.push_back (std::make_pair ("", lPTLocation));
82  }
83 
84  // Add the location list to the root of the Boost.Property_Tree
85  lPT.add_child ("locations", lPTLocationList);
86 
87  // Write the property tree into a JSON string
88  write_json (oStream, lPT);
89  }
90 
91  // ////////////////////////////////////////////////////////////////////
92  void BomJSONExport::jsonExportLocation (bpt::ptree& ioPTLocation,
93  const Location& iLocation) {
94  // Fill all the fields of the JSON instance
95  ioPTLocation.put ("iata_code", iLocation.getIataCode());
96  ioPTLocation.put ("icao_code", iLocation.getIcaoCode());
97  ioPTLocation.put ("geonames_id", iLocation.getGeonamesID());
98  ioPTLocation.put ("faa_code", iLocation.getFaaCode());
99  ioPTLocation.put ("city_code", iLocation.getCityCode());
100  ioPTLocation.put ("city_name_utf", iLocation.getCityUtfName());
101  ioPTLocation.put ("city_name_ascii", iLocation.getCityAsciiName());
102  ioPTLocation.put ("state_code", iLocation.getStateCode());
103  ioPTLocation.put ("country_code", iLocation.getCountryCode());
104  ioPTLocation.put ("country_name", iLocation.getCountryName());
105  ioPTLocation.put ("continent_name", iLocation.getContinentName());
106  ioPTLocation.put ("adm1_code", iLocation.getAdmin1Code());
107  ioPTLocation.put ("adm1_name_utf", iLocation.getAdmin1UtfName());
108  ioPTLocation.put ("adm1_name_ascii", iLocation.getAdmin1AsciiName());
109  ioPTLocation.put ("adm2_code", iLocation.getAdmin2Code());
110  ioPTLocation.put ("adm2_name_utf", iLocation.getAdmin2UtfName());
111  ioPTLocation.put ("adm2_name_ascii", iLocation.getAdmin2AsciiName());
112  ioPTLocation.put ("adm3_code", iLocation.getAdmin3Code());
113  ioPTLocation.put ("adm4_code", iLocation.getAdmin4Code());
114  ioPTLocation.put ("tvl_por_list", iLocation.getTvlPORListString());
115  ioPTLocation.put ("time_zone", iLocation.getTimeZone());
116  ioPTLocation.put ("lat", iLocation.getLatitude());
117  ioPTLocation.put ("lon", iLocation.getLongitude());
118  ioPTLocation.put ("page_rank", iLocation.getPageRank());
119  ioPTLocation.put ("wiki_link", iLocation.getWikiLink());
120  ioPTLocation.put ("original_keywords", iLocation.getOriginalKeywords());
121  ioPTLocation.put ("corrected_keywords", iLocation.getCorrectedKeywords());
122  ioPTLocation.put ("matching_percentage", iLocation.getPercentage());
123  ioPTLocation.put ("edit_distance", iLocation.getEditDistance());
124  ioPTLocation.put ("allowable_distance", iLocation.getAllowableEditDistance());
125 
126  bpt::ptree ptLocationNameList;
127  // Retrieve the place names in all the available languages
128  const NameMatrix& lNameMatrixFull = iLocation.getNameMatrix();
129  const NameMatrix_T& lNameMatrix = lNameMatrixFull.getNameMatrix();
130  for (NameMatrix_T::const_iterator itNameList = lNameMatrix.begin();
131  itNameList != lNameMatrix.end(); ++itNameList) {
132  // Retrieve the language code and locale
133  // const Language::EN_Language& lLanguage = itNameList->first;
134  const Names& lNames = itNameList->second;
135 
136  // For a given language, retrieve the list of place names
137  const NameList_T& lNameList = lNames.getNameList();
138 
139  for (NameList_T::const_iterator itName = lNameList.begin();
140  itName != lNameList.end(); ++itName) {
141  const std::string& lName = *itName;
142 
143  if (lName.empty() == false) {
144  bpt::ptree ptLocationName;
145  ptLocationName.put ("name", lName);
146  ptLocationNameList.push_back (std::make_pair ("", ptLocationName));
147  }
148  }
149  }
150 
151  // Add the name list to the Boost.Property_Tree representing the location
152  ioPTLocation.add_child ("names", ptLocationNameList);
153  }
154 
155 }
const Admin3Code_T & getAdmin3Code() const
Definition: Location.hpp:304
const Admin1Code_T & getAdmin1Code() const
Definition: Location.hpp:262
const CountryCode_T & getCountryCode() const
Definition: Location.hpp:171
const Latitude_T & getLatitude() const
Definition: Location.hpp:234
unsigned short NbOfMatches_T
Structure modelling a (geographical) location.
Definition: Location.hpp:24
const Admin1ASCIIName_T & getAdmin1AsciiName() const
Definition: Location.hpp:276
const CityASCIIName_T & getCityAsciiName() const
Definition: Location.hpp:150
const FAACode_T & getFaaCode() const
Definition: Location.hpp:72
const NbOfErrors_T & getAllowableEditDistance() const
Definition: Location.hpp:410
const ContinentName_T & getContinentName() const
Definition: Location.hpp:199
const NameMatrix_T & getNameMatrix() const
Definition: NameMatrix.hpp:44
const Admin1UTFName_T & getAdmin1UtfName() const
Definition: Location.hpp:269
const LocationList_T & getAlternateLocationList() const
Definition: Location.hpp:424
std::list< std::string > NameList_T
Definition: Names.hpp:20
const std::string & getCorrectedKeywords() const
Definition: Location.hpp:388
const TvlPORListString_T & getTvlPORListString() const
Definition: Location.hpp:101
const NameMatrix & getNameMatrix() const
Definition: Location.hpp:360
const Admin2UTFName_T & getAdmin2UtfName() const
Definition: Location.hpp:290
const PageRank_T & getPageRank() const
Definition: Location.hpp:339
const GeonamesID_T & getGeonamesID() const
Definition: Location.hpp:51
const ICAOCode_T & getIcaoCode() const
Definition: Location.hpp:65
const MatchingPercentage_T & getPercentage() const
Definition: Location.hpp:395
const NbOfErrors_T & getEditDistance() const
Definition: Location.hpp:402
const CityUTFName_T & getCityUtfName() const
Definition: Location.hpp:143
std::list< Location > LocationList_T
const IATACode_T & getIataCode() const
Definition: Location.hpp:37
const Admin2Code_T & getAdmin2Code() const
Definition: Location.hpp:283
const NameList_T & getNameList() const
Definition: Names.hpp:60
const LocationList_T & getExtraLocationList() const
Definition: Location.hpp:417
const Admin2ASCIIName_T & getAdmin2AsciiName() const
Definition: Location.hpp:297
const CityCode_T & getCityCode() const
Definition: Location.hpp:136
const std::string & getOriginalKeywords() const
Definition: Location.hpp:381
std::map< LanguageCode_T, Names > NameMatrix_T
Definition: Names.hpp:149
const Admin4Code_T & getAdmin4Code() const
Definition: Location.hpp:311
const StateCode_T & getStateCode() const
Definition: Location.hpp:164
static void jsonExportLocation(bpt::ptree &, const Location &)
const CountryName_T & getCountryName() const
Definition: Location.hpp:185
const WikiLink_T & getWikiLink() const
Definition: Location.hpp:353
static void jsonExportLocationList(std::ostream &, const LocationList_T &)
const TimeZone_T & getTimeZone() const
Definition: Location.hpp:206
const Longitude_T & getLongitude() const
Definition: Location.hpp:241