OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimLandsatTileSource.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts
6 //
7 // Description:
8 //
9 // Contains class implementaiton for the class "ossim LandsatTileSource".
10 //
11 //*******************************************************************
12 // $Id: ossimLandsatTileSource.cpp 21512 2012-08-22 11:53:57Z dburken $
13 
16 #include <ossim/base/ossimTrace.h>
25 
27  "ossimLandsatTileSource",
29 
30 static ossimTrace traceDebug("ossimLandsatTileSource:debug");
31 
32 
33 //*******************************************************************
34 // Public Constructor:
35 //*******************************************************************
37  :
39  theFfHdr(NULL)
40 {
41 }
42 
43 //*******************************************************************
44 // Public Constructor:
45 //*******************************************************************
47  const char* prefix)
48  :
50  theFfHdr(NULL)
51 {
52  if (loadState(kwl, prefix) == false)
53  {
55  }
56 }
57 
58 //*******************************************************************
59 // Destructor:
60 //*******************************************************************
62 {
63  theFfHdr = NULL;
64 }
65 
67 {
68  static const char MODULE[] = "ossimLandsatTileSource::open";
69 
70  if (traceDebug())
71  {
72  CLOG << " Entered..." << std::endl
73  << " trying to open file " << theImageFile << std::endl;
74  }
75 
76  ossimFilename tempFilename = theImageFile;
77  // See if the file passed in is a header file.
78 
80 
81  if (!theFfHdr) return false;
82 
83  // Start building the keyword list for the general raster base class.
84  ossimKeywordlist kwl;
85 
86  //***
87  // There can be up to seven (six for L7) files that belong to the header.
88  // Note that it seems the file names in the header are always upper case.
89  // So test the file given to us to see if they should be downcased. This
90  // is assuming that all files in the directory have the same case.
91  //***
92  vector<ossimFilename> fileList;
93 
94  for (ossim_uint32 i=0; i<theFfHdr->getBandCount(); ++i)
95  {
96  bool addFile = false;
98  if (f1.trim() != "")
99  {
100  // Make the file name.
102  f2 = f2.dirCat(f1);
103 
104  if (f2.exists())
105  {
106  addFile = true;
107  }
108  else
109  {
110  // Try it downcased...
111  f2 = theImageFile.path();
112  f1.downcase();
113  f2 = f2.dirCat(f1);
114  if (f2.exists())
115  {
116  addFile = true;
117  }
118  else
119  {
120  // Try is upcased...
121  f2 = theImageFile.path();
122  f1.upcase();
123  f2 = f2.dirCat(f1);
124  if (f2.exists())
125  {
126  addFile = true;
127  }
128  }
129  }
130 
131  if (addFile)
132  {
133  if (traceDebug())
134  {
135  CLOG << "\nAdding file: " << f2 << std::endl;
136  }
137  fileList.push_back(f2);
138  }
139  else
140  {
141  if (traceDebug())
142  {
143  f2 = theImageFile.path();
144  f1 = theFfHdr->getBandFilename(i);
145  f1.trim();
146  f2 = f2.dirCat(f1);
147  CLOG << "\nCould not find: " << f2 << std::endl;
148  }
149  }
150  }
151  }
152 
153  if(fileList.size() == 0)
154  {
155  close();
156  return false;
157  }
158 
159  ossimGeneralRasterInfo generalRasterInfo(fileList,
160  OSSIM_UINT8,
162  (ossim_uint32)fileList.size(),
165  0,
167  0);
168  if(fileList.size() == 1)
169  {
170  generalRasterInfo = ossimGeneralRasterInfo(fileList,
171  OSSIM_UINT8,
172  OSSIM_BSQ,
173  (ossim_uint32)fileList.size(),
176  0,
178  0);
179  }
180  theMetaData.clear();
182  theMetaData.setNumberOfBands((ossim_uint32)fileList.size());
183  m_rasterInfo = generalRasterInfo;
184  if(initializeHandler())
185  {
186  theImageFile = tempFilename;
187 
188  completeOpen();
189  }
190  else
191  {
192  if (traceDebug()) CLOG << " Exited..." << std::endl;
193  return false;
194  }
195 
196  if (traceDebug()) CLOG << " Exited..." << std::endl;
197 
198  return true;
199 }
200 
202 {
203  //***
204  // Landsat file name example: l71024031_03119990929_hpn.fst
205  // Three header header file type substrings:
206  // HPN = Pan
207  // HRF = VNIR/SWIR (visible near infrared/shortwave infrared)
208  // HTM = Thermal
209  //***
210  ossimFilename hdr = file.file();
211  hdr.downcase();
212  theFfHdr = 0;
213  if ( hdr.contains("hpn") || hdr.contains("hrf") || hdr.contains("htm") )
214  {
215  theFfHdr = new ossimFfL7(file.c_str());
216  }
217  else if (hdr.contains("header.dat"))
218  {
219  theFfHdr = new ossimFfL5(file.c_str());
220  }
221  else
222  {
223  theFfHdr = 0;
224  return;
225  }
227  {
228  theFfHdr = 0;
229  }
230  return;
231 
232  // I had to force the open to go with a header since there are duplicate entries when scanning
233  // landsat directories.
234  // For now I am commenting this code out.
235  //
236 #if 0
237  //***
238  // User may have passed in an image file name in which case the header file
239  // must be derived from it.
240  //***
241  if (hdr.size() < 25)
242  {
243  // file name not long enough...
244  if (traceDebug())
245  {
247  << "ossimLandsatTileSource::openHeader DEBUG:"
248  << "\nNot a standard landsat 7 file name: " << hdr << std::endl;
249  return;
250  }
251  }
252 
253  char substr[4];
254  const char* f = hdr.c_str();
255  strncpy(substr, (f+22), 3);
256  substr[3] = '\0';
257  ossimString s1 = substr;
258  ossimString s2;
259  s1.downcase();
260  if (s1 == "b80")
261  {
262  s2 = "hpn";
263  }
264  else if (s1 == "b61" || s1 == "b62")
265  {
266  s2 = "htm";
267  }
268  else if (s1 == "b10" || s1 == "b20" || s1 == "b30" ||
269  s1 == "b40" || s1 == "b50" || s1 == "b70")
270  {
271  s2 = "hrf";
272  }
273  else
274  {
275  // Not of any format we know of...
276  if (traceDebug())
277  {
279  << "ossimLandsatTileSource::openHeader DEBUG:"
280  << "\nCould not derive header name from: " << file
281  << std::endl;
282  }
283 
284  return;
285  }
286 
287  // Set the case to be the same as the file passed in.
288  if (substr[0] == 0x42) // ascii "B"
289  {
290  s1.upcase();
291  s2.upcase();
292  hdr.upcase();
293 
294  // Header files alway start with "L71"
295  hdr = hdr.substitute(ossimString("L72"), ossimString("L71"));
296  }
297  else
298  {
299  // Header files alway start with "l71"
300  hdr = hdr.substitute(ossimString("l72"), ossimString("l71"));
301  }
302 
303  // Make the hdr file name.
304  hdr = hdr.substitute(s1, s2);
305 
306  ossimFilename f1 = file.drive();
307  f1 += file.path();
308  hdr = f1.dirCat(hdr);
309  theFfHdr = new ossimFfL7(hdr.c_str());
310 
312  {
313  delete theFfHdr;
314  theFfHdr = NULL;
315  }
316 #endif
317 }
318 
320 {
321  if ( !theGeometry )
322  {
323  // Check for external geom:
325 
326  if ( !theGeometry )
327  {
329 
330  if ( theFfHdr.valid() )
331  {
332  // Make a model
335  {
336  //initialize the image geometry object with the model
337  theGeometry->setProjection(model);
338  }
339  }
340 
341  //---
342  // WARNING:
343  // Must create/set the geometry at this point or the next call to
344  // ossimImageGeometryRegistry::extendGeometry will put us in an infinite loop
345  // as it does a recursive call back to ossimImageHandler::getImageGeometry().
346  //---
347 
348  // Check for set projection.
349  if ( !theGeometry->getProjection() )
350  {
351  // Try factories for projection.
353  }
354  }
355 
356  // Set image things the geometry object should know about.
358  }
359 
360  return theGeometry;
361 }
362 
364  const char* prefix)
365 {
366  const char* lookup = kwl.find(prefix, ossimKeywordNames::FILENAME_KW);
367  if (lookup)
368  {
369  ossimFilename fileName = lookup;
370 
371  ossimString ext = fileName.ext();
372 
373  if((ext.upcase() == "FST") || (ext.upcase() == "DAT"))
374  {
375  //---
376  // This will call:
377  // ossimImageHandler::loadState() the open()
378  //---
380  {
381  return true;
382  }
383  }
384  }
385  return false;
386 }
387 
389  const ossimString& name)const
390 {
391  ossimRefPtr<ossimProperty> result = 0;
392 
393  if(name == "file_type")
394  {
395  if(ossimString(getFilename()).downcase().contains("header"))
396  {
397  return (new ossimStringProperty(name, "landsat5"));
398  }
399  else
400  {
401  return (new ossimStringProperty(name, "landsat7"));
402  }
403  }
404  else if (theFfHdr.valid())
405  {
406  result = theFfHdr->getProperty(name);
407  }
408 
409  if ( result.valid() == false )
410  {
412  }
413 
414  return result;
415 }
416 
418  std::vector<ossimString>& propertyNames)const
419 {
420  propertyNames.push_back("file_type");
421  if (theFfHdr.valid())
422  {
423  theFfHdr->getPropertyNames(propertyNames);
424  }
426 }
427 
429 {
430  return ossimString("Landsat");
431 }
432 
434 {
435  return ossimString("Landsat reader");
436 }
437 
439 {
440  return 0.0;
441 }
442 
444 {
445  return 1.0;
446 }
447 
449 {
450  return 255.0;
451 }
452 
454 {
455  return OSSIM_UINT8;
456 }
457 
459 {
460  if(!theFfHdr) return false;
461 
463 
464  return true;
465 }
466 
468 {
469  if(!theFfHdr) return "";
470 
471  return theFfHdr->getSatelliteName();
472 }
473 
475 {
477 
478  if(!theFfHdr||(idx > maxIdx))
479  {
480  return "";
481  }
482 
483  ossimFilename path = getFilename().path();
484  ossimString filename = theFfHdr->getBandFilename(idx);
485  filename = filename.trim();
486  ossimFilename file = path.dirCat(filename);
487 
488  if (file.exists())
489  {
490  return file;
491  }
492 
493  // Try downcased name.
494  file = path.dirCat(filename.downcase());
495  if (file.exists())
496  {
497  return file;
498  }
499 
500  // Try upcase name.
501  file = path.dirCat(filename.upcase());
502  if (file.exists())
503  {
504  return file;
505  }
506 
507  return ossimFilename();
508 }
509 
511 {
512  return (getNumberOfInputBands() == 1);
513 }
514 
516 {
517  return (getNumberOfInputBands() == 6);
518 }
519 
521 {
522  return (getNumberOfInputBands() == 2);
523 }
524 
void setScalarType(ossimScalarType aType)
static ossimImageGeometryRegistry * instance()
ossimRefPtr< ossimImageGeometry > theGeometry
ossimString substitute(const ossimString &searchKey, const ossimString &replacementValue, bool replaceAll=false) const
Substitutes searchKey string with replacementValue and returns a string.
int getLinesPerBand() const
Definition: ossimFfL7.cpp:616
void setProjection(ossimProjection *projection)
Sets the projection to be used for local-to-world coordinate transformation.
static ossimString upcase(const ossimString &aString)
Definition: ossimString.cpp:34
#define CLOG
Definition: ossimTrace.h:23
ossimFilename theImageFile
virtual bool open()
Pure virtual open.
Represents serializable keyword/value map.
static const ossimErrorCode OSSIM_OK
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
virtual unsigned int getBandCount() const
Definition: ossimFfL7.cpp:690
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
Gets a property for matching name.
Definition: ossimFfL7.cpp:636
int getPixelsPerLine() const
Definition: ossimFfL7.cpp:621
bool contains(char aChar) const
Definition: ossimString.h:58
ossimString getSatelliteName() const
Definition: ossimFfL7.cpp:601
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
static const ossimErrorCode OSSIM_ERROR
virtual ossimString getShortName() const
virtual ossimRefPtr< ossimImageGeometry > getImageGeometry()
Returns the image geometry object associated with this tile source or NULL if non defined...
virtual ossimScalarType getOutputScalarType() const
Returns the output pixel type of the tile source.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
virtual double getNullPixelValue(ossim_uint32 band=0) const
Override base getXXXXPixValue methods since the null/min/max can be set to something different...
virtual bool extendGeometry(ossimImageHandler *handler) const
ossimFilename getBandFilename(ossim_uint32 idx) const
Definition: ossimFfL7.cpp:606
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
Gets a list of property names available.
Definition: ossimFfL7.cpp:680
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
void setNumberOfBands(ossim_uint32 numberOfBands)
#define RTTI_DEF1_INST(cls, name, b1)
Definition: ossimRtti.h:481
bool exists() const
virtual const ossimFilename & getFilename() const
Returns the filename.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=NULL)
Method to the load (recreate) the state of an object from a keyword list.
std::string::size_type size() const
Definition: ossimString.h:405
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossimRefPtr< ossimFfL7 > theFfHdr
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
void initImageParameters(ossimImageGeometry *geom) const
Convenience method to set things needed in the image geometry from the image handler.
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
virtual void close()
Deletes the overview and clears the valid image vertices.
ossimFilename getBandFilename(ossim_uint32 idx) const
Container class that holds both 2D transform and 3D projection information for an image Only one inst...
ossimScalarType
bool getAcquisitionDate(ossimDate &date) const
virtual ossimRefPtr< ossimImageGeometry > getExternalImageGeometry() const
Returns the image geometry object associated with this tile source or NULL if non defined...
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
virtual void getPropertyNames(std::vector< ossimString > &propertyNames) const
Gets a list of property names available.
const ossimProjection * getProjection() const
Access methods for projection (may be NULL pointer).
virtual void completeOpen()
Will complete the opening process.
ossimImageMetaData theMetaData
ossimString getSatelliteName() const
virtual ossimErrorCode getErrorStatus() const
virtual ossimRefPtr< ossimProperty > getProperty(const ossimString &name) const
Gets a property for matching name.
virtual ossimString getLongName() const
ossimFilename dirCat(const ossimFilename &file) const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
ossimFilename file() const
ossimString getAcquisitionDate() const
Definition: ossimFfL7.cpp:582
ossimString ext() const
8 bit unsigned integer
virtual double getMinPixelValue(ossim_uint32 band=0) const
Retuns the min pixel value.
static const char * FILENAME_KW
virtual ~ossimLandsatTileSource()
ossimFilename path() const
ossimFilename drive() const
void openHeader(const ossimFilename &file)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
virtual ossim_uint32 getNumberOfInputBands() const
Returns the number of bands in the image.