TerraLib 4.1
E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeCoverage.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 *************************************************************************************/
00029 #ifndef  __TERRALIB_INTERNAL_COVERAGE_H
00030 #define  __TERRALIB_INTERNAL_COVERAGE_H
00031 
00032 #include "TeGeometry.h"
00033 #include "TeDataTypes.h"
00034 #include "TeCoverageParams.h"
00035 #include "TeCoverageDecoder.h"
00036 #include "TeCoverageDecoderCacheLRU.h"
00037 #include "TeCoverageDecoderDatabase.h"
00038 #include "TeCoverageInterpolator.h"
00039 #include "TeCoverageInterpolatorNN.h"
00040 #include <vector>
00041 
00060 template <class T>
00061 class TeCoverage : public TeGeometry
00062 {
00063 public:
00064 
00066 
00073     TeCoverage(TeCoverageParams& params, TeCoverageInterpolator<T>* interpolator = NULL) :
00074         params_(params),
00075         interpolator_(interpolator),
00076         decoder_(NULL)
00077     {
00078     }
00079 
00081 
00084     virtual ~TeCoverage()
00085     {
00086         clear();
00087     }
00088 
00090 
00094     virtual void init()
00095     {
00096         // Initialize decoder according to persistence type
00097         if (!decoder_)
00098         {
00099             if (params_.getPersistenceType() == TePERSISTENCE_DATABASE)
00100             {
00101                 decoder_ = new TeCoverageDecoderDatabase<T>(params_);
00102                 decoder_->init();
00103             }
00104             else if (params_.getPersistenceType() == TePERSISTENCE_DATABASE_CACHELRU)
00105             {
00106                 decoder_ = new TeCoverageDecoderCacheLRU<T>(params_);
00107                 decoder_->init();
00108             }
00109         }
00110 
00111         // Initialize interpolator
00112         if (!interpolator_)
00113         {
00114             // If undefined, create default interpolator
00115             interpolator_ = createDefaultInterpolator();
00116         }
00117         interpolator_->setDecoder(decoder_);
00118 
00119         // Set box according to params
00120         setBox(params_.getBoundingBox());
00121     }
00122 
00124 
00127     virtual void clear()
00128     {
00129         if (decoder_)
00130         {
00131             delete(decoder_);
00132             decoder_ = NULL;
00133         }
00134         if (interpolator_)
00135         {
00136             delete(interpolator_);
00137             interpolator_ = NULL;
00138         }
00139     }
00140 
00142 
00145     TeGeomRep elemType()
00146     {
00147         return TeCOVERAGE;
00148     }
00149 
00151 
00154     TeCoverageParams& getParameters()
00155     {
00156         return params_;
00157     }
00158 
00172     class iterator
00173     {
00178         typedef typename std::vector<TeGeomValuePair<T> >::iterator geom_it;
00179     public:
00180 
00182         iterator(geom_it it) :
00183             it_(it)
00184         {
00185         }
00186           
00188         virtual ~iterator()
00189         {
00190         }
00191 
00193         iterator operator++()
00194         {
00195             it_++;
00196             return *this;
00197         }
00198     
00200         iterator operator++(int)
00201         {
00202             iterator temp = *this;
00203             ++it_;
00204             return temp;
00205         }
00206 
00208 
00211         T operator*()
00212         {
00213             return it_->geom;
00214         }
00215 
00217 
00220         T* operator->()
00221         {
00222             return &(it_->geom);
00223         }
00224         
00226 
00230         double operator[](int dimension)
00231         {
00232             return it_->value[dimension];
00233         }
00234         
00236         bool operator==(const iterator& rhs)
00237         {
00238             return it_ == rhs.it_;
00239         }
00240 
00242         bool operator!=(const iterator& rhs)
00243         {
00244             return it_ != rhs.it_;
00245         }
00246 
00247     protected:
00248         geom_it it_; 
00249     };   
00250 
00251     // ---------------------  End of class TeCoverage::iterator
00252 
00254 
00267     typename TeCoverage<T>::iterator begin(
00268             TePolygon& poly = TePolygon(),
00269             TeSpatialRelation relation = TeINTERSECTS)
00270     {
00271         std::vector<TeGeomValuePair<T> >& selected = selectGeomValuePairs(poly, relation);
00272         TeCoverage<T>::iterator beginIt = TeCoverage<T>::iterator(selected.begin());
00273         return beginIt;
00274     }
00275 
00277 
00290     typename TeCoverage<T>::iterator end(
00291             TePolygon& poly = TePolygon(),
00292             TeSpatialRelation relation = TeINTERSECTS)
00293     {
00294         std::vector<TeGeomValuePair<T> >& selected = selectGeomValuePairs(poly, relation);
00295         TeCoverage<T>::iterator endIt = TeCoverage<T>::iterator(selected.end());
00296         return endIt;
00297     }
00298 
00300 
00306     void evaluate(const TeCoord2D& location, std::vector<double>& value)
00307     {
00308         interpolator_->evaluate(location, value);
00309     }
00310 
00312 
00320     void evaluate(TeRaster& raster, const int rasterBand, const int coverageDimension, const TeBox& box = TeBox())
00321     {
00322         interpolator_->evaluate(raster, rasterBand, coverageDimension, box);
00323     }
00324 
00325 protected:
00326 
00327     TeCoverageParams& params_; 
00328     TeCoverageDecoder<T>* decoder_; 
00329     TeCoverageInterpolator<T>* interpolator_; 
00330 
00332 
00336     TeCoverageInterpolator<T>* createDefaultInterpolator()
00337     {
00338         // Default Coverage interpolator is nearest neighbour
00339         return new TeCoverageInterpolatorNN<T>();
00340     }
00341 
00343 
00353     std::vector<TeGeomValuePair<T> >& selectGeomValuePairs(const TePolygon& poly, const TeSpatialRelation relation)
00354     {
00355         std::vector<TeGeomValuePair<T> >& selected = lastSelectionResult_;
00356 
00357         // Search decoder only if the selection is different from the last one
00358         if ((!TeEquals(lastSelectionPolygon_, poly)) || lastSelectionRelation_ != relation)
00359         {
00360             decoder_->selectGeomValuePairs(poly, relation, selected);
00361 
00362             // Save selection parameters and result
00363             lastSelectionPolygon_ = poly;
00364             lastSelectionRelation_ = relation;
00365             lastSelectionResult_ = selected;
00366         }
00367         return selected;
00368     }
00369 
00370 private:
00371 
00372     TePolygon lastSelectionPolygon_; 
00373     TeSpatialRelation lastSelectionRelation_; 
00374     std::vector<TeGeomValuePair<T> > lastSelectionResult_; 
00375 };
00376 
00377 
00378 #endif // __TERRALIB_INTERNAL_COVERAGE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines