TeGeneralizedProxMatrix.h

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 /*! \file TeGeneralizedProxMatrix.h
00024         \brief This file contains structures and definitions about generalized proximity matrices support in TerraLib
00025 */
00026 
00027 #ifndef TeProxMatrix_H
00028 #define TeProxMatrix_H
00029 
00030 #include "TeNeighbours.h"
00031 #include "TeProxMatrixImplementation.h"
00032 #include "TeProxMatrixConstructionStrategy.h"
00033 #include "TeProxMatrixSlicingStrategy.h"
00034 #include "TeProxMatrixWeightsStrategy.h"
00035 #include "TeSTElementSet.h"
00036 
00037 
00038 //! A class to represent a generalized proximity matrix 
00039 template<typename Set> 
00040 class TeGeneralizedProxMatrix  
00041 {
00042 private:
00043         TeProxMatrixImplementation*     imp_;  
00044         TeProxMatrixConstructionStrategy<Set>* sc_;   
00045         TeProxMatrixSlicingStrategy* ss_;   
00046         TeProxMatrixWeightsStrategy* sw_;  
00047         
00048 protected:
00049         //! Get the implementation from a concrete factory defined by impl_type
00050         TeProxMatrixImplementation*                             getImplementation(const TeGPMImplementation& implementation_type = TeGraphBreymann);
00051 
00052         //! Verify if a matrix was created correctly by the constructors.       
00053         bool isValid() const;
00054         
00055 public:
00056         int                                             gpm_id_;        
00057         bool                                    is_default_;
00058         int                                             total_slices_;
00059         string                                  neighbourhood_table_;
00060         TeGPMImplementation             impl_strategy_;
00061 
00062         //! Empty constructor
00063         TeGeneralizedProxMatrix ():
00064                 imp_(0), sc_(0), ss_(0), sw_(0), gpm_id_(-1),
00065                 is_default_(false), total_slices_(1), neighbourhood_table_(""), 
00066                 impl_strategy_(TeGraphBreymann)
00067           {}
00068 
00069   //! Constructor
00070         TeGeneralizedProxMatrix (const int& id, const string& tableName, TeProxMatrixImplementation* imp, TeProxMatrixWeightsStrategy* ws):
00071                 imp_(imp), sc_(0), ss_(0), sw_(ws),
00072                 gpm_id_(id), is_default_(false), total_slices_(1), 
00073                 neighbourhood_table_(tableName), impl_strategy_(TeGraphBreymann)
00074           {}
00075 
00076         //! Constructor parametrized with specific strategies. Each stragegy must be previously created and correctly parametrized.
00077         TeGeneralizedProxMatrix (TeProxMatrixConstructionStrategy<Set>* sc,  TeProxMatrixWeightsStrategy* sw = 0, 
00078                 TeProxMatrixSlicingStrategy* ss = 0, const TeGPMImplementation& type = TeGraphBreymann, 
00079                 const int& gpmId=-1, const bool& isDefault=false,  const string& neigsTable ="", const int& slices=1);
00080 
00081         //! Constructor based on default strategies: (a) Local adjacency of first order; (b) No siling;  and (c)  No weighs (all equal to 1).
00082         TeGeneralizedProxMatrix(TeSTElementSet* objects, TeGeomRep geom_type, const TeGPMImplementation& type = TeGraphBreymann);
00083 
00084         //! Copy constructor
00085         TeGeneralizedProxMatrix(const TeGeneralizedProxMatrix& p);
00086 
00087         //! Attribution Operator 
00088         TeGeneralizedProxMatrix& operator=(const TeGeneralizedProxMatrix& p);
00089 
00090         bool clearImplementation ();
00091 
00092         //! Comparison Operator
00093         bool operator==(const TeGeneralizedProxMatrix& p) const;
00094         
00095         
00096         /** @name getNeighbours Methods for return the neighbours
00097         *  All methods return the  neighbours of a given object in a given slice. The default is the first slice.
00098         *  Slices are defined according to the Slicing Strategy in use (e.g., according to distance zones, corresponding to neighbourhood orders, weights intervals, etc.). 
00099         *  If the parameter slice is not provided, the first slice is returned.
00100         *  The operator[] method should preferably be used 
00101         *  For each object, all the connection attributes are stored as properties (weight is the first).
00102         */
00103         //@{ 
00104         //! Return the neighbours of an object in a slice, packed in a TeNeighbours 
00105         TeNeighbours getNeighbours (const string& object_id, int slice = 1);
00106 
00107         //! Return the neighbours of an object in a slice, packed in a TeNeighboursMap
00108         TeNeighboursMap getMapNeighbours (const string& object_id, int slice = 1);
00109         
00110         //! Operator [], return the neighbours packed in a TeNeighbours
00111         TeNeighbours operator[] (const string& object_id); 
00112 
00113         //! Return the neighbours of an object and their attributes in a spatial temporal element set (TeSTElementSet)
00114         TeSTElementSet getSTENeighbours(const string& object_id); 
00115         //@}
00116 
00117         
00118         /** @name ChangeStrategies Methods for changing current strategies 
00119         *  change current strategies 
00120         */
00121         //@{ 
00122         //! Set current construction strategy 
00123         bool setCurrentConstructionStrategy (TeProxMatrixConstructionStrategy<Set>* sc);  
00124         
00125         //! Set current weights strategy 
00126         bool setCurrentWeightsStrategy (TeProxMatrixWeightsStrategy* sw); 
00127         
00128         //! Set current slicing strategy 
00129         bool setCurrentSlicingStrategy (TeProxMatrixSlicingStrategy* ss); 
00130 
00131         //! Get current construction params 
00132         TeProxMatrixConstructionParams* getConstructionParams() 
00133         {       
00134                 if(sc_)
00135                         return &(sc_->constructionParams()); 
00136                 return 0;
00137         } 
00138         
00139         //! Get current weights params 
00140         TeProxMatrixWeightsParams* getWeightsParams() 
00141         { 
00142                 if(sw_)
00143                         return &(sw_->weightsParams()); 
00144                 return 0;
00145         }  
00146         
00147         //! Get current slicing params 
00148         TeProxMatrixSlicingParams* getSlicingParams() 
00149         { 
00150                 if(ss_)
00151             return &(ss_->slicingParams()); 
00152                 return 0;
00153         } 
00154         //@}
00155         
00156 
00157         //! Reconstruct matrix and recompute weights and slicing, accornding to current strategies.
00158         bool constructMatrix ();
00159 
00160         //! Recomputes the weigths, given a new strategy. The matrix is not reconstructed.
00161         bool recomputeWeights ();
00162 
00163         //! Sets the slicing strategy for neighbours selection.
00164         bool recomputeSlicing ();
00165 
00166         //! Verify if two objects are connected
00167         bool isConnected (const string& object_id1, const string& object_id2); 
00168 
00169         //! Connect two objects
00170         bool connectObjects (const string& object_id1, const string& object_id2, const TeProxMatrixAttributes& attr); 
00171         
00172         //! Connect two objects
00173         bool connectObjects (const string& object_id1, const string& object_id2);
00174 
00175         //! Disconnect two objects
00176         bool disconnectObjects (const string& object_id1, const string& object_id2); 
00177 
00178         //! Remove object
00179         bool removeObject (const string& object_id);  
00180 
00181         //! Get connection attributes
00182         bool getConnectionAttributes (const string& object_id1, string& object_id2, TeProxMatrixAttributes& attr); 
00183 
00184         //! Return the number of objects
00185         int numberOfObjects (); 
00186 
00187         //! Return the number of slices
00188         int numerOfSlices () {return total_slices_;}
00189 
00190         //! Save the matrix in a text file
00191         bool saveTextFile (const string& name, map<string, string>* ids=0); 
00192 
00193         //! Save the matrix in a text file
00194         bool saveGALFile (const string& name, map<string, string>* ids=0);      
00195         
00196         //! Save the matrix in a text file
00197         bool saveGWTFile (const string& name, map<string, string>* ids=0); 
00198 
00199         //! Save the matrix in a text file
00200         bool saveTextFile (const string& name, vector<string>* ids); 
00201 
00202         //! Save the matrix in a text file
00203         bool saveGALFile (const string& name, vector<string>* ids);     
00204         
00205         //! Save the matrix in a text file
00206         bool saveGWTFile (const string& name, vector<string>* ids); 
00207 
00208         //! Destructor
00209         virtual ~TeGeneralizedProxMatrix()
00210         {
00211                 if (imp_) 
00212                         delete imp_; //It is not counted
00213         }
00214 };
00215 
00216 //--- template implementations ---
00217 template<typename Set>
00218 TeGeneralizedProxMatrix<Set>::TeGeneralizedProxMatrix(TeSTElementSet* objects, TeGeomRep geom_type, const TeGPMImplementation& imp_type)
00219 {
00220         gpm_id_=-1;
00221     is_default_=false; 
00222         total_slices_=1; 
00223         neighbourhood_table_=""; 
00224     impl_strategy_=TeGraphBreymann;
00225         
00226         imp_ = 0;
00227         imp_ = getImplementation(imp_type);
00228         if ((geom_type==TePOLYGONS) || (geom_type==TeCELLS))
00229         {
00230                 sc_ = new TeProxMatrixLocalAdjacencyStrategy (objects, geom_type);
00231                 ss_ = new TeProxMatrixNoSlicingStrategy;
00232                 sw_ = new TeProxMatrixNoWeightsStrategy;
00233         }
00234         else
00235         {
00236                 imp_ = 0;
00237                 sc_ = 0;
00238                 ss_ = 0;
00239                 sw_ = 0;
00240         }
00241 }
00242 
00243 template<typename Set>
00244 TeGeneralizedProxMatrix<Set>::TeGeneralizedProxMatrix (TeProxMatrixConstructionStrategy<Set>* sc,  TeProxMatrixWeightsStrategy* sw, 
00245                 TeProxMatrixSlicingStrategy* ss, const TeGPMImplementation& type, 
00246                 const int& gpmId, const bool& isDefault,  const string& neigsTable, const int& slices):
00247         sc_(sc),
00248         ss_(ss),
00249         sw_(sw),
00250         gpm_id_(gpmId), 
00251         is_default_(isDefault),
00252         total_slices_(slices),
00253         neighbourhood_table_(neigsTable),
00254         impl_strategy_(type)
00255         
00256 {
00257         
00258         imp_ = 0;
00259         imp_ = getImplementation(type);
00260         
00261         if(!ss)
00262                 ss_ = new TeProxMatrixNoSlicingStrategy();
00263 
00264         if(!sw)
00265                 sw_ = new TeProxMatrixNoWeightsStrategy();
00266 }
00267 
00268 
00269 template<typename Set> bool
00270 TeGeneralizedProxMatrix<Set>::isValid () const
00271 {
00272         if ((imp_) && (sc_) && (ss_) && (sw_))
00273                 return true;
00274 
00275         return false;
00276 }
00277 
00278 
00279 template<typename Set> TeProxMatrixImplementation*
00280 TeGeneralizedProxMatrix<Set>::getImplementation (const TeGPMImplementation& type)
00281 {
00282          if (imp_ == 0) 
00283                 imp_ = TeProxMatrixAbstractFactory::MakeConcreteImplementation (type); 
00284     return imp_;
00285 }
00286 
00287 
00288 
00289 template<typename Set>
00290 TeGeneralizedProxMatrix<Set>::TeGeneralizedProxMatrix(const TeGeneralizedProxMatrix<Set>& pm)
00291 {
00292         if (pm.imp_ == 0)
00293                 imp_ = 0;
00294         else 
00295                 imp_ = pm.imp_->createCopy ();
00296 
00297         sc_ = pm.sc_;
00298         ss_ = pm.ss_;
00299         sw_ = pm.sw_;
00300 
00301         gpm_id_=pm.gpm_id_;     
00302         is_default_=pm.is_default_;
00303         total_slices_=pm.total_slices_;
00304         neighbourhood_table_=pm.neighbourhood_table_;
00305         impl_strategy_=pm.impl_strategy_;
00306 }
00307 
00308 
00309 template<typename Set> TeGeneralizedProxMatrix<Set>& 
00310 TeGeneralizedProxMatrix<Set>::operator=(const TeGeneralizedProxMatrix<Set>& pm) 
00311 {
00312         if (*this == pm) return *this;
00313 
00314         if (imp_) delete imp_;
00315         imp_ = 0;
00316         if (pm.imp_)    imp_ = pm.imp_->createCopy ();
00317 
00318         sc_ = pm.sc_;
00319         ss_ = pm.ss_;
00320         sw_ = pm.sw_;
00321         gpm_id_=pm.gpm_id_;     
00322         is_default_=pm.is_default_;
00323         total_slices_=pm.total_slices_;
00324         neighbourhood_table_=pm.neighbourhood_table_;
00325         impl_strategy_=pm.impl_strategy_;
00326 
00327         return *this;
00328 
00329 }
00330 
00331 template<typename Set> bool 
00332 TeGeneralizedProxMatrix<Set>::operator==(const TeGeneralizedProxMatrix<Set>& pm) const
00333 {
00334         if (isValid() && pm.isValid()) 
00335         {       
00336                 if ((sc_->IsEqual (*(pm.sc_))) &&
00337                         (ss_->operator==(*(pm.ss_))) &&
00338                         (sw_->operator==(*(pm.sw_))) &&
00339                         (total_slices_ == pm.total_slices_) &&
00340                         (imp_->isEqual(*(pm.imp_)))) 
00341                         return true;
00342         } 
00343         else    
00344                 if (!isValid() && !pm.isValid()) 
00345                         return true;
00346         return false;
00347 }
00348 
00349 template<typename Set> TeNeighbours
00350 TeGeneralizedProxMatrix<Set>:: getNeighbours (const string& object_id, int slice) 
00351 {
00352         TeNeighbours neigh2;
00353         if (slice > total_slices_) 
00354                 return neigh2;
00355 
00356         if (imp_) 
00357         {
00358                 TeNeighbours neigh1;
00359                 imp_->getNeighbours (object_id, neigh1);
00360                 for (int i=0; i < neigh1.size(); i++) 
00361                         if (neigh1.Attributes(i).Slice() == slice) 
00362                                 neigh2.Insert (neigh1.ObjectId(i), neigh1.Attributes(i));
00363         }
00364 
00365         return neigh2;
00366 }
00367 
00368 
00369 template<typename Set> TeNeighbours 
00370 TeGeneralizedProxMatrix<Set>::operator[](const string& object_id) 
00371 {
00372         return getNeighbours(object_id);
00373 }
00374 
00375 
00376 template<typename Set> TeNeighboursMap
00377 TeGeneralizedProxMatrix<Set>::getMapNeighbours (const string& object_id, int slice) 
00378 {
00379         TeNeighboursMap neighMap;
00380         if (slice > total_slices_) 
00381                 return neighMap;
00382 
00383         if (imp_) 
00384         {
00385                 TeNeighbours neigh;
00386                 imp_->getNeighbours (object_id, neigh);
00387 
00388                 for (int i=0; i < neigh.size(); i++) 
00389                         if (neigh.Attributes(i).Slice() == slice) 
00390                                 neighMap[neigh.ObjectId(i)] = neigh.Attributes(i);
00391         }
00392 
00393         return neighMap;
00394 }
00395 
00396 
00397 template<typename Set> TeSTElementSet 
00398 TeGeneralizedProxMatrix<Set>::getSTENeighbours(const string& object_id)
00399 {
00400         TeSTElementSet selected_objects;
00401         if (imp_ && sc_) 
00402         {
00403                 TeNeighbours neigh;
00404                 imp_->getNeighbours (object_id, neigh);
00405                 
00406                 for (int i = 0; i < neigh.size(); i++) 
00407                 {
00408                         // Construct a sto instance with its attributes
00409                         TeSTInstance obj;
00410                         obj.objectId(neigh.ObjectId(i));  
00411                         
00412                         //load the attributes
00413                         TePropertyVector propVector;
00414                         sc_->objects()->getPropertyVector(object_id, propVector);
00415                         obj.properties(propVector);
00416                                 
00417                         // insert object in the return vector
00418                         selected_objects.insertSTInstance(obj);
00419                 }
00420         }
00421         return selected_objects;
00422 }
00423 
00424 
00425 template<typename Set> bool 
00426 TeGeneralizedProxMatrix<Set>::setCurrentConstructionStrategy (TeProxMatrixConstructionStrategy<Set>* sc)  
00427 {       
00428         if (sc == 0) 
00429                 return false; 
00430         sc_ = sc; 
00431         return true;
00432 }
00433 
00434 
00435 template<typename Set> bool 
00436 TeGeneralizedProxMatrix<Set>::setCurrentWeightsStrategy (TeProxMatrixWeightsStrategy* sw) 
00437 {       
00438         if (sw == 0) 
00439                 return false; 
00440         sw_ = sw; 
00441         return true;
00442 }
00443 
00444 template<typename Set> bool 
00445 TeGeneralizedProxMatrix<Set>::setCurrentSlicingStrategy (TeProxMatrixSlicingStrategy* ss) 
00446 {
00447         if (ss == 0) 
00448                 return false; 
00449         ss_ = ss; 
00450         return true;
00451 }
00452 
00453 template<typename Set> bool
00454 TeGeneralizedProxMatrix<Set>::constructMatrix ()
00455 {
00456         if(!isValid())
00457                 return false;
00458 
00459         //      ClearImplementation();
00460         imp_ = 0;
00461         imp_ = getImplementation();
00462 
00463         if (sc_) 
00464         {
00465                 if (sc_->Construct (imp_)) 
00466                 {
00467                         if (ss_) 
00468                                 ss_->Slice (imp_);
00469                         if (sw_) 
00470                                 sw_->ComputeWeigths (imp_); 
00471                         return true;
00472                 } 
00473         }
00474         imp_ = 0;
00475         sc_ = 0;
00476         ss_ = 0;
00477         sw_ = 0;
00478         return false;
00479 }
00480 
00481 
00482 template<typename Set> bool
00483 TeGeneralizedProxMatrix<Set>::clearImplementation ()
00484 {
00485         TeProxMatrixImplementation* aux;
00486         if (imp_ == 0)  
00487                 aux = getImplementation ();
00488         else    
00489                 aux = getImplementation (imp_->type());
00490 
00491         if (aux == 0) 
00492                 return false;
00493         
00494         delete imp_;
00495         imp_ = aux;
00496 
00497         return true;
00498 }
00499 
00500 template<typename Set> bool
00501 TeGeneralizedProxMatrix<Set>::recomputeWeights ()
00502 {
00503         if (isValid()){
00504                 sw_->ComputeWeigths (imp_); return true;
00505         } 
00506         return false;
00507 }
00508 
00509 
00510 template<typename Set> bool
00511 TeGeneralizedProxMatrix<Set>::recomputeSlicing ()
00512 {
00513         if (isValid()){
00514                 ss_->Slice (imp_); return true;
00515         } 
00516         return false;
00517 }
00518 
00519 
00520 template<typename Set> bool 
00521 TeGeneralizedProxMatrix<Set>::isConnected (const string& object_id1, const string& object_id2) 
00522 {
00523         if (imp_ == 0) 
00524                 return false;  
00525         return imp_->isConnected (object_id1, object_id2);
00526 }
00527 
00528 template<typename Set> bool 
00529 TeGeneralizedProxMatrix<Set>::connectObjects (const string& object_id1, const string& object_id2, const TeProxMatrixAttributes& attr)
00530 {
00531         if (!imp_) 
00532                 getImplementation();  
00533         imp_->connectObjects (object_id1, object_id2, attr);
00534         return true;
00535 }
00536 
00537 template<typename Set> bool 
00538 TeGeneralizedProxMatrix<Set>::connectObjects (const string& object_id1, const string& object_id2)
00539 {
00540         if (!imp_) 
00541                 getImplementation();  
00542         TeProxMatrixAttributes attr;
00543         imp_->connectObjects (object_id1, object_id2, attr);
00544         return true;
00545 }
00546 
00547 template<typename Set> bool 
00548 TeGeneralizedProxMatrix<Set>::disconnectObjects (const string& object_id1, const string& object_id2)
00549 {
00550         if (imp_ == 0) 
00551                 return false;  
00552         return imp_->disconnectObjects (object_id1, object_id2);
00553 }
00554 
00555 template<typename Set> bool 
00556 TeGeneralizedProxMatrix<Set>::removeObject (const string& object_id)
00557 {
00558         if (imp_ == 0) 
00559                 return false;  
00560         return imp_->removeObject (object_id);
00561 }
00562 
00563 template<typename Set> bool 
00564 TeGeneralizedProxMatrix<Set>::getConnectionAttributes (const string& object_id1, string& object_id2, TeProxMatrixAttributes& attr)
00565 {
00566         if (imp_ == 0) 
00567                 return false;
00568         return imp_->getConnectionAttributes (object_id1, object_id2, attr); 
00569 }
00570 
00571 template<typename Set> int  
00572 TeGeneralizedProxMatrix<Set>::numberOfObjects () 
00573 {
00574         if (imp_ == 0) 
00575                 return 0;  
00576         return imp_->numberOfObjects ();
00577 }
00578 
00579 template<typename Set> bool 
00580 TeGeneralizedProxMatrix<Set>::saveTextFile (const string& name, map<string, string>* ids) 
00581 {       
00582         if (imp_ == 0) 
00583                 return false;
00584         return imp_->saveTextFile (name, ids); 
00585 }
00586 
00587 
00588 template<typename Set> bool 
00589 TeGeneralizedProxMatrix<Set>::saveGALFile (const string& name, map<string, string>* ids) 
00590 {       
00591         if (imp_ == 0) 
00592                 return false;
00593         return imp_->saveGALFile (name, ids); 
00594 }
00595 
00596 template<typename Set> bool 
00597 TeGeneralizedProxMatrix<Set>::saveGWTFile (const string& name, map<string, string>* ids) 
00598 {       
00599         if (imp_ == 0) 
00600                 return false;
00601         return imp_->saveGWTFile (name, ids); 
00602 }
00603 
00604 template<typename Set> bool 
00605 TeGeneralizedProxMatrix<Set>::saveTextFile (const string& name, vector<string>* ids) 
00606 {       
00607         if (imp_ == 0) 
00608                 return false;
00609         return imp_->saveTextFile (name, ids); 
00610 }
00611 
00612 
00613 template<typename Set> bool 
00614 TeGeneralizedProxMatrix<Set>::saveGALFile (const string& name, vector<string>* ids) 
00615 {       
00616         if (imp_ == 0) 
00617                 return false;
00618         return imp_->saveGALFile (name, ids); 
00619 }
00620 
00621 template<typename Set> bool 
00622 TeGeneralizedProxMatrix<Set>::saveGWTFile (const string& name, vector<string>* ids) 
00623 {       
00624         if (imp_ == 0) 
00625                 return false;
00626         return imp_->saveGWTFile (name, ids); 
00627 }
00628 
00629 
00630 #endif

Generated on Sun Jul 29 04:01:21 2012 for TerraLib - Development Source by  doxygen 1.5.3