OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimHdfReader.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: Class definition for HDF reader.
8 //
9 //----------------------------------------------------------------------------
10 // $Id: ossimHdfReader.cpp 2645 2011-05-26 15:21:34Z oscar.kramer $
11 
12 //Std includes
13 #include <set>
14 
15 //ossim includes
16 #include <ossimHdfReader.h>
18 #include <ossim/base/ossimTrace.h>
22 
23 #ifdef OSSIM_ID_ENABLED
24 static const char OSSIM_ID[] = "$Id";
25 #endif
26 
27 static ossimTrace traceDebug("ossimHdfReader:debug");
28 
30 
32 {
33  return (fabs(left - right) < epsilon);
34 }
35 
38  m_gdalTileSource(0),
39  m_entryFileList(),
40  m_numberOfBands(0),
41  m_scalarType(OSSIM_SCALAR_UNKNOWN),
42  m_currentEntryRender(0),
43  m_tile(0)
44 {
45 }
46 
48 {
49  close();
51 }
52 
54 {
55  return ossimString("ossim_hdf_reader");
56 }
57 
59 {
60  return ossimString("ossim hdf reader");
61 }
62 
64 {
65  return ossimString("ossimHdfReader");
66 }
67 
69  ossim_uint32 resLevel) const
70 {
71  ossim_uint32 result = 0;
72  if (resLevel == 0)
73  {
74  if (m_gdalTileSource.valid())
75  {
76  result = m_gdalTileSource->getNumberOfLines(resLevel);
77  }
78  }
79  else if (theOverview.valid())
80  {
81  result = theOverview->getNumberOfSamples(resLevel);
82  }
83  return result;
84 }
85 
87  ossim_uint32 resLevel) const
88 {
89  ossim_uint32 result = 0;
90  if (resLevel == 0)
91  {
92  if (m_gdalTileSource.valid())
93  {
94  result = m_gdalTileSource->getNumberOfSamples(resLevel);
95  }
96  }
97  else if (theOverview.valid())
98  {
99  result = theOverview->getNumberOfSamples(resLevel);
100  }
101  return result;
102 }
103 
105 {
106  static const char MODULE[] = "ossimHdfReader::open";
107 
108  if (traceDebug())
109  {
111  << MODULE << " entered...\n"
112  << "image: " << theImageFile << "\n";
113  }
114 
115  bool result = false;
116 
117  if (!isSupportedExtension())
118  {
119  return false;
120  }
121 
122  if (m_entryFileList.size() == 0)
123  {
124  if(isOpen())
125  {
126  close();
127  }
128 
131 
132  if ( m_gdalTileSource->open() == false )
133  {
134  m_gdalTileSource = 0;
135  return false;
136  }
137 
138  std::vector<ossimString> entryStringList;
139  if (m_gdalTileSource )
140  {
141  m_gdalTileSource->getEntryNames(entryStringList);
142  }
143 
144  // bool isSwathImage = false;
145  if (entryStringList.size() > 0)
146  {
147  for (ossim_uint32 i = 0; i < entryStringList.size(); i++)
148  {
149  m_entryFileList.push_back(i);
150  }
151  }
152  else
153  {
154  result = false;
155  }
156  }
157 
158  //set entry id for the gdal tile source
160  {
161 
164 
166  m_tile->initialize();
167 
168  completeOpen();
169  result = true;
170  }
171  else
172  {
173  result = false;
174  }
175 
176  if (result == false)
177  {
178  close();
179  }
180 
181  if (traceDebug())
182  {
184  << MODULE << " exit status = " << (result?"true":"false\n")
185  << std::endl;
186  }
187 
188  return result;
189 }
190 
192 {
193  std::vector<ossimString> fileList = fileName.split(":");
194  if (fileList.size() > 0)
195  {
196  ossimString subName = fileList[0];
197  if (subName.contains("_SDS"))
198  {
199  return true;
200  }
201  }
202  return false;
203 }
204 
206 {
207  return m_gdalTileSource.get();
208 }
209 
211 {
212  m_tile = 0;
213 
214  if (m_gdalTileSource.valid())
215  {
216  m_gdalTileSource = 0;
217  }
218  m_entryFileList.clear();
219 }
220 
222  ossim_uint32 resLevel)
223 {
224  if (m_tile.valid())
225  {
226  // Image rectangle must be set prior to calling getTile.
227  m_tile->setImageRectangle(tile_rect);
228 
229  if ( getTile( m_tile.get(), resLevel ) == false )
230  {
232  {
233  m_tile->makeBlank();
234  }
235  }
236  }
237 
238  return m_tile;
239 }
240 
242  ossim_uint32 resLevel)
243 {
244  bool status = false;
245 
246  //---
247  // Not open, this tile source bypassed, or invalid res level,
248  // return a blank tile.
249  //---
250  if( isOpen() && isSourceEnabled() && isValidRLevel(resLevel) &&
251  result && (result->getNumberOfBands() == getNumberOfOutputBands()) )
252  {
253  result->ref(); // Increment ref count.
254 
255  //---
256  // Check for overview tile. Some overviews can contain r0 so always
257  // call even if resLevel is 0. Method returns true on success, false
258  // on error.
259  //---
260  status = getOverviewTile(resLevel, result);
261 
262  if (!status) // Did not get an overview tile.
263  {
264  status = true;
265 
266  ossimIrect tile_rect = result->getImageRectangle();
267 
268  if (getImageRectangle().intersects(tile_rect))
269  {
270  // Make a clip rect.
271  ossimIrect clipRect = tile_rect.clipToRect(getImageRectangle());
272 
273  if (tile_rect.completely_within(clipRect) == false)
274  {
275  // Not filling whole tile so blank it out first.
276  result->makeBlank();
277  }
278 
279  if (m_gdalTileSource.valid())
280  {
281  ossimRefPtr<ossimImageData> imageData =
282  m_gdalTileSource->getTile(tile_rect, resLevel);
283  result->loadTile(imageData->getBuf(), tile_rect, clipRect, OSSIM_BSQ);
284  }
285 
286  }
287  else // No intersection...
288  {
289  result->makeBlank();
290  }
291  }
292  result->validate();
293 
294  result->unref(); // Decrement ref count.
295  }
296 
297  return status;
298 }
299 
301 {
302  ossimString ext = theImageFile.ext();
303  ext.downcase();
304 
305  if ( ext == "hdf" || ext == "h4" || ext == "hdf4" ||
306  ext == "he4" || ext == "hdf5" || ext == "he5" || ext == "h5" ||
307  ext == "l1r")
308  {
309  return true;
310  }
311  return false;
312 }
313 
315 {
316  return m_numberOfBands;
317 }
318 
320 {
321  return m_numberOfBands;
322 }
323 
325 {
326  ossim_uint32 result = 128;
327  if (m_gdalTileSource.valid())
328  {
330  }
331  return result;
332 }
333 
335 {
336  ossim_uint32 result = 128;
337  if (m_gdalTileSource.valid())
338  {
340  }
341  return result;
342 }
343 
345 {
346  if (m_gdalTileSource.valid())
347  {
349  }
350  return m_scalarType;
351 }
352 
354 {
355  if ( !theGeometry )
356  {
357  // Check the internal geometry first to avoid a factory call.
358  if ( m_gdalTileSource.valid() )
359  {
361  }
362  }
363  return theGeometry;
364 }
365 
366 bool ossimHdfReader::loadState(const ossimKeywordlist& kwl, const char* prefix)
367 {
368  bool result = false;
369  if ( ossimImageHandler::loadState(kwl, prefix) )
370  {
371  result = open();
372  }
373  return result;
374 }
375 
377 {
378  if (m_currentEntryRender == entryIdx)
379  {
380  return true; // Nothing to do...
381  }
382  theDecimationFactors.clear();
383  theGeometry = 0;
384  theOverview = 0;
386  m_currentEntryRender = entryIdx;
387  return open();
388 }
389 
391 {
392  return m_entryFileList.size();
393 }
394 
395 void ossimHdfReader::getEntryList(std::vector<ossim_uint32>& entryList) const
396 {
397  entryList.clear();
398  for (ossim_uint32 i = 0; i < m_entryFileList.size(); i++)
399  {
400  entryList.push_back(m_entryFileList[i]);
401  }
402 }
403 
405 {
406  if (m_gdalTileSource.valid())
407  {
408  std::vector<ossimString> entryStringList;
409  m_gdalTileSource->getEntryNames(entryStringList);
410  if (entryId < entryStringList.size())
411  {
412  return entryStringList[entryId];
413  }
414  }
415  return "";
416 }
417 
419 {
420  ossimString result = "";
421  if (m_gdalTileSource.valid())
422  {
423  GDALDriverH driver = m_gdalTileSource->getDriver();
424  if (driver)
425  {
426  result = GDALGetDriverShortName(driver);
427  }
428  }
429  return result;
430 }
431 
432 bool ossimHdfReader::setOutputBandList(const vector<ossim_uint32>& outputBandList)
433 {
434  if (outputBandList.size() && m_gdalTileSource )
435  {
436  m_gdalTileSource->setOutputBandList(outputBandList);
437  open();
438  return true;
439  }
440  return false;
441 }
bool isSDSDataset(ossimString fileName)
void clear()
Erases the entire container.
Definition: ossimString.h:432
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
virtual ossimString getShortName() const
Returns short name.
ossimRefPtr< ossimImageGeometry > theGeometry
virtual ossimRefPtr< ossimImageGeometry > getImageGeometry()
Returns the image geometry object associated with this tile source or NULL if non defined...
bool doubleEquals(ossim_float64 left, ossim_float64 right, ossim_float64 epsilon)
virtual ossimString getClassName() const
Returns class name.
virtual ossim_uint32 getNumberOfBands() const
virtual void getEntryNames(std::vector< ossimString > &entryStringList) const
ossimFilename theImageFile
virtual void setImageRectangle(const ossimIrect &rect)
virtual ossimString getLongName() const
Returns long name.
ossimRefPtr< ossimImageData > m_tile
Represents serializable keyword/value map.
bool valid() const
Definition: ossimRefPtr.h:75
std::vector< ossimDpt > theDecimationFactors
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 bool setOutputBandList(const std::vector< ossim_uint32 > &band_list)
If the image handler "isBandSeletor()" then the band selection of the output chip can be controlled...
virtual ossim_uint32 getNumberOfEntries() const
bool contains(char aChar) const
Definition: ossimString.h:58
virtual ossim_uint32 getImageTileWidth() const
Returns the tile width of the image or 0 if the image is not tiled.
virtual ossim_uint32 getNumberOfLines(ossim_uint32 resLevel=0) const
Gets number of lines for res level.
virtual bool open()
Open method.
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.
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getNumberOfLines(ossim_uint32 reduced_res_level=0) const
Returns the number of bands in the image.
virtual ossim_uint32 getNumberOfSamples(ossim_uint32 resLevel=0) const
Gets the number of samples for res level.
virtual ossim_uint32 getImageTileWidth() const
Returns the tile width of the image or 0 if the image is not tiled.
ossimScalarType m_scalarType
void ref() const
increment the reference count by one, indicating that this object has another pointer which is refere...
virtual void initialize()
Initialize the data buffer.
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 bool isValidRLevel(ossim_uint32 resLevel) const
Determines if the passed in reslution level is valid.
bool completely_within(const ossimIrect &rect) const
Definition: ossimIrect.cpp:425
double ossim_float64
virtual void loadTile(const void *src, const ossimIrect &src_rect, ossimInterleaveType il_type)
#define RTTI_DEF1_INST(cls, name, b1)
Definition: ossimRtti.h:481
ossimRefPtr< ossimGdalTileSource > m_gdalTileSource
virtual bool getOverviewTile(ossim_uint32 resLevel, ossimImageData *result)
Method to get an overview tile.
static ossimImageDataFactory * instance()
virtual ossim_uint32 getImageTileHeight() const
Returns the tile width of the image or 0 if the image is not tiled.
virtual ossimDataObjectStatus validate() const
virtual ossim_uint32 getNumberOfSamples(ossim_uint32 reduced_res_level=0) const
Returns the number of bands available from an image.
virtual ossim_uint32 getNumberOfInputBands() const
Returns the number of bands in the image.
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
virtual void setFilename(const ossimFilename &filename)
Sets the filename.
unsigned int ossim_uint32
bool isSupportedExtension()
virtual void close()
Method to close current entry.
virtual ossimIrect getImageRectangle() const
virtual ~ossimHdfReader()
virtural destructor
ossim_uint32 m_currentEntryRender
virtual ossimScalarType getOutputScalarType() const
Returns the output pixel type of the tile source.
virtual void close()
Deletes the overview and clears the valid image vertices.
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
virtual ossimIrect getImageRectangle(ossim_uint32 resLevel=0) const
Returns zero-based bounding rectangle of the image.
virtual ossim_uint32 getNumberOfInputBands() const
Returns the number of bands in the image.
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
Returns a pointer to a tile given an origin representing the upper left corner of the tile to grab fr...
virtual void getEntryList(std::vector< ossim_uint32 > &entryList) const
virtual ossimRefPtr< ossimImageData > create(ossimSource *owner, ossimScalarType scalar, ossim_uint32 bands=1) const
ossimIrect clipToRect(const ossimIrect &rect) const
Definition: ossimIrect.cpp:501
ossimScalarType
void unref() const
decrement the reference count by one, indicating that a pointer to this object is referencing it...
virtual ossimScalarType getOutputScalarType() const
Returns the output pixel type of the tile source.
virtual ossimRefPtr< ossimImageGeometry > getImageGeometry()
Returns the image geometry object associated with this tile source or NULL if non defined...
virtual bool setCurrentEntry(ossim_uint32 entryIdx)
virtual bool setOutputBandList(const std::vector< ossim_uint32 > &band_list)
If the image handler "isBandSeletor()" then the band selection of the output chip can be controlled...
return status
virtual void makeBlank()
Initializes data to null pixel values.
virtual void completeOpen()
Will complete the opening process.
ossimRefPtr< ossimImageHandler > theOverview
This class defines an abstract Handler which all image handlers(loaders) should derive from...
ossimString getDriverName()
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &rect, ossim_uint32 resLevel=0)
Method to grab a tile(rectangle) from image.
virtual const void * getBuf() const
std::vector< ossim_uint32 > m_entryFileList
ossimString getEntryString(ossim_uint32 entryId) const
ossim_uint32 m_numberOfBands
ossimFilename theOverviewFile
virtual ossim_uint32 getImageTileHeight() const
Returns the tile width of the image or 0 if the image is not tiled.
ossimString ext() const
virtual ossim_uint32 getNumberOfSamples(ossim_uint32 resLevel=0) const =0
Pure virtual, derived classes must implement.
virtual bool isOpen() const
Method to test for open file stream.
virtual bool setCurrentEntry(ossim_uint32 entryIdx)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
ossimHdfReader()
default construtor