RepositorySOMDataTerralib.h

00001 #ifndef RepositorySOMDataTerralib_H
00002 #define RepositorySOMDataTerralib_H
00003 
00004 #include "TeAdoDB.h"
00005 #include "TeDatabase.h"
00006 
00007 #include "ISOMDataRepository.h"
00008 #include "Data.h"
00009 #include "Defs.h"
00010 
00011 class RepositorySOMDataTerralib : public ISOMDataRepository {
00012 public:
00014         void save( SOMData data );
00015 
00017         SOMData *  load( vector<string> params, int flag );
00018 public:
00019         TMatrix getCentroidCoords( vector<string> params  );
00020 };
00021 
00022 
00023 TMatrix 
00024 RepositorySOMDataTerralib::getCentroidCoords( vector<string> params  )
00025 {
00026         string host = params[0];
00027     string dbname = params[1];
00028     string user = params[2];
00029     string pass = params[3];
00030         string layer = params[4];
00031         string where = params[5];
00032 
00033         // Opens a connection to a database accessible though ADO
00034     TeDatabase* db_ = new TeAdo();
00035     if (!db_->connect(host,user,pass,dbname,0))
00036     {
00037             cout << "Error: " << db_->errorMessage() << endl;
00038             exit(0);
00039     }
00040     cout << "1) Opened connection to: " << dbname << " on server: " << host << endl << endl;
00041 
00042     // Loads a layer by its name 
00043     TeLayer *layer1 = new TeLayer( layer );
00044 
00045     if (!db_->loadLayer(layer1))                
00046     {                                                                                               
00047             cout << "Error: " << db_->errorMessage() << endl;
00048             db_->close();
00049             exit(0);
00050     }
00051 
00052     // Retrieves the information about the polygons representation
00053     TeRepresentation* rep = layer1->getRepresentation(TePOLYGONS);
00054     if (!rep)
00055     {                                                                                               
00056             cout << "Layer has no polygons!" << endl;
00057             db_->close();
00058             exit(0);
00059     }
00060         
00061     string geomTableName = rep->tableName_;
00062 
00063     // Build the SQL to get all polygons that are inside a given box
00064     //TeBox bb(320000,7390000,338000,7400000);
00065     string q;
00066     q = "SELECT * FROM " + geomTableName + " WHERE ";
00067     //    q +=  db_->getSQLBoxWhere (bb, TePOLYGONS);
00068                 q += where;
00069 
00070     // Get a portal to the database
00071     TeDatabasePortal *portal = db_->getPortal();
00072     if (!portal)
00073     {                                                                                               
00074                 cout << "Error trying to get a database portal" << endl;
00075                 db_->close();
00076                 exit(0);
00077     }
00078 
00079     // Submit the query and go to the first position of the portal 
00080     if (!portal->query(q) || !portal->fetchRow())
00081     {       
00082                 // error trying to submit the query or no records returned
00083                 delete portal;
00084                 exit(0);
00085     }
00086 
00087     // Process all the polygons found
00088     TePolygonSet ps;
00089     bool flag = true;
00090     do
00091     {
00092                 TePolygon poly;
00093                 flag = portal->fetchGeometry(poly);
00094                 ps.add(poly);
00095     }
00096     while (flag);   // while there is a record to be read
00097     delete portal;
00098 
00099     // Shows the result
00100     for (int i=0; i<ps.size(); i++)
00101     {
00102                 TePolygon pol = ps[i];
00103                 cout << "Geometry id: " << pol[0].geomId() << " object id: " << pol.objectId() << endl;
00104                 for (int j=1; j<pol.size(); j++)
00105                          cout << "\t Child id: " << pol[j].geomId() << endl;
00106      }
00107 
00108      // Close database
00109             db_->close();
00110         cout <<  "End! (Press enter)\n";
00111         cout.flush();
00112         getchar();
00113         return 0;
00114 };
00115 
00116 
00117 void 
00118 RepositorySOMDataTerralib::save( SOMData data ) 
00119 {
00120         cout << "Save Data\n";
00121 };
00122 
00123 SOMData *  
00124 RepositorySOMDataTerralib::load( vector<string> params, int flag )
00125 {
00126         
00127         TvecLabel lvec;
00128         MatrixInt mat;
00129         VecInt vec;
00130         MatrixInt::iterator im;
00131         VecInt::iterator        iv;
00132 
00133         string host = params[0];
00134     string dbname = params[1];
00135     string user = params[2];
00136     string pass = params[3];
00137         string table = params[4];
00138         string where = params[5];
00139 
00140         int dimension, i, j;
00141 
00142         // Opens a connection to a database accessible though ADO
00143     TeDatabase* db = new TeAdo();
00144     if (!db->connect(host,user,pass,dbname,0))
00145     {
00146                cout << "Error: " << db->errorMessage() << endl;
00147                return 0;
00148     }
00149 
00150         //      4) Rettrieve some data from a table throught a SQL query 
00151 
00152     TeDatabasePortal* portal = db->getPortal();
00153     if (!portal)
00154     {
00155             cout << "Error getting a portal to dabase: " << db->errorMessage() << endl;
00156             db->close();
00157             return 0;
00158     }
00159         string variables = params[6];
00160         for (i=7; i<params.size(); i++) variables = variables + ", " + params[i];
00161     string sql = "SELECT " + variables + " FROM " + table + " WHERE " + where;
00162 
00163         dimension = params.size() - 7;
00164 
00165     if (portal->query(sql))
00166     {            
00167             while (portal->fetchRow())                      // retrieve the attributoes
00168             {                                   
00169                                         lvec.push_back( portal->getData(0) );
00170 
00171                                         vec.clear();
00172                                         for (int j=1; j<= dimension; j++)  vec.push_back( portal->getDouble(j) );                       
00173 
00174                                         mat.push_back( vec );
00175             }
00176 
00177                         TMatrix mdata = create_matrix( 1, mat.size(), 1, dimension );
00178                         i = j = 1;
00179                         
00180                         for (im=mat.begin(); im<mat.end(); im++) {
00181                                 j = 1;                          
00182                                 for (iv=(*im).begin(); iv < (*im).end(); iv++) 
00183                                 {
00184                                         mdata[i][j] = *iv;                                                                      
00185                                         j++;
00186                                 }
00187                                 i++;
00188                         }
00189 
00190                         SOMData * som_data = new SOMData;
00191         
00192                         som_data->setDimension( dimension );
00193                         som_data->setDataSize( mat.size() );
00194                         som_data->setEntries( mdata );
00195                         som_data->setLabel( lvec );
00196 
00197                         delete portal;
00198                         db->close();
00199 
00200                         return som_data;
00201 
00202     }
00203     else
00204     {
00205             cout << "Error submiting query: " << db->errorMessage() << endl;
00206             delete portal;
00207             db->close();
00208             return 0;
00209     }
00210 
00211     delete portal;
00212     db->close();
00213 
00214         return 0;
00215 }
00216 
00217 #endif

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3