OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGdalFactory.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //*******************************************************************
10 // $Id: ossimGdalFactory.cpp 22229 2013-04-12 14:13:46Z dburken $
11 
12 #include <ossimGdalFactory.h>
13 #include <ossimGdalTileSource.h>
14 #include <ossimOgrGdalTileSource.h>
16 #include <ossimHdfReader.h>
17 
19 #include <ossim/base/ossimString.h>
21 #include <ossim/base/ossimTrace.h>
23 
24 #include <gdal.h>
25 #include <ogrsf_frmts.h>
26 
27 static const ossimTrace traceDebug("ossimGdalFactory:debug");
28 
30 
33 {
35 }
36 
37 
39 {
40  if(!theInstance)
41  {
43  CPLSetErrorHandler((CPLErrorHandler)CPLQuietErrorHandler);
44  GDALAllRegister();
45  OGRRegisterAll();
46  }
47 
48  // lets turn off gdal error reporting
49  //
50 // GDALSetCacheMax(1024*1024*20);
51 // GDALSetCacheMax(0);
52 
53  return theInstance;
54 }
55 
57  bool openOverview)const
58 {
59  // For GDAL we can't check for file exists since they support encoded opens for
60  // subdatasets
61  //
62 
63  if(fileName.ext().downcase() == "nui") return 0;
64  if(traceDebug())
65  {
67  << "ossimGdalFactory::open(filename) DEBUG: entered..." << std::endl;
68  }
69 
71 
72  //try hdf reader first
73  if (traceDebug())
74  {
76  << "ossimGdalFactory::open(filename) DEBUG:"
77  << "\ntrying ossimHdfReader"
78  << std::endl;
79  }
80  result = new ossimHdfReader;
82  if(result->open(fileName))
83  {
84  return result.release();
85  }
86 
87  if (traceDebug())
88  {
90  << "ossimGdalFactory::open(filename) DEBUG:"
91  << "\ntrying ossimGdalTileSource"
92  << std::endl;
93  }
94 
95  result = new ossimGdalTileSource;
97  if(result->open(fileName))
98  {
99  return result.release();
100  }
101 
102  if (traceDebug())
103  {
105  << "ossimGdalFactory::open(filename) DEBUG:"
106  << "\ntrying ossimOgrVectorTileSource\n";
107  }
108 
109  // No need to set overview flag.
110  result = new ossimOgrVectorTileSource;
111  if(result->open(fileName))
112  {
113  return result.release();
114  }
115 
116  // we will have vector datasets only open if file exists.
117  if(!fileName.exists() ||
118  fileName.before(":", 3).upcase() == "SDE" ||
119  fileName.before(":", 4).upcase() == "GLTP" ||
120  fileName.ext().downcase() == "mdb")
121  {
122  result = 0;
123  return result.release();
124  }
125 
126  // No need to set overview flag.
127  if (traceDebug())
128  {
130  << "ossimGdalFactory::open(filename) DEBUG:"
131  << "\ntrying ossimOgrGdalTileSource\n";
132  }
133 
134  result = new ossimOgrGdalTileSource;
135  if(result->open(fileName))
136  {
137  return result.release();
138  }
139  result = 0;
140  return result.release();
141  if(traceDebug())
142  {
144  << "ossimGdalFactory::open(filename) DEBUG: leaving..."
145  << std::endl;
146  }
147 
148  return (ossimImageHandler*)NULL;
149 }
150 
152  const char* prefix)const
153 {
155 
156  if(traceDebug())
157  {
158  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimGdalFactory::open(kwl, prefix) DEBUG: entered..." << std::endl;
159  }
160  if(traceDebug())
161  {
162  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimGdalFactory::open(kwl, prefix) DEBUG: trying ossimHdfReader" << std::endl;
163  }
164  result = new ossimHdfReader;
165  if(result->loadState(kwl, prefix))
166  {
167  return result.release();
168  }
169  result = 0;
170 
171  if(traceDebug())
172  {
173  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimGdalFactory::open(kwl, prefix) DEBUG: trying ossimGdalTileSource" << std::endl;
174  }
175  result = new ossimGdalTileSource;
176  if(result->loadState(kwl, prefix))
177  {
178  return result.release();
179  }
180  result = 0;
181  if(traceDebug())
182  {
183  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimGdalFactory::open(kwl, prefix) DEBUG: trying ossimOgrGdalTileSource" << std::endl;
184  }
185 
186  result = new ossimOgrGdalTileSource;
187  if(result->loadState(kwl, prefix))
188  {
189  return result.release();
190  }
191  result = 0;
192  if(traceDebug())
193  {
194  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimGdalFactory::open(kwl, prefix) DEBUG: leaving..." << std::endl;
195  }
196 
197  return result.release();
198 }
199 
201  const ossimFilename& file ) const
202 {
204  if ( file.size() )
205  {
206  result = new ossimGdalTileSource;
207 
208  result->setOpenOverviewFlag( false ); // Always false.
209 
210  if ( result->open( file ) == false )
211  {
212  result = 0;
213  }
214  }
215  return result;
216 }
217 
219 {
220  if(STATIC_TYPE_NAME(ossimHdfReader) == typeName)
221  {
222  return new ossimHdfReader();
223  }
224  if(STATIC_TYPE_NAME(ossimGdalTileSource) == typeName)
225  {
226  return new ossimGdalTileSource();
227  }
229  {
230  return new ossimOgrGdalTileSource();
231  }
232 
233  return (ossimObject*)0;
234 }
235 
237  const char* prefix)const
238 {
240  const char* type = kwl.find(prefix, ossimKeywordNames::TYPE_KW);
241 
242  if(type)
243  {
244  if (ossimString(type).trim() == STATIC_TYPE_NAME(ossimImageHandler))
245  {
246  const char* lookup = kwl.find(prefix, ossimKeywordNames::FILENAME_KW);
247  if (lookup)
248  {
249  // Call the open that takes a filename...
250  result = this->open(kwl, prefix);//ossimFilename(lookup));
251  }
252  }
253  else
254  {
255  result = createObject(ossimString(type));
256  if(result.valid())
257  {
258  result->loadState(kwl, prefix);
259  }
260  }
261  }
262 
263  return result.release();
264 }
265 
266 void ossimGdalFactory::getTypeNameList(std::vector<ossimString>& typeList)const
267 {
268  typeList.push_back(STATIC_TYPE_NAME(ossimHdfReader));
269  typeList.push_back(STATIC_TYPE_NAME(ossimGdalTileSource));
270  typeList.push_back(STATIC_TYPE_NAME(ossimOgrGdalTileSource));
271 }
272 
274 {
275  // ossimOgrGdalTileSource - shape file reader:
276  extensionList.push_back( ossimString("shp") );
277 
278  int driverCount = GDALGetDriverCount();
279  int idx = 0;
280 
281  for(idx = 0; idx < driverCount; ++idx)
282  {
283  GDALDriverH driver = GDALGetDriver(idx);
284 
285  if(driver)
286  {
287  const char* metaData = GDALGetMetadataItem(driver, GDAL_DMD_EXTENSION, 0);
288  int nMetaData = metaData ? strlen(metaData) : 0;
289  if(metaData && nMetaData>0 )
290  {
291  std::vector<ossimString> splitArray;
292  ossimString(metaData).split(splitArray, " /");
293 
294  ossim_uint32 idxExtension = 0;
295 
296  for(idxExtension = 0; idxExtension < splitArray.size(); ++idxExtension)
297  {
298  extensionList.push_back(splitArray[idxExtension].downcase());
299  }
300  }
301  }
302  }
303 
304  if(GDALGetDriverByName("AAIGrid"))
305  {
306  extensionList.push_back("adf");
307  }
308 }
309 
312 {
313  if ( ext == "shp" )
314  {
315  result.push_back(new ossimOgrGdalTileSource);
316  }
317  else
318  {
320  getSupportedExtensions(extList);
321 
322  ossimString testExt = ext.downcase();
323 
324  if(std::find(extList.getList().begin(),
325  extList.getList().end(), testExt) != extList.getList().end())
326  {
327  result.push_back(new ossimGdalTileSource);
328  }
329  }
330 }
331 
333 {
334  int driverCount = GDALGetDriverCount();
335  int idx = 0;
336 
337  for(idx = 0; idx < driverCount; ++idx)
338  {
339  GDALDriverH driver = GDALGetDriver(idx);
340 
341  if(driver)
342  {
343  const char* metaData = GDALGetMetadataItem(driver, GDAL_DMD_MIMETYPE, 0);
344  int nMetaData = metaData ? strlen(metaData) : 0;
345  if(metaData && nMetaData>0 )
346  {
347  if(ossimString(metaData) == mimeType)
348  {
349  result.push_back(new ossimGdalTileSource());
350  return;
351  }
352  }
353  }
354  }
355 }
356 
virtual bool open()=0
Pure virtual open.
void setOpenOverviewFlag(bool flag)
Sets theOpenOverviewFlag.
ossimString before(const ossimString &str, std::string::size_type pos=0) const
METHOD: before(str, pos) Returns string beginning at pos and ending one before the token str If strin...
RTTI_DEF1(ossimGdalFactory, "ossimGdalFactory", ossimImageHandlerFactoryBase)
static ossimString upcase(const ossimString &aString)
Definition: ossimString.cpp:34
Represents serializable keyword/value map.
virtual ossimObject * createObject(const ossimString &typeName) const
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) 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.
virtual ossimImageHandler * open(const ossimFilename &fileName, bool openOverview=true) const
open that takes a filename.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
static ossimGdalFactory * instance()
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 void getSupportedExtensions(ossimImageHandlerFactoryBase::UniqueStringList &extensionList) const
virtual void getTypeNameList(std::vector< ossimString > &typeList) const
static const char * TYPE_KW
#define STATIC_TYPE_NAME(T)
Definition: ossimRtti.h:325
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
bool exists() const
std::string::size_type size() const
Definition: ossimString.h:405
unsigned int ossim_uint32
T * release()
Definition: ossimRefPtr.h:93
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
static ossimGdalFactory * theInstance
virtual void getImageHandlersByMimeType(ossimImageHandlerFactoryBase::ImageHandlerList &result, const ossimString &mimeType) const
virtual ~ossimGdalFactory()
This class defines an abstract Handler which all image handlers(loaders) should derive from...
virtual void getImageHandlersBySuffix(ossimImageHandlerFactoryBase::ImageHandlerList &result, const ossimString &ext) const
ossimString ext() const
std::vector< ossimRefPtr< ossimImageHandler > > ImageHandlerList
virtual ossimRefPtr< ossimImageHandler > openOverview(const ossimFilename &file) const
Open overview that takes a file name.
static const char * FILENAME_KW
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)