OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGeneralRasterElevHandler.cpp
Go to the documentation of this file.
13 #include <ossim/base/ossimDpt.h>
14 #include <ossim/base/ossimGpt.h>
15 
16 RTTI_DEF1(ossimGeneralRasterElevHandler, "ossimGeneralRasterElevHandler", ossimElevCellHandler);
17 
19  :ossimElevCellHandler(file.c_str()),
20  m_streamOpen(false)
21 {
22  if(!open(file))
23  {
25  }
26 }
27 
30  theGeneralRasterInfo(src.theGeneralRasterInfo),
31  m_streamOpen(false), // ????
32  m_memoryMap(src.m_memoryMap)
33 {
34 }
35 
37  : m_streamOpen(false)
38 {
39 
40  close();
43  {
45  }
46 }
47 
49 {
50 }
51 
53 {
55 
56  if(m_memoryMap.empty())
57  {
59  {
60  case OSSIM_SINT8:
61  {
64  gpt);
65  break;
66  }
67  case OSSIM_UINT8:
68  {
71  gpt);
72  break;
73  }
74  case OSSIM_SINT16:
75  {
78  gpt);
79  break;
80  }
81  case OSSIM_UINT16:
82  {
85  gpt);
86  break;
87  }
88  case OSSIM_SINT32:
89  {
92  gpt);
93  break;
94  }
95  case OSSIM_UINT32:
96  {
99  gpt);
100  break;
101  }
102  case OSSIM_FLOAT32:
103  {
106  gpt);
107  break;
108  }
109  case OSSIM_FLOAT64:
110  {
113  gpt);
114  break;
115  }
116  default:
117  {
118  break;
119  }
120  }
121  }
122  else
123  {
125  {
126  case OSSIM_SINT8:
127  {
130  gpt);
131  break;
132  }
133  case OSSIM_UINT8:
134  {
137  gpt);
138  break;
139  }
140  case OSSIM_SINT16:
141  {
144  gpt);
145  break;
146  }
147  case OSSIM_UINT16:
148  {
151  gpt);
152  break;
153  }
154  case OSSIM_SINT32:
155  {
158  gpt);
159  break;
160  }
161  case OSSIM_UINT32:
162  {
165  gpt);
166  break;
167  }
168  case OSSIM_FLOAT32:
169  {
172  gpt);
173  break;
174  }
175  case OSSIM_FLOAT64:
176  {
179  gpt);
180  break;
181  }
182  default:
183  {
184  break;
185  }
186  }
187 
188  }
189 
190  return result;
191 }
192 
194 {
196 }
197 
198 double ossimGeneralRasterElevHandler::getPostValue(const ossimIpt& /* gridPt */) const
199 {
201  << "ossimGeneralRasterElevHandler::getPostValue(const ossimIpt& gridPt): NEED TO IMPLEMENT TO NEW INTERFACE\n";
202  return ossim::nan();
203 }
204 
206 {
207  if(!m_memoryMap.empty()) return true;
208  std::lock_guard<std::mutex> lock(m_inputStreamMutex);
209 
210  //---
211  // Change to use flag as is_open is non-const on some old compilers.
212  return m_streamOpen;
213  // return (m_inputStream.is_open());
214 }
215 
216 bool ossimGeneralRasterElevHandler::open(const ossimFilename& file, bool memoryMapFlag)
217 {
218  close();
219  if(!setFilename(file)) return false;
220  m_inputStream.clear();
221  m_inputStream.open(theGeneralRasterInfo.theFilename.c_str(), ios::in | ios::binary);
222 
223  if(memoryMapFlag)
224  {
225  if(!m_inputStream.bad())
226  {
228  if(!m_memoryMap.empty())
229  {
230  m_inputStream.read((char*)(&m_memoryMap.front()), (streamsize)m_memoryMap.size());
231  }
232  }
233  m_inputStream.close();
234  }
235 
236  // Capture the stream state for non-const is_open on old compiler.
237  m_streamOpen = m_inputStream.is_open();
238 
239  return m_streamOpen;
240 }
241 
246 {
247  m_inputStream.close();
248  m_memoryMap.clear();
249  m_streamOpen = false;
250 }
251 
253 {
254  if(file.trim() == "")
255  {
256  return false;
257  }
258  theFilename = file;
259  ossimFilename hdrFile = file;
260  ossimFilename geomFile = file;
265  hdrFile = hdrFile.setExtension("omd");
266  geomFile = geomFile.setExtension("geom");
267 
268  if(!hdrFile.exists()||
269  !geomFile.exists())
270  {
271  return false;
272  }
273  ossimKeywordlist kwl(hdrFile);
275  {
276  return false;
277  }
278 
279  kwl.add(ossimKeywordNames::FILENAME_KW, file.c_str(), true);
280  ossimGeneralRasterInfo generalInfo;
281 
282  if(!generalInfo.loadState(kwl))
283  {
284  return false;
285  }
286  if(generalInfo.numberOfBands() != 1)
287  {
288  ossimNotify(ossimNotifyLevel_WARN) << "ossimGeneralRasterElevHandler::initializeInfo WARNING:The number of bands are not specified in the header file" << std::endl;
289  return false;
290  }
291 
292  kwl.clear();
293  if(kwl.addFile(geomFile))
294  {
305 
306  //add by simbla
307 
308  // ---
309  // Try to determine if there is a prefix like "image0." or not.
310  // ---
311  std::string prefix = "";
312  if ( kwl.find("image0.type") )
313  {
314  prefix = "image0.";
315  }
316 
318  if(!theGeneralRasterInfo.theGeometry->loadState( kwl, prefix.c_str() ))
319  {
321  }
322 
324  {
325  return false;
326  }
327  ossimGpt defaultDatum;
328  ossimGpt ulGpt;
329  ossimGpt urGpt;
330  ossimGpt lrGpt;
331  ossimGpt llGpt;
332  theGeneralRasterInfo.theDatum = defaultDatum.datum();
337 
343  theGroundRect = ossimGrect(ulGpt, urGpt, lrGpt, llGpt);
346  }
347  else
348  {
349  return false;
350  }
351 
352  return true;
353 }
354 
355 #if 0
356 void ossimGeneralRasterElevHandler::initializeList(const ossimFilename& file)
357 {
358  close();
359  theFilename = file;
360  theBoundingRectInfoList.clear();
361  if(theFilename.exists())
362  {
363  if(theFilename.isDir())
364  {
366 
367  ossimFilename file;
368  ossim_uint32 maxCount = 10;
369  ossim_uint32 count = 0;
370  bool foundOne = false;
371  if(dir.getFirst(file))
372  {
373  do
374  {
375  ++count;
376  ossimString ext = file.ext();
377  ext = ext.downcase();
378  if(ext == "ras")
379  {
380  ossimGeneralRasterElevHandler::BoundingRectInfo info;
381  if(initializeInfo(info, file))
382  {
383  foundOne = true;
384  addInfo(info);
385  }
386  }
387  } while(dir.getNext(file) &&
388  (!foundOne &&
389  (count < maxCount)));
390  }
391 #if 0
392  std::vector<ossimFilename> result;
393 
394  dir.findAllFilesThatMatch(result,
395  ".*\\.ras");
396 
397  if(result.size())
398  {
399  ossimGeneralRasterElevHandler::BoundingRectInfo info;
400  ossim_uint32 idx = 0;
401  for(idx = 0; idx < result.size(); ++idx)
402  {
403  if(initializeInfo(info, ossimFilename(result[idx])))
404  {
405  addInfo(info);
406  }
407  }
408  }
409 #endif
410  if(theBoundingRectInfoList.size()>0)
411  {
412  theNullHeightValue = theBoundingRectInfoList[0].theNullHeightValue;
413  }
414  }
415  else
416  {
417  ossimGeneralRasterElevHandler::BoundingRectInfo info;
418  if(initializeInfo(info, theFilename))
419  {
420  addInfo(info);
421  }
422  }
423  }
424 }
425 #endif
426 template <class T>
428  T /* dummy */,
430  const ossimGpt& gpt)
431 {
432  ossimEndian endian;
433 
434  ossimGpt shiftedPoint = gpt;
435  shiftedPoint.changeDatum(info.theDatum);
436  if(!info.theWgs84GroundRect.pointWithin(shiftedPoint))
437  {
438  return ossim::nan();
439  }
440 
441  ossimDpt pt;
442  info.theGeometry->worldToLocal(shiftedPoint,pt);
443  double xi = pt.x;
444  double yi = pt.y;
445 
446  xi -= info.theUl.x;
447  yi -= info.theUl.y;
448 
449  //modifed by simbla 2008 7.17
450  //double xi = (shiftedPoint.lond() - info.theUlGpt.lond())/info.thePostSpacing.x;
451  //double yi = (info.theUlGpt.latd() -
452  // shiftedPoint.latd())/info.thePostSpacing.y;
453 
454 
455  ossim_sint64 x0 = static_cast<ossim_sint64>(xi);
456  ossim_sint64 y0 = static_cast<ossim_sint64>(yi);
457 
458  double xt0 = xi - x0;
459  double yt0 = yi - y0;
460  double xt1 = 1-xt0;
461  double yt1 = 1-yt0;
462 
463  double w00 = xt1*yt1;
464  double w01 = xt0*yt1;
465  double w10 = xt1*yt0;
466  double w11 = xt0*yt0;
467 
468 
469  if ( xi < 0 || yi < 0 ||
470  x0 > (info.theWidth - 1.0) ||
471  y0 > (info.theHeight - 1.0) )
472  {
473  return ossim::nan();
474  }
475 
476  if(x0 == (info.theWidth - 1.0))
477  {
478  --x0;
479  }
480  if(y0 == (info.theHeight - 1.0))
481  {
482  --y0;
483  }
484  T p[4];
485 
486  ossim_uint64 bytesPerLine = info.theBytesPerRawLine;
487 
488  std::streampos offset = y0*bytesPerLine + x0*sizeof(T);
489 
490  {
491  std::lock_guard<std::mutex> lock(m_inputStreamMutex);
492  if(m_inputStream.fail())
493  {
494  m_inputStream.clear();
495  }
496  m_inputStream.seekg(offset, ios::beg);
497  m_inputStream.read((char*)p, sizeof(T));
498 
499  // Get the second post.
500  m_inputStream.read((char*)(p+1), sizeof(T));
501 
502  // offset += (bytesPerLine-2*sizeof(T));
503 
504  m_inputStream.ignore(bytesPerLine-2*sizeof(T));
505  // Get the third post.
506  m_inputStream.read((char*)(p+2), sizeof(T));
507 
508  // Get the fourth post.
509  m_inputStream.read((char*)(p+3), sizeof(T));
510 
511  if(m_inputStream.fail())
512  {
513  m_inputStream.clear();
514  return ossim::nan();
515  }
516  }
517  if(endian.getSystemEndianType() != info.theByteOrder)
518  {
519  endian.swap((T*)p, (ossim_uint32)4);
520  }
521  double p00 = p[0];
522  double p01 = p[1];
523  double p10 = p[2];
524  double p11 = p[3];
525 
526  if (p00 == info.theNullHeightValue)
527  w00 = 0.0;
528  if (p01 == info.theNullHeightValue)
529  w01 = 0.0;
530  if (p10 == info.theNullHeightValue)
531  w10 = 0.0;
532  if (p11 == info.theNullHeightValue)
533  w11 = 0.0;
534 
535 #if 0 /* Serious debug only... */
536  cout << "\np00: " << p00
537  << "\np01: " << p01
538  << "\np10: " << p10
539  << "\np11: " << p11
540  << "\nw00: " << w00
541  << "\nw01: " << w01
542  << "\nw10: " << w10
543  << "\nw11: " << w11
544  << endl;
545 #endif
546 
547  double sum_weights = w00 + w01 + w10 + w11;
548 
549  if (sum_weights)
550  {
551  return (p00*w00 + p01*w01 + p10*w10 + p11*w11) / sum_weights;
552  }
553 
554  return ossim::nan();
555 }
556 
557 template <class T>
559  T /* dummy */,
561  const ossimGpt& gpt)
562 {
563  ossimEndian endian;
564 
565  ossimGpt shiftedPoint = gpt;
566  shiftedPoint.changeDatum(info.theDatum);
567  if(!info.theWgs84GroundRect.pointWithin(shiftedPoint))
568  {
569  return ossim::nan();
570  }
571 
572  ossimDpt pt;
573  info.theGeometry->worldToLocal(shiftedPoint,pt);
574  double xi = pt.x;
575  double yi = pt.y;
576 
577  xi -= info.theUl.x;
578  yi -= info.theUl.y;
579 
580  //modifed by simbla 2008 7.17
581  //double xi = (shiftedPoint.lond() - info.theUlGpt.lond())/info.thePostSpacing.x;
582  //double yi = (info.theUlGpt.latd() -
583  // shiftedPoint.latd())/info.thePostSpacing.y;
584 
585 
586  ossim_sint64 x0 = static_cast<ossim_sint64>(xi);
587  ossim_sint64 y0 = static_cast<ossim_sint64>(yi);
588 
589  double xt0 = xi - x0;
590  double yt0 = yi - y0;
591  double xt1 = 1-xt0;
592  double yt1 = 1-yt0;
593 
594  double w00 = xt1*yt1;
595  double w01 = xt0*yt1;
596  double w10 = xt1*yt0;
597  double w11 = xt0*yt0;
598  if ( xi < 0 || yi < 0 ||
599  x0 > (info.theWidth - 1.0) ||
600  y0 > (info.theHeight - 1.0) )
601  {
602  return ossim::nan();
603  }
604 
605  if(x0 == (info.theWidth - 1.0))
606  {
607  --x0;
608  }
609  if(y0 == (info.theHeight - 1.0))
610  {
611  --y0;
612  }
613  ossim_uint64 bytesPerLine = info.theBytesPerRawLine;
614 
615  ossim_uint64 offset = y0*bytesPerLine + x0*sizeof(T);
616  ossim_uint64 offset2 = offset+bytesPerLine;
617 
618  T v00 = *(reinterpret_cast<T*> (&m_memoryMap[offset]));
619  T v01 = *(reinterpret_cast<T*> (&m_memoryMap[offset + sizeof(T)]));
620  T v10 = *(reinterpret_cast<T*> (&m_memoryMap[offset2]));
621  T v11 = *(reinterpret_cast<T*> (&m_memoryMap[offset2 + sizeof(T)]));
622  if(endian.getSystemEndianType() != info.theByteOrder)
623  {
624  endian.swap(v00);
625  endian.swap(v01);
626  endian.swap(v10);
627  endian.swap(v11);
628  }
629  double p00 = v00;
630  double p01 = v01;
631  double p10 = v10;
632  double p11 = v11;
633 
634  if (p00 == info.theNullHeightValue)
635  w00 = 0.0;
636  if (p01 == info.theNullHeightValue)
637  w01 = 0.0;
638  if (p10 == info.theNullHeightValue)
639  w10 = 0.0;
640  if (p11 == info.theNullHeightValue)
641  w11 = 0.0;
642 
643 #if 0 /* Serious debug only... */
644  cout << "\np00: " << p00
645  << "\np01: " << p01
646  << "\np10: " << p10
647  << "\np11: " << p11
648  << "\nw00: " << w00
649  << "\nw01: " << w01
650  << "\nw10: " << w10
651  << "\nw11: " << w11
652  << endl;
653 #endif
654 
655  double sum_weights = w00 + w01 + w10 + w11;
656 
657  if (sum_weights)
658  {
659  return (p00*w00 + p01*w01 + p10*w10 + p11*w11) / sum_weights;
660  }
661 
662  return ossim::nan();
663 }
664 #if 0
665 void ossimGeneralRasterElevHandler::addInfo(const ossimGeneralRasterElevHandler::BoundingRectInfo& info)
666 {
667  if(theBoundingRectInfoList.empty())
668  {
669  theBoundingRectInfoList.push_back(info);
670  theNullHeightValue = info.theNullHeightValue;
671  theBoundingRect = info.theBounds;
672  }
673  else
674  {
675  BoundingRectListType::iterator iter = theBoundingRectInfoList.begin();
676  bool found = false;
677 // if(theNullHeightValue == info.theNullHeightValue)
678 // {
679  while((iter!=theBoundingRectInfoList.end())&&
680  (!found))
681  {
682  if(info.thePostSpacing.length() <= iter->thePostSpacing.length())
683  {
684  theBoundingRectInfoList.insert(iter, info);
685  theBoundingRect = theBoundingRect.combine(info.theBounds);
686  found = true;
687  }
688  else
689  {
690  ++iter;
691  }
692  }
693  if(!found)
694  {
695  theBoundingRectInfoList.push_back(info);
696  }
697  }
698 }
699 #endif
701 {
703 }
704 
706 {
707  ossimDpt pt = gpt;
708 
710 // BoundingRectListType::const_iterator i = theBoundingRectInfoList.begin();
711 // while (i != theBoundingRectInfoList.end())
712 // {
713 // if ((*i).theBounds.pointWithin(pt))
714 // {
715 // return true;
716 // }
717 // ++i;
718 // }
719 // return false;
720 }
722 {
723  return theGeneralRasterInfo;
724 }
double getHeightAboveMSLFileTemplate(T dummy, const ossimGeneralRasterElevHandler::GeneralRasterInfo &info, const ossimGpt &gpt)
8 bit signed integer
void clear()
Erases the entire container.
Definition: ossimString.h:432
bool pointWithin(const ossimDpt &pt, double epsilon=0.0) const
Definition: ossimDrect.h:781
64 bit floating point
16 bit unsigned integer
Represents serializable keyword/value map.
bool addFile(const char *file)
virtual bool setFilename(const ossimFilename &file)
bool open(const ossimFilename &file, bool memoryMapFlag=false)
Opens a stream to the srtm cell.
bool valid() const
Definition: ossimRefPtr.h:75
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Attempts to initialize a transform and a projection given the KWL.
const char * find(const char *key) const
ossim_int64 fileSize() const
float ossim_float32
ossimScalarType getScalarType() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
double y
Definition: ossimDpt.h:165
ossim_uint32 height() const
Definition: ossimIrect.h:487
16 bit signed integer
const ossimIpt & ul() const
Definition: ossimIrect.h:274
ossim_int32 bytesPerRawLine() const
Returns the number of bytes in a raw line.
static const ossimErrorCode OSSIM_ERROR
signed char ossim_sint8
32 bit floating point
unsigned short ossim_uint16
bool isDir() const
const ossimIpt & ll() const
Definition: ossimIrect.h:277
32 bit unsigned integer
double getNullPix(ossim_uint32 band) const
void changeDatum(const ossimDatum *datum)
This will actually perform a shift.
Definition: ossimGpt.cpp:316
ossimByteOrder getImageDataByteOrder() const
double getHeightAboveMSLMemoryTemplate(T dummy, const ossimGeneralRasterElevHandler::GeneralRasterInfo &info, const ossimGpt &gpt)
virtual double getPostValue(const ossimIpt &gridPt) const
METHOD: getPostValue Returns the value at a given grid point as a double.
const ossimDatum * datum() const
datum().
Definition: ossimGpt.h:196
ossimDrect getBoundingRect() const
This method does not really fit the handler since this handle a directory not a cell that could have ...
double ossim_float64
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
ossimGrect theGroundRect
bool exists() const
virtual bool pointHasCoverage(const ossimGpt &gpt) const
Loops through ossimGeneralRasterElevHandler::BoundingRectListType and checks for coverage.
bool localToWorld(const ossimDpt &local_pt, ossimGpt &world_pt) const
Exposes the 3D projection from image to world coordinates.
signed short ossim_sint16
ossimGeneralRasterElevHandler::GeneralRasterInfo theGeneralRasterInfo
32 bit signed integer
unsigned long long ossim_uint64
unsigned int ossim_uint32
ossimDpt getMetersPerPixel() const
Returns the GSD associated with this image in the active projection.
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
signed int ossim_sint32
ossimFilename theFilename
Virtual method for reading.
const ossimIpt & lr() const
Definition: ossimIrect.h:276
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
ossim_uint32 width() const
Definition: ossimIrect.h:500
ossimByteOrder getSystemEndianType() const
Definition: ossimEndian.h:78
const ossimIpt & ur() const
Definition: ossimIrect.h:275
Container class that holds both 2D transform and 3D projection information for an image Only one inst...
virtual ossimIpt getSizeOfElevCell() const
METHOD: getSizeOfElevCell Returns the number of post in the cell.
virtual double getHeightAboveMSL(const ossimGpt &)
METHOD: getHeightAboveMSL Height access methods.
virtual ossimErrorCode getErrorStatus() const
ossim_int32 y
Definition: ossimIpt.h:142
bool m_streamOpen
true if stream is open.
const ossimGeneralRasterElevHandler::GeneralRasterInfo & generalRasterInfo() const
virtual void close()
Closes the stream to the file.
double x
Definition: ossimDpt.h:164
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
RTTI_DEF1(ossimGeneralRasterElevHandler, "ossimGeneralRasterElevHandler", ossimElevCellHandler)
const ossimImageMetaData & getImageMetaData() const
const ossimIrect & imageRect() const
Zero based rectangle of the valid image.
ossimString ext() const
bool worldToLocal(const ossimGpt &world_pt, ossimDpt &local_pt) const
Exposes the 3D world-to-local image coordinate reverse projection.
ossim_int32 x
Definition: ossimIpt.h:141
8 bit unsigned integer
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
ossimGeneralRasterElevHandler(const ossimFilename &file="")
static const char * FILENAME_KW
ossim_int32 numberOfBands() const
Number of bands.
void swap(ossim_sint8 &)
Definition: ossimEndian.h:26
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
signed long long ossim_sint64