openModeller
Version 1.4.0
|
00001 00030 #include <openmodeller/occ_io/OccurrencesFactory.hh> 00031 00032 #include <openmodeller/Log.hh> 00033 #include <openmodeller/Exceptions.hh> 00034 00035 bool OccurrencesFactory::_initiated; 00036 00037 /****************/ 00038 /*** instance ***/ 00039 OccurrencesFactory& 00040 OccurrencesFactory::instance() 00041 { 00042 static OccurrencesFactory _instance; 00043 00044 if ( ! _initiated ) { 00045 00046 _instance.registerDriver( "TXT", &DelimitedTextOccurrences::CreateOccurrencesReaderCallback ); 00047 _instance.registerDriver( "XML", &SerializedXmlOccurrences::CreateOccurrencesReaderCallback ); 00048 00049 #ifdef TERRALIB_FOUND 00050 _instance.registerDriver( "TerraLib", &TeOccurrences::CreateOccurrencesReaderCallback ); 00051 #endif 00052 00053 #ifdef CURL_FOUND 00054 _instance.registerDriver( "TAPIR", &TapirOccurrences::CreateOccurrencesReaderCallback ); 00055 _instance.registerDriver( "GBIF" , &GbifOccurrences::CreateOccurrencesReaderCallback ); 00056 #endif 00057 00058 _initiated = true; 00059 } 00060 00061 return _instance; 00062 } 00063 00064 /***********************/ 00065 /*** register driver ***/ 00066 bool 00067 OccurrencesFactory::registerDriver( const string& driverId, CreateOccurrencesReaderCallback builder ) 00068 { 00069 return _drivers.insert( DriversMap::value_type( driverId, builder ) ).second; 00070 } 00071 00072 /*************************/ 00073 /*** unregister driver ***/ 00074 bool 00075 OccurrencesFactory::unregisterDriver( const string& driverId ) 00076 { 00077 return _drivers.erase( driverId ) != 0; 00078 } 00079 00080 /******************************/ 00081 /*** get registered drivers ***/ 00082 vector<string> 00083 OccurrencesFactory::getRegisteredDrivers() 00084 { 00085 vector<string> driver_ids; 00086 00087 DriversMap::const_iterator d = _drivers.begin(); 00088 DriversMap::const_iterator end = _drivers.end(); 00089 00090 while ( d != end ) { 00091 00092 driver_ids.push_back( d->first ); 00093 00094 ++d; 00095 } 00096 00097 return driver_ids; 00098 } 00099 00100 /**************/ 00101 /*** create ***/ 00102 OccurrencesReader* 00103 OccurrencesFactory::create( const char * source, const char * coordSystem ) 00104 { 00105 string source_str( source ); 00106 00107 // It must be TerraLib point data if source starts with "terralib>" 00108 int i = source_str.find( "terralib>" ); 00109 00110 DriversMap::const_iterator di; 00111 00112 if ( i == 0 ) { 00113 00114 di = _drivers.find( "TerraLib" ); 00115 00116 if ( di != _drivers.end() ) { 00117 00118 OccurrencesReader * te_driver = (di->second)( source, coordSystem ); 00119 te_driver->load(); 00120 00121 return te_driver; 00122 } 00123 else { 00124 00125 throw OccurrencesReaderException( "TerraLib driver not found" ); 00126 } 00127 } 00128 00129 // It must be GBIF REST if source is the GBIF REST URL for occurrences 00130 if ( source_str == "http://data.gbif.org/ws/rest/occurrence/list" ) { 00131 00132 di = _drivers.find( "GBIF" ); 00133 00134 if ( di != _drivers.end() ) { 00135 00136 OccurrencesReader * gbif_driver = (di->second)( source, coordSystem ); 00137 gbif_driver->load(); 00138 00139 return gbif_driver; 00140 } 00141 else { 00142 00143 throw OccurrencesReaderException( "GBIF driver not found" ); 00144 } 00145 } 00146 00147 i = source_str.find( "http://" ); 00148 00149 if ( i == 0 ) { 00150 00151 // Try TAPIR driver first 00152 di = _drivers.find( "TAPIR" ); 00153 00154 if ( di != _drivers.end() ) { 00155 00156 OccurrencesReader * tapir_driver = (di->second)( source, coordSystem ); 00157 00158 if ( tapir_driver->load() ) { 00159 00160 return tapir_driver; 00161 } 00162 } 00163 00164 // Then try GBIF (in case they changed the URL or if someone else decided to 00165 // implement a service using the GBIF REST protocol) 00166 di = _drivers.find( "GBIF" ); 00167 00168 if ( di != _drivers.end() ) { 00169 00170 OccurrencesReader * gbif_driver = (di->second)( source, coordSystem ); 00171 00172 if ( gbif_driver->load() ) { 00173 00174 return gbif_driver; 00175 } 00176 } 00177 } 00178 00179 // Try to open as XML file 00180 try { 00181 00182 Log::instance()->debug( "Trying to open occurrences source as XML file.\n" ); 00183 00184 OccurrencesReader * xml_driver = new SerializedXmlOccurrences( source, coordSystem ); 00185 00186 if ( xml_driver->load() ) { 00187 00188 return xml_driver; 00189 } 00190 00191 delete xml_driver; 00192 } 00193 catch ( OmException& e ) { 00194 00195 Log::instance()->debug( "XML occurrences reader exception: %s\n", e.what() ); 00196 } 00197 00198 Log::instance()->debug( "Trying to open occurrences source as TXT file.\n" ); 00199 00200 // Default driver for delimited text 00201 OccurrencesReader * file_driver = new DelimitedTextOccurrences( source, coordSystem ); 00202 00203 file_driver->load(); 00204 00205 return file_driver; 00206 }