OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimSensorModelFactory.cpp
Go to the documentation of this file.
1 //*****************************************************************************
2 // FILE: ossimSensorModelFactory.cc
3 //
4 // License: MIT
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // AUTHOR: Oscar Kramer
9 //
10 // DESCRIPTION:
11 // Contains implementation of class ossimSensorModelFactory
12 //
13 //*****************************************************************************
14 // $Id$
15 
23 
24 #include <fstream>
25 #include <algorithm>
26 #include <memory>
27 
28 //***
29 // Define Trace flags for use within this file:
30 //***
31 #include <ossim/base/ossimTrace.h>
32 static ossimTrace traceExec = ossimTrace("ossimSensorModelFactory:exec");
33 static ossimTrace traceDebug = ossimTrace("ossimSensorModelFactory:debug");
34 
35 //***
36 // Note to programmer: To add a new model, search this file for "ADD_MODEL"
37 // to locate places requiring editing. Functional example below...
38 //
39 // ADD_MODEL: Include all sensor model headers here:
40 //***
52 //#include <ossim/projection/ossimNitfMapModel.h>
67 
68 //***
69 // ADD_MODEL: List names of all sensor models produced by this factory:
70 //***
71 //const char* MY_NEW_MODEL = "myNewModel";
72 // const char* OCG_MODEL = "ossimCoarseGridModel";
73 // const char* RPC_MODEL = "ossimRpcModel";
74 // const char* IKONOS_RPC_MODEL = "ossimIkonosRpcModel";
75 // const char* NITF_RPC_MODEL = "ossimNitfRpcModel";
76 // const char* LANDSAT_MODEL = "ossimLandSatModel";
77 // const char* NITF_MAP_MODEL = "ossimNitfMapModel";
78 // const char* FCSI_MODEL = "ossimFcsiModel";
79 // const char* EMERGE_FCSI_MODEL= "ossimEmergeFcsiModel";
80 
81 //*****************************************************************************
82 // STATIC METHOD: ossimSensorModelFactory::instance()
83 //*****************************************************************************
85 {
86  static ossimSensorModelFactory inst;
87  return &inst;
88 }
89 
90 //*****************************************************************************
91 // METHOD: ossimSensorModelFactory::create(kwl, prefix)
92 //*****************************************************************************
94  const char *prefix) const
95 {
97 
98  //
99  // Permit specification of geometry file name in lieu of type:
100  //
101  const char* value = keywordList.find(prefix, ossimKeywordNames::GEOM_FILE_KW);
102  if (value)
103  {
104  result = createProjection(ossimFilename(value), 0);
105  }
106  //
107  // Search for occurence of "type" keyword:
108  //
109  else
110  {
111  value = keywordList.find(prefix, ossimKeywordNames::TYPE_KW);
112  if(value)
113  {
114  result = createProjection(ossimString(value));
115  if(result.valid())
116  {
117  if(!result->loadState(keywordList, prefix))
118  {
119  result = 0;
120  }
121  }
122  }
123  }
124 
125  return result.release();
126 }
127 
128 //*****************************************************************************
129 // METHOD:
130 //
131 //*****************************************************************************
134 {
135  //***
136  // Name should represent the model type:
137  //***
139  {
140  return new ossimSkyBoxLearSensor;
141  }
142  if(name == STATIC_TYPE_NAME(ossimIpodSensor))
143  {
144  return new ossimIpodSensor;
145  }
147  {
148  return new ossimCoarseGridModel;
149  }
150  if(name == STATIC_TYPE_NAME(ossimBuckeyeSensor))
151  {
152  return new ossimBuckeyeSensor;
153  }
154 
155  if (name == STATIC_TYPE_NAME(ossimRpcModel))
156  {
157  return new ossimRpcModel;
158  }
159 
161  {
162  return new ossimLandSatModel;
163  }
164 
165 // if(name == STATIC_TYPE_NAME(ossimNitfMapModel))
166 // {
167 // return new ossimNitfMapModel;
168 // }
169 
171  {
172  return new ossimQuickbirdRpcModel;
173  }
174 
176  {
177  return new ossimIkonosRpcModel;
178  }
179 
181  {
182  return new ossimNitfRpcModel;
183  }
184  if(name == STATIC_TYPE_NAME(ossimFcsiModel))
185  {
186  return new ossimFcsiModel;
187  }
189  {
190  return new ossimApplanixUtmModel;
191  }
193  {
194  return new ossimApplanixEcefModel;
195  }
196  if(name == STATIC_TYPE_NAME(ossimSpot5Model))
197  {
198  return new ossimSpot5Model;
199  }
200  if(name == STATIC_TYPE_NAME(ossimSarModel))
201  {
202  return new ossimSarModel;
203  }
205  {
206  return new ossimRS1SarModel;
207  }
209  {
210  return new ossimPpjFrameSensor;
211  }
213  {
214  return new ossimAlphaSensorHRI;
215  }
217  {
218  return new ossimAlphaSensorHSI;
219  }
221  {
222  return new ossimNitfRsmModel();
223  }
225  {
226  return new ossimSpectraboticsRedEdgeModel();
227  }
228 
229 
230  //***
231  // ADD_MODEL: (Please leave this comment for the next programmer)
232  //***
233 // if(name == MY_NEW_MODEL)
234 // return new myNewModel;
235 
236  return NULL;
237 }
238 
239 //*****************************************************************************
240 // METHOD
241 //*****************************************************************************
244 {
245  return createProjection(typeName);
246 }
247 
248 //*****************************************************************************
249 // METHOD
250 //*****************************************************************************
253  const char* prefix)const
254 {
255  return createProjection(kwl, prefix);
256 }
257 
258 //*****************************************************************************
259 // METHOD
260 //*****************************************************************************
261 void
262 ossimSensorModelFactory::getTypeNameList(std::vector<ossimString>& typeList)
263  const
264 {
265  typeList.push_back(STATIC_TYPE_NAME(ossimApplanixEcefModel));
266  typeList.push_back(STATIC_TYPE_NAME(ossimApplanixUtmModel));
267  typeList.push_back(STATIC_TYPE_NAME(ossimCoarseGridModel));
268  typeList.push_back(STATIC_TYPE_NAME(ossimRpcModel));
269  typeList.push_back(STATIC_TYPE_NAME(ossimIkonosRpcModel));
270  typeList.push_back(STATIC_TYPE_NAME(ossimQuickbirdRpcModel));
271  typeList.push_back(STATIC_TYPE_NAME(ossimNitfRpcModel));
272  typeList.push_back(STATIC_TYPE_NAME(ossimLandSatModel));
273 // typeList.push_back(STATIC_TYPE_NAME(ossimNitfMapModel));
274  typeList.push_back(STATIC_TYPE_NAME(ossimFcsiModel));
275  typeList.push_back(STATIC_TYPE_NAME(ossimSpot5Model));
276  typeList.push_back(STATIC_TYPE_NAME(ossimSarModel));
277  typeList.push_back(STATIC_TYPE_NAME(ossimRS1SarModel));
278  typeList.push_back(STATIC_TYPE_NAME(ossimBuckeyeSensor));
279  typeList.push_back(STATIC_TYPE_NAME(ossimSkyBoxLearSensor));
280  typeList.push_back(STATIC_TYPE_NAME(ossimIpodSensor));
281  typeList.push_back(STATIC_TYPE_NAME(ossimPpjFrameSensor));
282  typeList.push_back(STATIC_TYPE_NAME(ossimAlphaSensorHRI));
283  typeList.push_back(STATIC_TYPE_NAME(ossimAlphaSensorHSI));
284  typeList.push_back(STATIC_TYPE_NAME(ossimNitfRsmModel));
286  //***
287  // ADD_MODEL: Please leave this comment for the next programmer. Add above.
288  //***
289  //typeList.push_back(ossimString(MY_NEW_MODEL));
290 
291 }
292 
294  const ossimFilename& filename, ossim_uint32 entryIdx) const
295 {
296  // ossimFilename::exists() currently does not work with s3 url's.
297  // if(!filename.exists()) return 0;
298 
299  if(filename.empty()) return 0;
300 
301  static const char MODULE[] = "ossimSensorModelFactory::createProjection";
302 
303  ossimKeywordlist kwl;
305 
306  ossimFilename geomFile = filename;
307  geomFile = geomFile.setExtension("geom");
308 
309  if(geomFile.exists()&&
310  kwl.addFile(filename.c_str()))
311  {
312  ossimFilename coarseGrid;
313 
314  const char* type = kwl.find(ossimKeywordNames::TYPE_KW);
315  if(type)
316  {
317  if(ossimString(type) ==
319  {
320  findCoarseGrid(coarseGrid, filename);
321 
322  if(coarseGrid.exists() &&(coarseGrid != ""))
323  {
324  kwl.add("grid_file_name",
325  coarseGrid.c_str(),
326  true);
327  model = new ossimCoarseGridModel(kwl);
328  if(!model->getErrorStatus())
329  {
330  return model.release();
331  }
332  model = 0;
333  }
334  }
335  }
336  kwl.clear();
337  }
338 
339  // See if there is an external geomtry.
341  createProjectionFromGeometryFile(filename, entryIdx);
342  if (proj.valid())
343  {
344  return proj.release();
345  }
346 
347  if(model.valid())
348  {
349  model = 0;
350  }
351 
352  // first check for override
353  //
354  if(geomFile.exists()&&kwl.addFile(geomFile.c_str()))
355  {
356  model = createProjection(kwl);
357  if(model.valid())
358  {
359  return model.release();
360  }
361  model = 0;
362  }
363 
364  if(traceDebug())
365  {
367  << MODULE << " DEBUG: Testing ossimCoarsGridModel" << std::endl;
368  }
369 
370  ifstream input(geomFile.c_str());
371  char ecgTest[4] = { 0 };
372  input.read(ecgTest, 3); // even if `read()` fails, it will be initialized thanks to `= { 0 };`
373  input.close();
374  if(std::string(ecgTest) == "eCG")
375  {
376  ossimKeywordlist kwlTemp;
377  kwlTemp.add("type",
378  "ossimCoarseGridModel",
379  true);
380  kwlTemp.add("geom_file",
381  geomFile.c_str(),
382  true);
383  return createProjection(kwlTemp);
384  }
385 
386  if(traceDebug())
387  {
389  << MODULE << " DEBUG: testing ossimRpcModel" << std::endl;
390  }
391 
392  //---
393  // Test for quick bird rpc. Could be either a tiff or nitf so not wrapped
394  // around "isNitf()" anymore.
395  //---
396  if(traceDebug())
397  {
399  << MODULE << " DEBUG: testing ossimQuickbirdRpcModel"
400  << std::endl;
401  }
402 
404  if(qbModel->parseFile(filename))
405  {
406  if(traceDebug())
407  {
409  << MODULE << " DEBUG: returning ossimQuickbirdRpcModel"
410  << std::endl;
411  }
412  model = qbModel.get();
413  qbModel = 0;
414  return model.release();
415  }
416  else
417  {
418  qbModel = 0;
419  }
420 
421 // ossimRefPtr<ossimRpcModel> qbModel = new ossimQuickbirdRpcModel;
422 // if(qbModel->parseFile(filename))
423 // {
424 // if(traceDebug())
425 // {
426 // ossimNotify(ossimNotifyLevel_DEBUG)
427 // << MODULE << " DEBUG: returning ossimQuickbirdRpcModel"
428 // << std::endl;
429 // }
430 // model = qbModel.get();
431 // qbModel = 0;
432 // return model.release();
433 // }
434 // else
435 // {
436 // qbModel = 0;
437 // }
438 //
439  //---
440  // Test for ikonos rpc. Could be tiff or nitf which is handled in
441  // parseFile method.
442  //---
444  if(ikModel->parseFile(filename))
445  {
446  if(traceDebug())
447  {
449  << MODULE << " DEBUG returning ossimQuickbirdRpcModel"
450  << std::endl;
451  }
452  model = ikModel.get();
453  ikModel = 0;
454  return model.release();
455  }
456  else
457  {
458  ikModel = 0;
459  }
460  if(isNitf(filename))
461  {
462  if(traceDebug())
463  {
465  << MODULE << " DEBUG: testing ossimNitfRsmModel" << std::endl;
466  }
468 
469  if(rsmModel->parseFile(filename, entryIdx))
470  {
471  model = rsmModel.get();
472  rsmModel = 0;
473  return model.release();
474  }
475  else
476  {
477  rsmModel = 0;
478  }
479  if(traceDebug())
480  {
482  << MODULE << " DEBUG: testing ossimNitfRsmModel" << std::endl;
483  }
484 
486  if ( rpcModel->parseFile(filename, entryIdx) ) // filename = NITF_file
487  {
488  model = rpcModel.get();
489  rpcModel = 0;
490  return model.release();
491  }
492  else
493  {
494  rpcModel = 0;
495  }
496 
497  if(traceDebug())
498  {
500  << MODULE << " DEBUG: testing ossimIkinosRpcModel" << std::endl;
501  }
502 
503  // RP - This model crashes routinely if it ever happens to get a hold of a NITF
504  //model = new ossimNitfMapModel(filename); // filename = NITF_file
505  //if(!model->getErrorStatus())
506  //{
507  // return model.release();
508  //}
509  //model = 0;
510  }
511  else if(isLandsat(filename))
512  {
513  model = new ossimLandSatModel(filename);
514  if(!model->getErrorStatus())
515  {
516  return model.release();
517  }
518  model = 0;
519  }
520 
521  model = new ossimRS1SarModel(filename);
523  {
524  return model.release();
525  }
526  model = 0;
527 
528  // SPOT:
529  ossimFilename spot5Test = geomFile;
530  if(!spot5Test.exists())
531  {
532  spot5Test = geomFile.path();
533  spot5Test = spot5Test.dirCat(ossimFilename("METADATA.DIM"));
534  if (spot5Test.exists() == false)
535  {
536  spot5Test = geomFile.path();
537  spot5Test = spot5Test.dirCat(ossimFilename("metadata.dim"));
538  }
539  }
540  if(spot5Test.exists())
541  {
542  //---
543  // Check the basename of the input file. So we don't create a projection
544  // for ancillary files, icon.jpg amd preview.jpg.
545  //---
546  ossimFilename baseName = filename.file();
547  baseName.downcase();
548 
549  if ( (baseName != "icon.jpg" ) && ( baseName != "preview.jpg" ) )
550  {
553  if(meta->loadXmlFile(spot5Test))
554  {
555  model = new ossimSpot5Model(meta.get());
556  if(!model->getErrorStatus())
557  {
558  return model.release();
559  }
560  }
561  }
562  }
563  model = 0;
564 
565  ossimFilename ppjFilename = filename;
566  ppjFilename = ppjFilename.setExtension("ppj");
567  if(traceDebug())
568  {
570  << MODULE << " DEBUG: testing ossimPpjFrameSensor" << std::endl;
571  }
572  if(ppjFilename.exists())
573  {
575 
576  if(ppjFile->readFile(ppjFilename))
577  {
579  ossimDpt imageSize = ppjFile->getImageSize();
580  sensor->setFocalLength(ppjFile->getIntrinsic()[0][0], ppjFile->getIntrinsic()[1][1]);
581  sensor->setPrincipalPoint(ppjFile->getPrincipalPoint());
582  sensor->setecef2CamMatrix(ppjFile->getExtrinsic().SymSubMatrix(1,3));
583  sensor->setCameraPosition(ppjFile->getPlatformPosition());
584  sensor->setImageSize(imageSize);
585  sensor->setImageRect(ossimDrect(0,0,imageSize.x-1, imageSize.y-1));
586  sensor->setRefImgPt(ossimDpt(imageSize.x*.5, imageSize.y*.5));
588  sensor->updateModel();
589  return sensor.release();
590  }
591  ppjFile = 0;
592  }
593 
594  ossimFilename hdrFilename = filename;
595  hdrFilename = hdrFilename.setExtension("hdr"); // image.hdr
596  if ( !hdrFilename.exists() )
597  {
598  hdrFilename = filename;
599  hdrFilename.string() += ".hdr"; // image.ras.hdr
600  }
601  if(traceDebug())
602  {
604  << MODULE << " DEBUG: testing ossimAlphaSensor\nheader file: " << hdrFilename << std::endl;
605  }
606  if(hdrFilename.exists())
607  {
609  if(supData->readSupportFiles(hdrFilename))
610  {
611  if (supData->isHSI())
612  {
613  if(traceDebug())
614  {
616  << MODULE << " DEBUG: loading ossimAlphaSensorHSI" << std::endl;
617  }
619  if ( sensor->initialize( *(supData.get()) ) )
620  {
621  return (ossimProjection*)sensor.release();
622  }
623  }
624  else
625  {
626  if(traceDebug())
627  {
629  << MODULE << " DEBUG: loading ossimAlphaSensorHRI" << std::endl;
630  }
632  if ( sensor->initialize( *(supData.get()) ) )
633  {
634  return (ossimProjection*)sensor.release();
635  }
636  }
637  }
638  supData = 0;
639  }
640 
641  model = new ossimCoarseGridModel(geomFile);
642  if(model.valid())
643  {
644  if(!model->getErrorStatus())
645  {
646  return model.release();
647  }
648  model = 0;
649  }
650 
651  return model.release();
652 }
653 
655 {
656  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
657  createIstream( filename, std::ios_base::in | std::ios_base::binary );
658 
659  if( in )
660  {
661  char nitfFile[4];
662  in->read((char*)nitfFile, 4);
663 
664  return (ossimString(nitfFile, nitfFile+4) == "NITF");
665  }
666 
667  return false;
668 }
669 
671 {
672  ossimFilename temp(filename);
673  temp.downcase();
674  ossimRefPtr<ossimFfL7> ff_headerp;
675  if (temp.contains("header.dat"))
676  {
677  ff_headerp = new ossimFfL5(filename);
678  }
679  else
680  {
681  ff_headerp = new ossimFfL7(filename);
682  }
683  bool r = !(ff_headerp->getErrorStatus());
684  ff_headerp = 0;
685  return r;
686 }
687 
689  const ossimFilename& geomFile)const
690 {
691  result = geomFile;
692  result.setFile(result.fileNoExtension()+"_ocg");
693  result.setExtension("dat");
694 
695  if(!result.exists())
696  {
697  result = geomFile;
698  result.setExtension("dat");
699  }
700 
701  // let's find a .dat file in the current directory
702  //
703  if(!result.exists())
704  {
705  result = "";
706  ossimDirectory directoryList(geomFile.path());
707  ossimFilename file;
708  if(directoryList.getFirst(file,
710  {
711  ossimString testString = "OSSIM_DBL_GRID";
712  char tempBuf[14];
713  do
714  {
715  if(file.ext().downcase() == "dat")
716  {
717  std::ifstream in(file.c_str());
718  if(in)
719  {
720  in.read((char*)tempBuf, 14);
721  in.close();
722  if(ossimString(tempBuf, tempBuf+14) == testString)
723  {
724  result = file;
725  }
726  }
727 
728  }
729  }while((directoryList.getNext(file))&&(result == ""));
730  }
731  // try to find it
732  }
733 }
734 
virtual bool readSupportFiles(const ossimFilename &hdrFile)
const NEWMAT::Matrix & getExtrinsic() const
virtual bool readFile(const ossimFilename &file)
bool loadXmlFile(const ossimFilename &file, bool processSwir=false)
ossimProjection * createProjectionFromGeometryFile(const ossimFilename &imageFile, ossim_uint32 entryIdx) const
This method takes the filename and tries to find an external ".geom" file.
ossimFilename & setFile(const ossimString &f)
static ossimSensorModelFactory * instance()
Represents serializable keyword/value map.
bool addFile(const char *file)
static const ossimErrorCode OSSIM_OK
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
double y
Definition: ossimDpt.h:165
bool contains(char aChar) const
Definition: ossimString.h:58
bool isLandsat(const ossimFilename &filename) const
static StreamFactoryRegistry * instance()
virtual bool parseFile(const ossimFilename &nitfFile, ossim_uint32 entryIndex=0)
void setFocalLength(double focX, double focY)
void setCameraPosition(const ossimGpt &value)
static const char * TYPE_KW
virtual ossimObject * createObject(const ossimString &typeName) const
const ossimDpt & getImageSize() const
#define STATIC_TYPE_NAME(T)
Definition: ossimRtti.h:325
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
void findCoarseGrid(ossimFilename &result, const ossimFilename &geomFile) const
bool isNitf(const ossimFilename &filename) const
const ossimDpt & getPrincipalPoint() const
virtual bool parseFile(const ossimFilename &nitfFile, ossim_uint32 entryIndex=0)
Method to parse an nitf file and initialize model.
bool exists() const
unsigned int ossim_uint32
void setAveragePrjectedHeight(double averageProjectedHeight)
T * release()
Definition: ossimRefPtr.h:93
static const char * GEOM_FILE_KW
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
virtual bool initialize(const ossimAlphaSensorSupportData &supData)
Initializes model from support data and calls update Model on success.
void setImageRect(const ossimDrect &imageRect)
void setPrincipalPoint(const ossimDpt &value)
virtual void getTypeNameList(std::vector< ossimString > &typeList) const
virtual bool parseFile(const ossimFilename &file)
virtual ossimErrorCode getErrorStatus() const
const ossimGpt & getPlatformPosition() const
ossimFilename fileNoExtension() const
double x
Definition: ossimDpt.h:164
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
bool empty() const
Definition: ossimString.h:411
virtual ossimProjection * createProjection(const ossimFilename &filename, ossim_uint32 entryIdx) const
takes a filename.
virtual bool initialize(const ossimAlphaSensorSupportData &supData)
Initializes model from support data and calls update Model on success.
ossimFilename file() const
void setRefImgPt(const ossimDpt &pt)
Sets the center line sampe of the image.
const NEWMAT::Matrix & getIntrinsic() const
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
ossimFilename path() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
void setecef2CamMatrix(const NEWMAT::Matrix &value)
void setImageSize(const ossimDpt &size)
const std::string & string() const
Definition: ossimString.h:414
virtual bool parseFile(const ossimFilename &file)
Public method for parsing generic image file.