00001 #ifndef IRVE_H
00002 #define IRVE_H
00003
00004 #include <TeAdoDB.h>
00005 #include <TeDatabase.h>
00006 #include <TeProjection.h>
00007 #include <TeGeneralizedProxMatrix.h>
00008 #include <TeSTObject.h>
00009 #include "Som.h"
00010
00011
00012 class IRVE {
00013 public:
00014 IRVE( SOM * som, vector<string> params, TIntVector clusters, int c ): _som(som), _params(params), map_cluster(clusters), c(c)
00015 {
00016 vector<string> vec;
00017 m = som->getMapcode()->getMapSize();
00018
00019 num_data = 359;
00020
00021 data_cluster = create_intvector( 0, num_data );
00022 visited = create_intvector( 0, num_data );
00023
00024 for (int i=0; i <= num_data; ++i ) visited[i] = data_cluster[i] = 0;
00025
00026 p = create_vector( 1, c );
00027 q = create_vector( 1, c );
00028
00029 for (int k = 1; k <= c; ++k ) p[k] = q[k] = 0;
00030
00031 codebook_label = som->getMapcode()->getLabel();
00032
00033 for (int j=1; j<=c; j++ ) clusters_label.push_back( vec );
00034
00035 ReadProxMatrix();
00036 };
00037
00038
00039 TMatrix Index();
00040
00041 private:
00042
00043 void MarkNeighborsVisited( int i, int c );
00044 void Calculate_p_q();
00045 void AssociateCluster();
00046
00047 bool ReadProxMatrix();
00048
00049 vector< vector<string> > clusters_label;
00050
00051 TVector p;
00052 TVector q;
00053 TIntVector map_cluster;
00054 TIntVector data_cluster;
00055 TIntVector visited;
00056
00057 TMatrix neighbors;
00058
00059 SOM *_som;
00060 vector<string> _params;
00061
00062 int m;
00063 int num_data;
00064 int c;
00065
00066 TLabel codebook_label;
00067 };
00068
00069
00070 void
00071 IRVE::MarkNeighborsVisited( int i, int c )
00072 {
00073 if ( ( data_cluster[i] == c ) && ( visited[ i ] == 0 ) ) {
00074 visited[ i ] = 1;
00075 for (int j=0; j<=num_data; j++)
00076 if ( neighbors[i][j] == 1 )
00077 if ( data_cluster[j] == c )
00078 MarkNeighborsVisited( j, c );
00079 }
00080 };
00081
00082
00083 void
00084 IRVE::Calculate_p_q()
00085 {
00086 for (int i=0; i<=num_data; i++ )
00087 if ( visited[i] == 0 ) {
00088 ++q[data_cluster[i]];
00089 MarkNeighborsVisited( i, data_cluster[i] );
00090 }
00091 };
00092
00093 void
00094 IRVE::AssociateCluster()
00095 {
00096 for (int i = 0; i < m; ++i )
00097 for (TvecLabel::iterator ite = codebook_label[i].begin(); ite != codebook_label[i].end(); ++ite ) {
00098 data_cluster[ strtoint( *ite ) ] = map_cluster[ i + 1 ];
00099 ++p[ map_cluster[ i + 1 ] ];
00100 }
00101
00102 };
00103
00104
00105 TMatrix
00106 IRVE::Index()
00107 {
00108 AssociateCluster();
00109
00110 Calculate_p_q();
00111
00112 TMatrix result = create_matrix( 0, c, 1, 3 );
00113
00114 result[0][1] = 0.0;
00115
00116 for (int i=1; i<=c; ++i ) {
00117 result[ i ][ 1 ] = q[ i ];
00118 result[ i ][ 2 ] = p[ i ];
00119
00120 if ( q[ i ] == p[ i ] )
00121 result[ i ][ 3 ] = 0;
00122 else
00123 result[ i ][ 3 ] = -( (q[i] - 1)/p[i]) + 1.0;
00124 result[ 0 ][ 1 ] += result[ i ][ 3 ];
00125 }
00126
00127 result[ 0 ][ 1 ] /= c;
00128
00129 return result;
00130 };
00131
00132
00133 bool
00134 IRVE::ReadProxMatrix() {
00135
00136 string host = _params[0];
00137 string dbname = _params[1];
00138 string user = _params[2];
00139 string pass = _params[3];
00140 string layer = _params[4];
00141 string where = _params[5];
00142
00143
00144 TeDatabase* db_ = new TeAdo();
00145 if (!db_->connect(host,user,pass,dbname,0))
00146 {
00147 cout << "Error: " << db_->errorMessage() << endl;
00148 return false;
00149 }
00150
00151
00152
00153
00154 TeLayer *layer1 = new TeLayer( layer );
00155
00156 if (!db_->loadLayer(layer1))
00157 {
00158 cout << "Error: " << db_->errorMessage() << endl;
00159 db_->close();
00160 return false;
00161 }
00162
00163
00164 TeRepresentation* rep = layer1->getRepresentation(TePOLYGONS);
00165 if (!rep)
00166 {
00167 cout << "Layer has no polygons!" << endl;
00168 db_->close();
00169 return false;
00170 }
00171
00172 string geomTableName = rep->tableName_;
00173
00174 string q;
00175 q = "SELECT * FROM " + geomTableName;
00176
00177
00178
00179
00180 TeDatabasePortal *portal = db_->getPortal();
00181 if (!portal)
00182 {
00183 cout << "Error trying to get a database portal" << endl;
00184 db_->close();
00185 return false;
00186 }
00187
00188
00189 if (!portal->query(q) || !portal->fetchRow())
00190 {
00191
00192 delete portal;
00193 return false;
00194 }
00195
00196
00197
00198 TePolygonSet ps;
00199 bool flag = true;
00200 do {
00201 TePolygon poly;
00202 flag = portal->fetchGeometry(poly);
00203 ps.add(poly);}
00204 while (flag);
00205
00206 delete portal;
00207
00208
00209 TeSTObject<TePolygonSet> objects;
00210 objects.setGeometry( ps );
00211
00212
00213 TeSTObjectSet<TePolygonSet> s( layer1, layer );
00214 s.insertSTObject( objects );
00215
00216 TeGeneralizedProxMatrix g( s, TePOLYGONS, "Breymann" );
00217
00218 int max = 359;
00219 int i, j, sum_tot = 0;
00220 TeNeighbours n;
00221 neighbors = create_matrix( 0, max, 0, max );
00222 num_data = max;
00223
00224 for (i=0; i<=max; i++) for (j=0; j <= max; j++) neighbors[i][j] = 0;
00225
00226 for (i=0; i<ps.size(); i++)
00227 {
00228 TePolygon pol = ps[i];
00229 for (j=0; j<pol.size(); j++) {
00230 n = g.getNeighbours( Te2String( pol[j].geomId() ) );
00231 for (int count = 0; count < n.size(); ++count )
00232 neighbors[ pol[j].geomId() ][ strtoint( n.ObjectId(count) ) ] = 1;
00233 }
00234 }
00235 db_->close();
00236 return true;
00237 };
00238
00239
00240
00241 #endif
00242
00243
00244