OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGeoidImage.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 // Generic geoid source which uses an image handler for reading the grid.
12 //
13 //----------------------------------------------------------------------------
14 
16 
17 #include <ossim/base/ossimDrect.h>
20 #include <ossim/base/ossimIrect.h>
22 #include <ossim/base/ossimNotify.h>
26 #include <cmath>
27 
29  : m_geom(0),
30  m_handler(0),
31  m_cacheTile(0),
32  m_geoidTypeName(),
33  m_memoryMapFlag(false),
34  m_enabledFlag(true),
35  m_imageRect()
36 {
37 }
38 
40 {
41  m_geom = 0;
42  m_handler = 0;
43  m_cacheTile = 0;
44 }
45 
46 bool ossimGeoidImage::open( const ossimFilename& file, ossimByteOrder /* byteOrder */ )
47 {
48  static const char MODULE[] = "ossimGeoidImage::open";
49 
50  m_geom = 0;
51  m_cacheTile = 0;
52  if ( m_handler.valid() )
53  {
54  m_handler->close();
55  }
56 
57  m_handler =
59  true, // try suffix 1st
60  false); // open overview
61  if ( m_handler.valid() )
62  {
63  // Store the bounding rectangle:
65 
66  // Scalar type:
68 
69  // Get the geometry info. Need this to compute grid indexes.
71 
72  if ( m_geom.valid() )
73  {
74  // Verify the geometry object has a good projection.
75  if ( m_geom->getProjection() )
76  {
77  if ( m_memoryMapFlag )
78  {
79  try
80  {
82 
83  if ( m_cacheTile.valid() )
84  {
85  // Close the image handler:
86  m_handler->close();
87  m_handler = 0;
88  }
89  }
90  catch ( const ossimException& e )
91  {
92  m_memoryMapFlag = false;
93  m_cacheTile = 0;
94 
96  << MODULE << " ERROR: Caught Exception!"
97  << "\n" << e.what()
98  << "\nMemory mapping entire grid into memory has been disabled..."
99  << std::endl;
100  }
101  }
102  }
103  else
104  {
105  m_handler = 0;
106  m_geom = 0;
108  << MODULE << " ERROR:"
109  << "\nGeometry object has null projection!"
110  << std::endl;
111  }
112  }
113 
114  else
115  {
116  m_handler = 0;
118  << MODULE << " ERROR:"
119  << "\nCould not get geometry info!"
120  << std::endl;
121  }
122  }
123 
124  return ( m_geom.valid() && (m_handler.valid() || (m_memoryMapFlag && m_cacheTile.valid()) ));
125 }
126 
128 {
129  return m_geoidTypeName;
130 }
131 
132 void ossimGeoidImage::setShortName( const std::string& geoidTypeName )
133 {
134  m_geoidTypeName = geoidTypeName;
135 }
136 
138 {
139  return m_memoryMapFlag;
140 }
141 
143 {
144  m_memoryMapFlag = flag;
145 }
146 
148  const char* prefix ) const
149 {
150  std::string myPrefix = ( prefix ? prefix : "" );
151 
152  // Save the connection string:
153  std::string key = "connection_string";
154  kwl.addPair( myPrefix, key, m_connectionString, true );
155 
156  // Save the geoid type name:
157  key = "geoid.type";
158  kwl.addPair( myPrefix, key, m_geoidTypeName.string(), true );
159 
160  // Save the memory map flag:
161  key = "memory_map";
162  std::string value = (m_memoryMapFlag ? "true" : "false");
163  kwl.addPair( myPrefix, key, value, true );
164 
165  // Save the enabledFlag:
167  value = m_enabledFlag ? "true" : "false";
168  kwl.addPair( myPrefix, key, value, true );
169 
170  // Save the type:
172  value = "geoid_image";
173  kwl.addPair( myPrefix, key, value, true );
174 
175  return true;
176 }
177 
179  const char* prefix )
180 {
181  bool result = false;
182 
183  std::string myPrefix = prefix ? prefix : "";
184 
185  // Check the type name:
186  std::string key = ossimKeywordNames::TYPE_KW;
187  std::string value = kwl.findKey( myPrefix, key );
188 
189  if ( (value == "geoid_image" ) || ( value == "ossimGeoidImage" ) )
190  {
191  // Get the geoid type name:
192  key = "geoid.type";
193  m_geoidTypeName = kwl.findKey( myPrefix, key );
194 
195  // Get the memory map flag:
196  key = "memory_map";
197  value = kwl.findKey( myPrefix, key );
198  m_memoryMapFlag = ossimString( value ).toBool();
199 
200  // Get the enabled flag:
202  value = kwl.findKey( myPrefix, key );
203  m_enabledFlag = ossimString( value ).toBool();
204 
205  // Get the connection string:
206  std::string key = "connection_string";
207  m_connectionString = kwl.findKey( myPrefix, key );
208  if ( m_connectionString.size() )
209  {
210  result = open( ossimFilename( m_connectionString ) );
211  }
212  }
213 
214  return result;
215 }
216 
217 
219 {
220  double offset = ossim::nan();
221 
222  if ( m_enabledFlag )
223  {
224  if ( m_geom.valid() &&
225  ( m_handler.valid() ||
227  )
228  )
229  {
230  switch(m_scalarType)
231  {
232  case OSSIM_SINT16:
233  {
234  offset = offsetFromEllipsoidTemplate((ossim_sint16)0, gpt);
235  break;
236  }
237  case OSSIM_FLOAT32:
238  {
239  offset = offsetFromEllipsoidTemplate((ossim_float32)0, gpt);
240  break;
241  }
242  case OSSIM_FLOAT64:
243  {
244  offset = offsetFromEllipsoidTemplate((ossim_float64)0, gpt);
245  break;
246  }
247  default:
248  {
250  << "ossimGeoidImage::offsetFromEllipsoid ERROR:\n"
251  << "Unhandled scalar type: "
253  << std::endl;
254  break;
255  }
256  }
257  }
258  else
259  {
261  << "ossimGeoidImage::offsetFromEllipsoid ERROR: Object not initialized!\n"
262  << std::endl;
263  }
264 
265  } // Matches: if ( m_enabledFlag )
266 
267  return offset;
268 }
269 
270 template <class T>
272  const ossimGpt& gpt)
273 {
274  double geoidOffset = ossim::nan();
275 
276  // Change the datum if needed:
277  ossimGpt copyGpt = gpt;
278  if(ossimDatumFactory::instance()->wgs84())
279  {
280  copyGpt.changeDatum(ossimDatumFactory::instance()->wgs84());
281  }
282 
283  // Fix wrap conditions:
284  copyGpt.wrap();
285 
286  // Get the local image point for the input ground point.
287  ossimDpt imgDpt;
288  m_geom->worldToLocal( copyGpt, imgDpt );
289 
290  if ( m_imageRect.pointWithin( ossimIpt( imgDpt ) ) )
291  {
292  ossim_int32 x0 = static_cast<ossim_int32>( imgDpt.x );
293  ossim_int32 y0 = static_cast<ossim_int32>( imgDpt.y );
294 
295  ossim_int32 IW = static_cast<ossim_int32>(m_imageRect.width());
296  ossim_int32 IH = static_cast<ossim_int32>(m_imageRect.height());
297 
298  if ( x0 == (IW-1) )
299  {
300  --x0; // Move over one point.
301  }
302  if ( y0 == (IH-1) )
303  {
304  --y0; // Move over one point.
305  }
306 
307  if ( !m_memoryMapFlag )
308  {
309  // Get the four points from the image handler.
310  ossimIrect tileRect(ossimIpt(x0, y0), ossimIpt(x0+1, y0+1));
311  m_cacheTile = m_handler->getTile( tileRect, 0 );
312  }
313 
314  if ( m_cacheTile.valid() )
315  {
316  // Get a pointer to the buffer.
317  const T* buf = static_cast<const T*>(m_cacheTile->getBuf());
318 
319  if ( buf )
320  {
321  const ossim_float64 NP = m_cacheTile->getNullPix(0);
322  const ossim_int64 TW = static_cast<ossim_int64>(m_cacheTile->getWidth());
323  ossim_int64 offset = (y0 - m_cacheTile->getOrigin().y) * TW +
324  (x0 - m_cacheTile->getOrigin().x);
325  ossim_int64 offset2 = offset + TW;
326 
327  double p00 = buf[offset];
328  double p01 = buf[offset+1];
329  double p10 = buf[offset2];
330  double p11 = buf[offset2+1];
331 
332  double xt0 = imgDpt.x - x0;
333  double yt0 = imgDpt.y - y0;
334  double xt1 = 1-xt0;
335  double yt1 = 1-yt0;
336 
337  double w00 = xt1*yt1;
338  double w01 = xt0*yt1;
339  double w10 = xt1*yt0;
340  double w11 = xt0*yt0;
341 
342  //---
343  // Test for null posts and set the corresponding weights to 0:
344  //---
345  if (p00 == NP) w00 = 0.0;
346  if (p01 == NP) w01 = 0.0;
347  if (p10 == NP) w10 = 0.0;
348  if (p11 == NP) w11 = 0.0;
349 
350  double sum_weights = w00 + w01 + w10 + w11;
351  if (sum_weights)
352  {
353  geoidOffset = (p00*w00 + p01*w01 + p10*w10 + p11*w11) / sum_weights;
354  }
355  }
356 
357  } // Matches: if ( m_cacheTile.valid() )
358 
359  } // Matches: if ( m_imageRect.pointWithin( imgPt ) )
360 
361  return geoidOffset;
362 }
363 
ossimString m_geoidTypeName
virtual ossim_uint32 getWidth() const
virtual double offsetFromEllipsoid(const ossimGpt &gpt)
ossimScalarType m_scalarType
64 bit floating point
ossimRefPtr< ossimImageData > m_cacheTile
bool getMemoryMapFlag() const
Gets the memory map flag.
void setMemoryMapFlag(bool flag)
Set the memory map flag.
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
Represents serializable keyword/value map.
const std::string & findKey(const std::string &key) const
Find methods that take std::string(s).
bool valid() const
Definition: ossimRefPtr.h:75
float ossim_float32
virtual ossimString getEntryString(ossim_int32 entry_number) const
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
std::string m_connectionString
double y
Definition: ossimDpt.h:165
ossim_uint32 height() const
Definition: ossimIrect.h:487
ossimGeoidImage()
default constructor
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of the object to a keyword list.
16 bit signed integer
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
32 bit floating point
void setShortName(const std::string &geoidTypeName)
Sets the geoid type name string.
virtual bool open(const ossimFilename &file, ossimByteOrder byteOrder=OSSIM_BIG_ENDIAN)
open method
void wrap()
Wrap method to maintain longitude between -180 and +180 and latitude between -90 and +90...
Definition: ossimGpt.h:305
ossimRefPtr< ossimImageGeometry > m_geom
static const char * TYPE_KW
void changeDatum(const ossimDatum *datum)
This will actually perform a shift.
Definition: ossimGpt.cpp:316
double ossim_float64
static ossimScalarTypeLut * instance()
Returns the static instance of an ossimScalarTypeLut object.
ossimIrect m_imageRect
virtual ossimRefPtr< ossimImageGeometry > getImageGeometry()
Returns the image geometry object associated with this tile source or NULL if non defined...
signed short ossim_sint16
virtual const char * what() const
Returns the error message.
ossimRefPtr< ossimImageHandler > m_handler
bool toBool() const
String to numeric methods.
virtual const ossim_float64 * getNullPix() const
static ossimDatumFactory * instance()
ossimByteOrder
virtual void close()
Deletes the overview and clears the valid image vertices.
virtual ossimIrect getImageRectangle(ossim_uint32 resLevel=0) const
Returns zero-based bounding rectangle of the image.
ossim_uint32 width() const
Definition: ossimIrect.h:500
static const char * ENABLED_KW
virtual ~ossimGeoidImage()
destructor
virtual ossimString getShortName() const
const ossimProjection * getProjection() const
Access methods for projection (may be NULL pointer).
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 ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
ossim_int32 y
Definition: ossimIpt.h:142
virtual const void * getBuf() const
double x
Definition: ossimDpt.h:164
long long ossim_int64
bool worldToLocal(const ossimGpt &world_pt, ossimDpt &local_pt) const
Exposes the 3D world-to-local image coordinate reverse projection.
static ossimImageHandlerRegistry * instance()
ossim_int32 x
Definition: ossimIpt.h:141
virtual const ossimIpt & getOrigin() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
int ossim_int32
double offsetFromEllipsoidTemplate(T dummy, const ossimGpt &gpt)
const std::string & string() const
Definition: ossimString.h:414
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)
bool pointWithin(const ossimIpt &pt) const
Definition: ossimIrect.h:729