OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimWkt.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: David Burken
8 //
9 // Description:
10 //
11 // ossimWkt class definition. A well known text(WKT) utility class.
12 //
13 //----------------------------------------------------------------------------
14 // $Id$
15 
16 /*************************************************************************************************
17  * Sample WKT string (for reference, line feeds and spaces added for human readability)
18  *
19  * PROJCS["NAD_1983_HARN_Lambert_Conformal_Conic",
20  * GEOGCS["GCS_North_American_1983_HARN",
21  * DATUM["NAD83_High_Accuracy_Regional_Network",
22  * SPHEROID["GRS_1980",6378137,298.257222101, AUTHORITY["EPSG","7019"]],
23  * AUTHORITY["EPSG","6152"]],
24  * PRIMEM["Greenwich",0],
25  * UNIT["degree",0.0174532925199433]],
26  * PROJECTION["Lambert_Conformal_Conic_2SP"],
27  * PARAMETER["standard_parallel_1",43],
28  * PARAMETER["standard_parallel_2",45.5],
29  * PARAMETER["latitude_of_origin",41.75],
30  * PARAMETER["central_meridian",-120.5],
31  * PARAMETER["false_easting",1312335.958005249],
32  * PARAMETER["false_northing",0],
33  * UNIT["foot",0.3048, AUTHORITY["EPSG","9002"]]]
34  *
35  **************************************************************************************************/
37 #include <cstdio>
38 #include <iostream>
39 #include <sstream>
40 
41 using namespace std; // tmp drb...
42 
44  : m_kwl()
45 {
46 }
47 
49 {
50 }
51 
52 bool ossimWkt::parse( const std::string& wkt )
53 {
54  bool result = false;
55  if ( wkt.size() )
56  {
57  std::istringstream is( wkt );
58  result = parseWktGroup( is, m_kwl );
59  }
60  return result;
61 }
62 
64 {
65  return m_kwl;
66 }
67 
69 {
70  bool result = false;
71 
72  if ( is.good() )
73  {
74  char c;
75 
76  // Get the wkt group name up to '[', e.g. "PROJCS[".
77  std::string prefix = "";
78  std::string object;
79  // std::string v;
80  while ( is.good() )
81  {
82  is.get(c);
83  if ( is.good() )
84  {
85  // Look for parens or square brackets.
86  if ( (c != '[') && (c != '(') )
87  {
88  object.push_back(c);
89  }
90  else
91  {
92  result = parseObject( is, prefix, object, kwl );
93  }
94  }
95  else
96  {
97  break;
98  }
99  }
100  }
101 
102  return result;
103 }
104 
106  const std::string& prefix,
107  const std::string& object,
108  ossimKeywordlist& kwl )
109 {
110  bool result = false;
111 
112  result = parseName( is, prefix, object, kwl );
113 
114  if ( result && is.good() )
115  {
116  char c;
117  ossim_uint32 myObjectIndex = 0;
118  ossim_uint32 paramIndex = 0;
119  while ( is.good() )
120  {
121  is.get(c);
122  if ( is.good() )
123  {
124  if ( c == ',' )
125  {
126  parseParam( is, prefix, object, myObjectIndex, paramIndex, kwl );
127  }
128  else if ( (c == ']') || (c == ')') )
129  {
130  break; // End of object.
131  }
132  }
133  }
134 
135  }
136 
137  return result;
138 }
139 
141  const std::string& prefix,
142  const std::string& object,
143  ossimKeywordlist& kwl )
144 {
145  bool result = false;
146  char c;
147  std::string name;
148 
149  // Find the first quote:
150  while ( is.good() )
151  {
152  is.get(c);
153  if ( is.good() )
154  {
155  if ( c == '"' )
156  {
157  break;
158  }
159  }
160  }
161 
162  // Get the name:
163  while ( is.good() )
164  {
165  is.get(c);
166  if ( is.good() )
167  {
168  if ( c != '"' )
169  {
170  name.push_back(c);
171  }
172  else
173  {
174  break; // End quote:
175  }
176  }
177  }
178 
179  if ( name.size() )
180  {
181  // Add to keyword list.
182  std::string key;
183  if ( prefix.size() )
184  {
185  key += prefix;
186  }
187  key += object;
188  key += ".name";
189  kwl.addPair( key, name );
190  result = true;
191  }
192 
193  return result;
194 }
195 
197  const std::string& prefix,
198  const std::string& object,
199  ossim_uint32& objectIndex,
200  ossim_uint32& paramIndex,
201  ossimKeywordlist& kwl )
202 {
203  bool result = false;
204  char c;
205  std::string name;
206 
207  // Get the name:
208  while ( is.good() )
209  {
210  int i = is.peek(); // Don't gobble the trailing comma or bracket.
211 
212  if ( (i == ',') || (i == ']') || (i == ')') )
213  {
214  // End of param.
215  if ( name.size() )
216  {
217  // Add to keyword list.
218  std::string key;
219  if ( prefix.size() )
220  {
221  key += prefix;
222  }
223  key += object;
224  key += ".param";
225  key += ossimString::toString(paramIndex).string();
226  kwl.addPair( key, name );
227  name = "";
228  ++paramIndex;
229  result = true;
230  }
231 
232  break; // Next param or at end of object.
233  }
234 
235  is.get(c);
236  if ( is.good() )
237  {
238  // Look nexted object.
239  if ( (c == '[') || (c == '(') )
240  {
241  std::string myPrefix;
242  if ( prefix.size() )
243  {
244  myPrefix += prefix;
245  }
246 
247  myPrefix += object;
248 
249  myPrefix += ".";
250 
251  //---
252  // Special hack for duplicated keyword "PARAMETER"
253  //---
254  if ( name == "PARAMETER" )
255  {
256  name += ossimString::toString(objectIndex).string();
257  ++objectIndex;
258  }
259 
260  result = parseObject( is, myPrefix, name, kwl );
261 
262  name = "";
263  }
264  else
265  {
266  name.push_back(c);
267  }
268  }
269  }
270 
271  return result;
272 }
273 
Represents serializable keyword/value map.
static ossimString toString(bool aValue)
Numeric to string methods.
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
ossimWkt()
default constructor
Definition: ossimWkt.cpp:43
unsigned int ossim_uint32
bool parseParam(std::istringstream &is, const std::string &prefix, const std::string &object, ossim_uint32 &objectIndex, ossim_uint32 &paramIndex, ossimKeywordlist &kwl)
Definition: ossimWkt.cpp:196
~ossimWkt()
destructor
Definition: ossimWkt.cpp:48
bool parseName(std::istringstream &is, const std::string &prefix, const std::string &object, ossimKeywordlist &kwl)
Definition: ossimWkt.cpp:140
bool parseObject(std::istringstream &is, const std::string &prefix, const std::string &object, ossimKeywordlist &kwl)
Definition: ossimWkt.cpp:105
bool parseWktGroup(std::istringstream &is, ossimKeywordlist &kwl)
Definition: ossimWkt.cpp:68
ossimKeywordlist m_kwl
Definition: ossimWkt.h:107
std::basic_istringstream< char > istringstream
Class for char input memory streams.
Definition: ossimIosFwd.h:32
bool parse(const std::string &wkt)
Parses string to keyword list.
Definition: ossimWkt.cpp:52
const ossimKeywordlist & getKwl() const
Definition: ossimWkt.cpp:63
const std::string & string() const
Definition: ossimString.h:414