OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimIkonosMetaData.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 // Description:
8 //
9 // Class definition for ossimIkonosMetaData.
10 //
11 // This class parses a Space Imaging Ikonos meta data file.
12 //
13 //********************************************************************
14 // $Id: ossimIkonosMetaData.cpp 17206 2010-04-25 23:20:40Z dburken $
15 
16 #include <cstdio>
17 #include <iostream>
20 #include <ossim/base/ossimNotify.h>
22 #include <ossim/base/ossimTrace.h>
23 
24 RTTI_DEF1(ossimIkonosMetaData, "ossimIkonosMetaData", ossimObject);
25 
26 // Define Trace flags for use within this file:
27 static ossimTrace traceExec ("ossimIkonosMetaData:exec");
28 static ossimTrace traceDebug ("ossimIkonosMetaData:debug");
29 
31  :
32  theNominalCollectionAzimuth(0.0),
33  theNominalCollectionElevation(0.0),
34  theSunAzimuth(0.0),
35  theSunElevation(0.0),
36  theNumBands(0),
37  theBandName("Unknown"),
38  theProductionDate("Unknown"),
39  theAcquisitionDate("Unknown"),
40  theAcquisitionTime("Unknown"),
41  theSensorID("Unknown")
42 {
43 }
44 
46 {
47 }
48 
50 {
51  static const char MODULE[] = "ossimIkonosMetaData::open";
52 
53  clearFields();
54 
55  //retrieve information from the metadata file
56  //if the ikonos tif is po_2619900_pan_0000000.tif
57  //the metadata file will be po_2619900_metadata.txt
58 
59  ossimString separator("_");
60  ossimString filenamebase = imageFile.fileNoExtension();
61  std::vector< ossimString > filenameparts = filenamebase.split(separator);
62 
63  if(filenameparts.size() < 2)
64  {
65  if(traceDebug())
66  {
68  << MODULE << " Ikonos filename non standard" << std::endl;
69  }
70  return false;
71  }
72  ossimFilename metadatafile = filenameparts[0];
73  metadatafile += "_";
74  metadatafile += filenameparts[1];
75  metadatafile += "_metadata.txt";
76 
77  metadatafile.setPath(imageFile.path());
78 
79  if( parseMetaData(metadatafile) == false )
80  {
81  if(traceDebug())
82  {
84  << MODULE << " errors parsing metadata" << std::endl;
85  }
86  return false;
87  }
88 
89  ossimFilename hdrfile = imageFile;
90  hdrfile.setExtension(ossimString("hdr"));
91  if( parseHdrData(hdrfile) == false )
92  {
93  if(traceDebug())
94  {
96  << MODULE << " errors parsing hdr" << std::endl;
97  }
98  return false;
99  }
100 
101  ossimFilename rpcfile = imageFile.noExtension();
102  rpcfile += "_rpc.txt";
103  if (parseRpcData (rpcfile) == false)
104  {
105  if(traceDebug())
106  {
108  << MODULE << " errors parsing rpc" << std::endl;
109  }
110  return false;
111  }
112 
113  return true;
114 }
115 
117 {
120  theSunAzimuth = 0.0;
121  theSunElevation = 0.0;
122  theNumBands = 0;
123  theBandName = "Unknown";
124  theProductionDate = "Unknown";
125  theAcquisitionDate = "Unknown";
126  theAcquisitionTime = "Unknown";
127  theSensorID = "Unknown";
128 }
129 
131 {
132 
133  out << "\n----------------- Info on Ikonos Image -------------------"
134  << "\n "
135  << "\n Nominal Azimuth: " << theNominalCollectionAzimuth
136  << "\n Nominal Elevation: " << theNominalCollectionElevation
137  << "\n Sun Azimuth: " << theSunAzimuth
138  << "\n Sun Elevation: " << theSunElevation
139  << "\n Number of bands: " << theNumBands
140  << "\n Band name: " << theBandName
141  << "\n Production date: " << theProductionDate
142  << "\n Acquisition date: " << theAcquisitionDate
143  << "\n Acquisition time: " << theAcquisitionTime
144  << "\n Sensor Type: " << theSensorID
145  << "\n"
146  << "\n---------------------------------------------------------"
147  << "\n " << std::endl;
148  return out;
149 }
150 
152 {
153  return theSensorID;
154 }
155 
157  const char* prefix)const
158 {
159 
160  kwl.add(prefix,
162  "ossimIkonosMetaData",
163  true);
164 
165  kwl.add(prefix,
166  "nominal_collection_azimuth_angle",
168  true);
169 
170  kwl.add(prefix,
171  "nominal_collection_elevation_angle",
173  true);
174 
175  kwl.add(prefix,
178  true);
179 
180  kwl.add(prefix,
183  true);
184 
185  kwl.add(prefix,
187  theNumBands,
188  true);
189 
190  kwl.add(prefix,
191  "band_name",
192  theBandName,
193  true);
194 
195  kwl.add(prefix,
196  "production_date",
198  true);
199 
200  kwl.add(prefix,
201  "acquisition_date",
203  true);
204 
205  kwl.add(prefix,
206  "acquisition_time",
208  true);
209 
210  kwl.add(prefix,
211  "sensor",
212  theSensorID,
213  true);
214 
215  return true;
216 }
217 
219  const char* prefix)
220 {
221  clearFields();
222 
223  const char* lookup = 0;
224  ossimString s;
225 
226  lookup = kwl.find(prefix, ossimKeywordNames::TYPE_KW);
227  if (lookup)
228  {
229  s = lookup;
230  if(s != "ossimIkonosMetaData")
231  {
232  return false;
233  }
234  }
235 
236  lookup = kwl.find(prefix, "nominal_collection_azimuth_angle");
237  if (lookup)
238  {
239  s = lookup;
241  }
242 
243  lookup = kwl.find(prefix, "nominal_collection_elevation_angle");
244  if (lookup)
245  {
246  s = lookup;
248  }
249 
250  lookup = kwl.find(prefix, ossimKeywordNames::AZIMUTH_ANGLE_KW);
251  if (lookup)
252  {
253  s = lookup;
254  theSunAzimuth = s.toFloat64();
255  }
256 
257  lookup = kwl.find(prefix, ossimKeywordNames::ELEVATION_ANGLE_KW);
258  if (lookup)
259  {
260  s = lookup;
262  }
263 
264  lookup = kwl.find(prefix, ossimKeywordNames::NUMBER_BANDS_KW);
265  if (lookup)
266  {
267  s = lookup;
268  theNumBands = s.toUInt32();
269  }
270 
271  lookup = kwl.find(prefix, "band_name");
272  if (lookup)
273  {
274  theBandName = lookup;
275  }
276 
277  lookup = kwl.find(prefix, "production_date");
278  if (lookup)
279  {
280  theProductionDate = lookup;
281  }
282 
283  lookup = kwl.find(prefix, "acquisition_date");
284  if (lookup)
285  {
286  theAcquisitionDate = lookup;
287  }
288 
289  lookup = kwl.find(prefix, "acquisition_time");
290  if (lookup)
291  {
292  theAcquisitionTime = lookup;
293  }
294 
295  lookup = kwl.find(prefix, "sensor");
296  if (lookup)
297  {
298  theSensorID = lookup;
299  }
300 
301  return true;
302 }
303 
304 //*****************************************************************************
305 // PROTECTED METHOD: ossimIkonosMetaData::parseMetaData()
306 //
307 // Parses the Ikonos metadata file.
308 //
309 //*****************************************************************************
311 {
312  if (traceExec())
313  {
315  << "DEBUG ossimIkonosRpcModel::parseMetaData(data_file): entering..."
316  << std::endl;
317  }
318 
319  FILE* fptr = fopen (data_file, "r");
320  if (!fptr)
321  {
322  if (traceDebug())
323  {
325  << "ossimIkonosRpcModel::parseMetaData(data_file) DEBUG:"
326  << "\nCould not open Meta data file: " << data_file
327  << "\nreturning with error..." << std::endl;
328  }
329  return false;
330  }
331 
332  char* strptr;
333  char dummy[80], name[80];
334  double value;
335 
336  //---
337  // Read the file into a buffer:
338  //---
339  ossim_int32 fileSize = static_cast<ossim_int32>(data_file.fileSize());
340  char* filebuf = new char[fileSize];
341  fread(filebuf, 1, fileSize, fptr);
342  fclose(fptr);
343 
344  //---
345  // Production date:
346  //---
347  strptr = strstr(filebuf, "\nCreation Date:");
348  if (!strptr)
349  {
350  if(traceDebug())
351  {
353  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
354  << "\n\tAborting construction. Error encountered parsing "
355  << "presumed meta-data file." << std::endl;
356  }
357  delete [] filebuf;
358  return false;
359  }
360 
361  sscanf(strptr, "%15c %s", dummy, name);
362  theProductionDate = name;
363 
364  //***
365  // Sensor Type:
366  //***
367  strptr = strstr(strptr, "\nSensor:");
368  if (!strptr)
369  {
370  if(traceDebug())
371  {
373  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
374  << "\n\tAborting construction. Error encountered parsing "
375  << "presumed meta-data file." << std::endl;
376  }
377  delete [] filebuf;
378  return false;
379  }
380 
381  sscanf(strptr, "%8c %s", dummy, name);
382  theSensorID = name;
383 
384 
385  //***
386  // Nominal Azimuth:
387  //***
388  strptr = strstr(strptr, "\nNominal Collection Azimuth:");
389  if (!strptr)
390  {
391  if(traceDebug())
392  {
394  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
395  << "\n\tAborting construction. Error encountered parsing "
396  << "presumed meta-data file." << std::endl;
397  }
398  delete [] filebuf;
399  return false;
400  }
401 
402  sscanf(strptr, "%28c %lf %s", dummy, &value, dummy);
404 
405  //***
406  // Nominal Elevation:
407  //***
408  strptr = strstr(strptr, "\nNominal Collection Elevation:");
409  if (!strptr)
410  {
411  if(traceDebug())
412  {
414  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
415  << "\n\tAborting construction. Error encountered parsing "
416  << "presumed meta-data file." << std::endl;
417  }
418  delete [] filebuf;
419  return false;
420  }
421 
422  sscanf(strptr, "%31c %lf %s", dummy, &value, dummy);
424 
425  //***
426  // Sun Azimuth:
427  //***
428  strptr = strstr(strptr, "\nSun Angle Azimuth:");
429  if (!strptr)
430  {
431  if(traceDebug())
432  {
434  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
435  << "\n\tAborting construction. Error encountered parsing "
436  << "presumed meta-data file." << std::endl;
437  }
438  delete [] filebuf;
439  return false;
440  }
441 
442  sscanf(strptr, "%19c %lf %s", dummy, &value, dummy);
443  theSunAzimuth = value;
444 
445  //***
446  // Sun Elevation:
447  //***
448  strptr = strstr(strptr, "\nSun Angle Elevation:");
449  if (!strptr)
450  {
451  if(traceDebug())
452  {
454  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
455  << "\n\tAborting construction. Error encountered parsing "
456  << "presumed meta-data file." << std::endl;
457  }
458  delete [] filebuf;
459  return false;
460  }
461 
462  sscanf(strptr, "%21c %lf %s", dummy, &value, name);
463  theSunElevation = value;
464 
465  //---
466  // Acquisition date and time:
467  //---
468  strptr = strstr(filebuf, "\nAcquisition Date/Time:");
469  if (!strptr)
470  {
471  if(traceDebug())
472  {
474  << "FATAL ossimIkonosRpcModel::parseMetaData(data_file): "
475  << "\n\tAborting construction. Error encountered parsing "
476  << "presumed meta-data file." << std::endl;
477  }
478  delete [] filebuf;
479  return false;
480  }
481 
482  char name2[80];
483  sscanf(strptr, "%23c %s %s", dummy, name, name2);
484  theAcquisitionDate = name;
485  theAcquisitionTime = name2;
486 
487  delete [] filebuf;
488  filebuf = 0;
489 
490  if (traceExec())
491  {
493  << "DEBUG ossimIkonosRpcModel::parseMetaData(data_file): returning..."
494  << std::endl;
495  }
496 
497  return true;
498 }
499 
500 //*****************************************************************************
501 // PROTECTED METHOD: ossimIkonosMetaData::parseHdrData()
502 //
503 // Parses the Ikonos hdr file.
504 //
505 //*****************************************************************************
507 {
508  if (traceExec())
509  {
511  << "DEBUG ossimIkonosRpcModel::parseHdrData(data_file): entering..."
512  << std::endl;
513  }
514 
515  FILE* fptr = fopen (data_file, "r");
516  if (!fptr)
517  {
518  if (traceDebug())
519  {
521  << "ossimIkonosRpcModel::parseHdrData(data_file) WARN:"
522  << "\nCould not open hdr data file <" << data_file << ">. "
523  << "returning with error..." << std::endl;
524  }
525  return false;
526  }
527 
528  char* strptr;
529  // char linebuf[80];
530  char dummy[80];
531  char name[80];
532  int value=0;
533 
534  //***
535  // Read the file into a buffer:
536  //***
537  char filebuf[5000];
538  fread(filebuf, 1, 5000, fptr);
539  fclose(fptr);
540 
541  //***
542  // Band name:
543  //***
544  strptr = strstr(filebuf, "\nBand:");
545  if (!strptr)
546  {
547  if(traceDebug())
548  {
550  << "ossimIkonosRpcModel::parseHdrData(data_file):"
551  << "\n\tAborting construction. Error encountered parsing "
552  << "presumed hdr file." << std::endl;
553  }
554 
555  return false;
556  }
557 
558  sscanf(strptr, "%6c %s", dummy, name);
559  theBandName = name;
560 
561  //***
562  // Number of Bands:
563  //***
564  strptr = strstr(filebuf, "\nNumber of Bands:");
565  if (!strptr)
566  {
567  if(traceDebug())
568  {
570  << "ossimIkonosRpcModel::parseHdrData(data_file):"
571  << "\n\tAborting construction. Error encountered parsing "
572  << "presumed hdr file." << std::endl;
573  }
574 
575  return false;
576  }
577 
578  sscanf(strptr, "%17c %d", dummy, &value);
579  theNumBands = value;
580 
581  if (traceExec())
582  {
584  << "DEBUG ossimIkonosRpcModel::parseHdrData(data_file): returning..."
585  << std::endl;
586  }
587  return true;
588 }
589 
590 //*****************************************************************************
591 // PROTECTED METHOD: ossimIkonosMetaData::parseRpcData()
592 //
593 // Parses the Ikonos rpc file.
594 //
595 //*****************************************************************************
597 {
598  return true;
599 }
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of the object to a keyword list.
ossimFilename noExtension() const
std::basic_filebuf< char > filebuf
Class for char file buffers.
Definition: ossimIosFwd.h:41
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of the object from a keyword list.
virtual std::ostream & print(std::ostream &out) const
Generic print method.
Represents serializable keyword/value map.
const char * find(const char *key) const
ossim_int64 fileSize() const
ossimFilename & setPath(const ossimString &p)
void split(std::vector< ossimString > &result, const ossimString &separatorList, bool skipBlankFields=false) const
Splits this string into a vector of strings (fields) using the delimiter list specified.
ossim_uint32 toUInt32() const
static const char * TYPE_KW
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
RTTI_DEF1(ossimIkonosMetaData, "ossimIkonosMetaData", ossimObject)
ossimIkonosMetaData()
default constructor
bool parseRpcData(const ossimFilename &data_file)
Method to parse Ikonos rpc file.
ossim_float64 theNominalCollectionElevation
ossim_float64 toFloat64() const
virtual ~ossimIkonosMetaData()
virtual destructor
ossim_float64 theSunElevation
bool open(const ossimFilename &imageFile)
Open method that takes the image file, derives the metadata, header and rpc files, then calls parse methods parseMetaData, parseHdrData, and parseRpcData.
static const char * NUMBER_BANDS_KW
static const char * AZIMUTH_ANGLE_KW
bool parseMetaData(const ossimFilename &metadata)
Method to parse Ikonos metadata file.
ossim_float64 theNominalCollectionAzimuth
ossimFilename fileNoExtension() const
static const char * ELEVATION_ANGLE_KW
ossimString getSensorID() const
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
ossimFilename path() const
bool parseHdrData(const ossimFilename &data_file)
Method to parse Ikonos header file.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32