Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RasterBand.hpp
Go to the documentation of this file.
00001 
00012 #ifndef LGL_RASTERBAND_HPP
00013 #define LGL_RASTERBAND_HPP
00014 
00015 #include <limits>
00016 #include <iostream>
00017 #include <boost/static_assert.hpp>
00018 
00019 #include <gdal_priv.h>
00020 
00021 #include <lgl/Raster.hpp>
00022 
00023 namespace jafar { // namespace jafar
00024   namespace lgl { // namespace lgl
00025     
00026     template<typename T>
00027     struct DataType {
00028       static const bool value=false;
00029     };
00030     
00031     template<>
00032     struct DataType<double> {
00033       static const bool value=true;
00034       static const GDALDataType dataType = GDT_Float64;
00035       static const double min() { return 0.0; };
00036       static const double max() { return 1.0; };
00037       /*
00038       const static double MIN = 0.0;
00039       const static double MAX = 1.0;        
00040       */
00041     };
00042 
00043     template<>
00044     struct DataType<float> {
00045       static const bool value=true;
00046       static const GDALDataType dataType = GDT_Float32;
00047       static const double min() { return 0.0; };
00048       static const double max() { return 1.0; };
00049       /*
00050       const static double MIN = 0.0;
00051       const static double MAX = 1.0;        
00052       */
00053     };
00054     
00055     template<>
00056     struct DataType<unsigned char> {
00057       static const bool value=true;
00058       static const GDALDataType dataType = GDT_Byte;
00059       static const unsigned char min() { return 0;  };
00060       static const unsigned char max() { return 255;};        
00061     };
00062     
00063     template<>
00064     struct DataType<unsigned int> {
00065       static const bool value=true;
00066       static const GDALDataType dataType = GDT_UInt32;
00067       static const int min() { return 0;};
00068       static const int max() { return UINT_MAX;};   
00069     };
00070     
00071     enum FUSION_METHOD {MAX, MIN, MEAN, BAYES};
00072     enum HOMOGENEOUS_TYPE {HT_NONE=-1, SIMPLE, NEARLY, SIGMA};
00073       
00077     template <typename T>
00078     class GDRasterBand : public Raster { //{{{
00079         
00080       public:
00091         GDRasterBand(GDALDataset* _dataset, GDALRasterBand *_band, 
00092               double _weight=1.0,
00093               double _decompositionLimit=8.0,
00094               FUSION_METHOD _fmethod=MEAN,
00095               HOMOGENEOUS_TYPE _homogType=SIGMA);
00096         
00097         ~GDRasterBand();
00098         
00099         void setGDALRasterBand(GDALRasterBand* _band);      
00100 
00101         //-------- Getters --------//
00102         GDALRasterBand* getGDALRasterBand();
00103 
00104         double getWeight();
00105         
00106         int getTypeSize()const;
00107         GDALDataType getType()const;
00108               
00109         int getxroot() const;     
00110         int getyroot() const;     
00111 
00113         int getxsize() const { return xsize; }
00115         int getysize() const { return ysize; };
00116 
00118         double getxsize_m() const;      
00120         double getysize_m() const;      
00121 
00123         double getxsize_m(const RasterRect& rect) const;      
00125         double getysize_m(const RasterRect& rect) const;      
00126 
00127         double getScale() const;
00128 
00130         void reset(T _v); 
00131 
00132         // a raster object should be able to return the data for a cluster
00133         // or a raster cell
00134         //T getData(const RasterCellIndex& cell) const;   
00135         //T getData(const RasterRect& rect) const;  
00136         T getValue(const RasterPoint& rpt) const; 
00137 
00138         T getValue(int i) const;  
00139 
00141         T* getValuePtr() const; 
00142 
00151         bool getValue(const int& x, const int& y, void* value) const;
00152 
00158         T get(const RasterCellIndex& cell) const;
00159 
00160         bool get(const RasterRect& rect, std::vector<T>& v) const;
00161 
00162         //void* getBlock(const RasterRect&) const;
00164         double getDecompositionLimit();
00165         
00166         //-------- Setters --------//
00167 
00168         void setValue(const RasterCellIndex& cell, T value) const;
00169         void setValue(const int index, T value) const;
00170 
00171         void setScale(double _scale);
00172 
00174         void setDecompositionLimit(double _dl);
00175 
00176         //-------- Calculation/Operations --------//
00177 
00181         bool sameValue(const RasterRect& rect) const;     
00182 
00187         bool nearlySameValue(const RasterRect& rect) const;
00188 
00193         bool sigmaSameValue(const RasterRect& rect) const;
00194 
00198         void setProbabilityLag(T newProbaLag);
00199 
00203         void setSigmaFactor(T newSigmaFactor);
00204 
00208         T getProbabilityLag() const;
00209 
00213         T getSigmaFactor() const;
00214 
00215         double mean(const RasterRect& rect) const;      
00216         double sd(const RasterRect& rect) const;
00217         
00221         // given a rect ((top,left),(right,bottom)), tell if this is homogeneous
00222         virtual bool isHomogeneous(const RasterRect& rect) const=0;
00223 
00225         HOMOGENEOUS_TYPE getHomogeneousType() const;
00226 
00228         void setHomogeneousType(HOMOGENEOUS_TYPE hhh); 
00229 
00235         bool  bufferToMem();
00236 
00238         bool  memToBuffer();
00239 
00240       protected:
00242         GDALDataset *dataset;
00243 
00247         int xsize;
00248 
00252         int ysize;
00253         
00255         double absWEResolution;
00256 
00258         double absNSResolution;
00259 
00261         GDALRasterBand* band;
00262 
00264         GDALDataType dataType;
00265 
00267         double weight;
00268 
00273         double decompositionLimit;
00274 
00276         FUSION_METHOD fmethod;
00277         
00287         T* values;
00288 
00294         T probabilityLag;
00295 
00306         T sigmaFactor;
00307 
00312         HOMOGENEOUS_TYPE homogType;
00313 
00314       private :
00316         BOOST_STATIC_ASSERT(DataType<T>::value);
00317 
00318     }; //}}} end of class GDRasterBand 
00319 
00320 
00325      template<typename T>
00326      GDRasterBand<T>::GDRasterBand(GDALDataset* _dataset, GDALRasterBand *_band, double _weight, double _decompositionLimit, FUSION_METHOD _fmethod, HOMOGENEOUS_TYPE _homogType):
00327       dataset(_dataset),
00328       band(_band),
00329       weight(_weight),
00330       decompositionLimit(_decompositionLimit),
00331       fmethod(_fmethod),
00332       homogType(_homogType)
00333     { //{{{
00334       
00335       if ((dataset==NULL)||(band==NULL)) {
00336         std::cout << "#WWW# [RasterBand] Create raster band with NULL dataset and gdal band " << std::endl;
00337         return;
00338       }     
00339       
00340       //std::cout << "#III# [RasterBand] Load raster band of type : " << GDALGetDataTypeName(band->GetRasterDataType()) << std::endl;
00341       
00342       if ((band->GetRasterDataType()!=GDT_Byte)
00343           &&(band->GetRasterDataType()!=GDT_UInt16)) {
00344         std::cout << "#EEE# [RasterBand] Error, the band type should be Byte or Uint16 " << std::endl;
00345         return;
00346       }
00347 
00348       // NULL pointing values array
00349       values = NULL;
00350 
00351       // Nb of columns and lines
00352       xsize=dataset->GetRasterXSize();
00353       ysize=dataset->GetRasterYSize();
00354 
00355       // Getting the resolution
00356       double thisGeoTransform[6];
00357       dataset->GetGeoTransform( thisGeoTransform );
00358       absWEResolution = fabs(thisGeoTransform[1]);
00359       absNSResolution = fabs(thisGeoTransform[5]);
00360 
00361       // buffer the data to memory and make the conversion from input
00362       // type (uint8 or uint16) to double value between [0,1]
00363       bufferToMem();
00364 
00365       dataType = GDT_Float64;
00366 
00367       setProbabilityLag((T)0.05);
00368       setSigmaFactor((T)1.5);
00369     } //}}}
00370      
00371     template<typename T>
00372     GDRasterBand<T>::~GDRasterBand() {
00373       free(values);
00374     }
00375     
00376     template<typename T>
00377     bool GDRasterBand<T>::bufferToMem() 
00378     {//{{{
00379       if (values != NULL)
00380       {
00381         free(values);
00382       }
00383       values = (T*)malloc(sizeof(T)*xsize*ysize);  
00384       
00385       // convert all to double
00386       double* tmp = (double*)malloc(sizeof(double)*xsize*ysize);    
00387       band->RasterIO( GF_Read, 0, 0, xsize , ysize, tmp, xsize, ysize, GDT_Float64, 0, 0 );
00388       
00389       T inputMin;
00390       T inputMax;
00391       if (band->GetRasterDataType()==GDT_Byte) {
00392         inputMin = (T)(std::numeric_limits<unsigned char>::min());
00393         inputMax = (T)(std::numeric_limits<unsigned char>::max());
00394       } else if (band->GetRasterDataType()==GDT_UInt16) {
00395         inputMin = (T)(std::numeric_limits<unsigned short>::min());
00396         inputMax = (T)(std::numeric_limits<unsigned short>::max());
00397       } else {
00398         return false;
00399       }
00400       
00401       T constFactor = ((DataType<T>::max() - DataType<T>::min())/(inputMax-inputMin));
00402       for (int j=0;j<ysize;j++) {
00403         for (int i=0;i<xsize;i++) {
00404           // saving data line by line (X axis first)
00405           values[i+xsize*j] = (T)((tmp[i+xsize*j]-inputMin)*constFactor);
00406         }
00407       }
00408       
00409       free(tmp);
00410       return true;
00411     }//}}}
00412     
00413     template<typename T>
00414     bool GDRasterBand<T>::memToBuffer()
00415     { //{{{
00416       // convert all to double
00417       double* tmp = (double*)malloc(sizeof(double)*xsize*ysize);    
00418 
00419       T inputMin;
00420       T inputMax;
00421       if (band->GetRasterDataType()==GDT_Byte) {
00422         inputMin = (T)(std::numeric_limits<unsigned char>::min());
00423         inputMax = (T)(std::numeric_limits<unsigned char>::max());
00424       } else if (band->GetRasterDataType()==GDT_UInt16) {
00425         inputMin = (T)(std::numeric_limits<unsigned short>::min());
00426         inputMax = (T)(std::numeric_limits<unsigned short>::max());
00427       } else {
00428         return false;
00429       }
00430 
00431       for (int j=0;j<ysize;j++) {
00432         for (int i=0;i<xsize;i++) {
00433           // Buffering data line by line (X axis first)
00434           tmp[i+xsize*j] = (T)(values[i+xsize*j]*((inputMax-inputMin)/(DataType<T>::max() - DataType<T>::min())));
00435         }
00436       }
00437 
00438       band->RasterIO( GF_Write, 0, 0, xsize , ysize, tmp, xsize, ysize, GDT_Float64, 0, 0 );    
00439 
00440       free(tmp);
00441 
00442       return true;
00443     } //}}}
00444 
00445     template<typename T>
00446     void GDRasterBand<T>::setGDALRasterBand(GDALRasterBand* _band) {
00447       band=_band;  
00448     }
00449 
00450     template<typename T>
00451     GDALRasterBand* GDRasterBand<T>::getGDALRasterBand() {
00452       return band;
00453     }
00454 
00455 
00456     template<typename T>
00457     int GDRasterBand<T>::getxroot() const {
00458       return 0;
00459     }
00460 
00461     template<typename T>
00462     int GDRasterBand<T>::getyroot() const {
00463       return 0;
00464     }
00465 
00466     template<typename T>
00467     double GDRasterBand<T>::getxsize_m() const {
00468       return ((double)xsize)*absWEResolution;
00469     }
00470 
00471     template<typename T>
00472     double GDRasterBand<T>::getysize_m() const {
00473       return ((double)ysize)*absNSResolution;
00474     }
00475 
00476     template<typename T>
00477     double GDRasterBand<T>::getxsize_m(const RasterRect& rect) const {
00478       return ((double)(rect.xsize()))*absWEResolution;
00479     }
00480 
00481     template<typename T>
00482     double GDRasterBand<T>::getysize_m(const RasterRect& rect) const {
00483       return ((double)(rect.ysize()))*absNSResolution;
00484     }
00485 
00486     template<typename T>
00487     double GDRasterBand<T>::getScale() const {
00488       return (double)band->GetScale();
00489     }
00490 
00491     template<typename T>
00492     void GDRasterBand<T>::setScale(double _scale) {
00493       band->SetScale((double)_scale);
00494     }
00495 
00496     // get the weight of this band  
00497     template<typename T>
00498     double GDRasterBand<T>::getWeight() {
00499       return weight;
00500     }
00501 
00502     template<typename T>
00503     int GDRasterBand<T>::getTypeSize() const {
00504       return GDALGetDataTypeSize(dataType);
00505     }
00506    
00507     template<typename T>
00508     GDALDataType GDRasterBand<T>::getType() const {
00509       return dataType;
00510     }
00511      
00512     template<typename T>
00513     T GDRasterBand<T>::get(const RasterCellIndex& cell) const {
00514       return values[cell.i+xsize*cell.j];
00515     }
00516 
00517     template<typename T>
00518     void GDRasterBand<T>::setValue(const RasterCellIndex& cell, T value) const {
00519       values[cell.i+xsize*cell.j]=value;
00520     }
00521 
00522     template<typename T>
00523     void GDRasterBand<T>::setValue(const int index, T value) const {
00524       values[index]=value;
00525     }
00526 
00527     template<typename T>
00528     T* GDRasterBand<T>::getValuePtr() const {
00529       return values;
00530     }
00531 
00532     template<typename T>
00533     T GDRasterBand<T>::getValue(const RasterPoint& rpt) const {
00534       return values[rpt.x+xsize*rpt.y];
00535     }
00536 
00537     template<typename T>
00538     T GDRasterBand<T>::getValue(const int i) const {
00539       return values[i];
00540     }
00541 
00542     template<typename T>
00543     bool GDRasterBand<T>::getValue(const int& x, const int& y, void* value) const 
00544     {//{{{
00545       // read at (x,y) pixel with RasterIO
00546       // automatic conversion from unsigned byte to double 
00547       if ( band->RasterIO( GF_Read, x, y, 1, 1, value, 1, 1, dataType, 0, 0 ) == CE_Failure )
00548       {
00549         return false;
00550       }
00551       return true;
00552     }//}}}
00553 
00554     template<typename T>
00555     void GDRasterBand<T>::reset(T _v)
00556     {//{{{
00557       for (int j=0;j<ysize;j++) {
00558         for (int i=0;i<xsize;i++) {
00559           // saving data line by line (X axis first)
00560           values[i+xsize*j] = _v;
00561         }
00562       }
00563     }//}}}
00564 
00565     template<typename T>
00566     bool GDRasterBand<T>::get(const RasterRect& rect, std::vector<T>& v) const
00567     {//{{{
00568       int i,j;
00569       // modify vector capacity 
00570       v.reserve(rect.xsize()*rect.ysize());
00571       // Fill vector with raster values in the ROI specified by rect
00572       for (j = rect.ytopleft; j <= rect.yend(); j++) {
00573         for (i = rect.xtopleft; i <= rect.xend(); i++) {
00574           v.push_back(values[i+xsize*j]);
00575         }
00576       }
00577       return true;
00578     }//}}}
00579    
00580     template<typename T>
00581     bool GDRasterBand<T>::sameValue(const RasterRect& rect) const
00582     {//{{{  
00583       int i,j;
00584       T previous_value, cur_value = 0;
00585       previous_value = get(RasterCellIndex(rect.xtopleft,rect.ytopleft));
00586       for (i=rect.xtopleft; i < rect.xtopleft+rect.xsize(); i++) {
00587       for (j=rect.ytopleft; j < rect.ytopleft+rect.ysize(); j++) {
00588           cur_value = get(RasterCellIndex(i,j));
00589           if (cur_value != previous_value) {
00590             return false;
00591           }
00592           previous_value=cur_value;
00593         }
00594       }
00595       return true;
00596     }//}}}
00597 
00598     template<typename T>
00599     T GDRasterBand<T>::getProbabilityLag() const
00600     {//{{{
00601       return probabilityLag;
00602     }//}}}
00603 
00604     template<typename T>
00605     T GDRasterBand<T>::getSigmaFactor() const
00606     {//{{{
00607       return sigmaFactor;
00608     }//}}}
00609 
00610     template<typename T>
00611     void GDRasterBand<T>::setHomogeneousType(HOMOGENEOUS_TYPE hhh) 
00612     {//{{{
00613       homogType = hhh;
00614     }//}}}
00615 
00616     template<typename T>
00617     HOMOGENEOUS_TYPE GDRasterBand<T>::getHomogeneousType() const
00618     {//{{{
00619       return homogType;
00620     }//}}}
00621 
00622     template<typename T>
00623     void GDRasterBand<T>::setProbabilityLag(T newProbaLag) 
00624     {//{{{
00625       probabilityLag = newProbaLag;
00626     }//}}}
00627 
00628     template<typename T>
00629     void GDRasterBand<T>::setSigmaFactor(T newSigmaFactor) 
00630     {//{{{
00631       sigmaFactor = newSigmaFactor;
00632     }//}}}
00633 
00634     template<typename T>
00635     bool GDRasterBand<T>::nearlySameValue(const RasterRect& rect) const
00636     {//{{{  
00637       int i,j;
00638       int Nmeans = 0;
00639       T tmp = 0;
00640       // Mean and value
00641       //T meanVariation = 0;
00642       T previousMeanValue = 0;
00643       T currentMeanValue = 0;
00644       T curValue = 0;
00645       T smallestValue = (T)1;
00646       T highestValue = 0;
00647       // The first value will be the mean and 
00648       T upBorderLimit   = (T)probabilityLag;
00649       T downBorderLimit = (T)probabilityLag;
00650       T zeroValue = (T)0;
00651       //--- Scanning the rectangle zone
00652       for (i=rect.xtopleft; i < rect.xtopleft+rect.xsize(); i++)
00653       {
00654         for (j=rect.ytopleft; j < rect.ytopleft+rect.ysize(); j++) 
00655         {
00656           //--- Getting value and average at i j
00657           curValue = get(RasterCellIndex(i,j));
00658           if (curValue < smallestValue)
00659           { smallestValue = curValue; }
00660           if (curValue > highestValue)
00661           { highestValue = curValue; }
00662           // Running mean of the raster rect zone
00663           if (Nmeans > 0)
00664           {
00665             currentMeanValue = (currentMeanValue * ((T)Nmeans/(T)(Nmeans+1))) + (curValue / ((T)(Nmeans+1)));
00666           } else { // Initial Case
00667             currentMeanValue  = curValue;
00668             previousMeanValue = currentMeanValue;
00669           }
00670           Nmeans++;
00671 
00672           //--- Checking if up and down limits are respected else zone is not homogeneous
00673           //meanVariation = previousMeanValue - currentMeanValue;
00674           // Zone not nearly homogeneous
00675           //if ((downBorderLimit - meanVariation) < zeroValue)
00676           //{ return false; }
00677           // Zone not nearly homogeneous
00678           //if ((upBorderLimit + meanVariation)   < zeroValue)
00679           //{ return false; }
00680           // Setting down-limit
00681           tmp = (currentMeanValue - (T)probabilityLag);
00682           if ( tmp < zeroValue)
00683           { tmp = zeroValue; }
00684           downBorderLimit = smallestValue - tmp;
00685           // smallest value out of zone segmentation
00686           if (downBorderLimit < zeroValue) { return false; }
00687           // Setting up-limit
00688           tmp = (currentMeanValue + (T)probabilityLag);
00689           if ( tmp > (T)1)
00690           { tmp = (T)1; }
00691           upBorderLimit = tmp - highestValue;
00692           // highest value out of zone segmentation
00693           if (upBorderLimit < zeroValue) { return false; }
00694           // adjusting for next step
00695           previousMeanValue = currentMeanValue;
00696         }
00697       }
00698       return true;
00699     }//}}}
00700 
00701     template<typename T>
00702     bool GDRasterBand<T>::sigmaSameValue(const RasterRect& rect) const
00703     {//{{{
00704       // All values are considered the same in the next case.
00705       if (sigmaFactor <= 0.0)
00706       {
00707         return false;
00708       }
00709 
00710       if (sigmaFactor > 3.0)
00711       {
00712         return true;
00713       }
00714       //-- else calculate and test sigma
00715       int i,j;
00716       int Nmeans = 0;
00717       T tmp = 0;
00718       // Mean and value
00719       T currentMeanValue =  0;
00720       T sigmaVal = 0;
00721       //--- Scanning the rectangle zone
00722       for (i=rect.xtopleft; i < rect.xtopleft+rect.xsize(); i++)
00723       {
00724         for (j=rect.ytopleft; j < rect.ytopleft+rect.ysize(); j++) 
00725         {
00726           //--- Getting value and finding average
00727           currentMeanValue = currentMeanValue + get(RasterCellIndex(i,j));
00728           Nmeans++;
00729         }
00730       }
00731       //--- Getting mean
00732       if (Nmeans > 0)
00733       {
00734         currentMeanValue = currentMeanValue / ((T)Nmeans);
00735       } else {
00736         return false;
00737       }
00738 
00739       //--- Calculating Sigma
00740       for (i=rect.xtopleft; i < rect.xtopleft+rect.xsize(); i++)
00741       {
00742         for (j=rect.ytopleft; j < rect.ytopleft+rect.ysize(); j++) 
00743         {
00744           //--- Getting value and finding average
00745           tmp = currentMeanValue - get(RasterCellIndex(i,j));
00746           sigmaVal = sigmaVal + tmp*tmp;
00747         }
00748       }
00749       sigmaVal = sqrt(sigmaVal/((T)Nmeans));
00750 
00751       //--- Checking for very low sigmas too narrow-probability-spread cells.
00752       if (sigmaVal <= (probabilityLag/100.0))
00753       {
00754         // is homogeneous
00755         return true;
00756       }
00757       //--- Checking for high sigmas too widely-probability-spread cells.
00758       if (sigmaVal > probabilityLag)
00759       {
00760         // is hereterogeneous
00761         return false;
00762       }
00763 
00764       //--- Checking for homogeneity
00765       for (i=rect.xtopleft; i < rect.xtopleft+rect.xsize(); i++)
00766       {
00767         for (j=rect.ytopleft; j < rect.ytopleft+rect.ysize(); j++) 
00768         {
00769           tmp = fabs(currentMeanValue - get(RasterCellIndex(i,j)));
00770           if (tmp > sigmaFactor * sigmaVal)
00771           {
00772             return false;
00773           }
00774         }
00775       }
00776       return true;
00777     }//}}}
00778 
00779     template<typename T>
00780     double GDRasterBand<T>::getDecompositionLimit() {
00781       return decompositionLimit;
00782     }
00783 
00784     template<typename T>
00785     void GDRasterBand<T>::setDecompositionLimit(double _dl) {
00786       decompositionLimit = _dl;
00787     }
00788 
00793    template<typename T>
00794    double GDRasterBand<T>::mean(const RasterRect& rect) const {
00795     double result = -1.0;
00796 
00797     // Bad case
00798       if (rect.size() <= 0)
00799     {
00800       return result;
00801     }
00802 
00803     // Get the data
00804     std::vector<T> data;
00805     get(rect,data);
00806     
00807     // Calculate the mean of data
00808     double sum = 0.0;
00809     for (int i=0; i<rect.size(); i++) {
00810       sum += (double)data[i];
00811     }
00812     result = sum/(double)rect.size();
00813 
00814     return result;
00815    }
00816 
00817    template<typename T>
00818    double  GDRasterBand<T>::sd(const RasterRect& rect) const {
00819     double result = -1.0;
00820     double mean   = GDRasterBand<T>::mean(rect);
00821     double tmp = 0.0;
00822 
00823     // Bad case
00824       if (rect.size() <= 0)
00825     {
00826       return result;
00827     }
00828 
00829     // get the data
00830     std::vector<T> data;
00831     get(rect,data);
00832 
00833     // Calculate the standard deviation
00834     result = 0.0;
00835     for (int i=0;i<rect.size();i++) {
00836       tmp = mean - (double)data[i];
00837       tmp = tmp * tmp;
00838       result += tmp;
00839     }
00840     result = result/(double)(rect.size());
00841   
00842     return result;
00843    }
00844 
00845    
00850 //   template<typename T>
00851 //   void* GDRasterBand<T>::getBlock(const RasterRect& rect) const {
00852 //    
00853 //    int i,j,k,m,l;
00854 //    l=0;
00855 //    int nXBlocks, nYBlocks, nXBlockSize, nYBlockSize;
00856 //   
00857 //    // the data size
00858 //    int dataSize = GDALGetDataTypeSize(dataType);
00859 // 
00860 //    // alloc the value
00861 //    void* value = malloc(dataSize*1);
00862 //   
00863 //    void* block = (unsigned char*)malloc(dataSize*rect.size());
00864 //   
00865 //    band->GetBlockSize( &nXBlockSize, &nYBlockSize );
00866 //   
00867 //    // if the wanted block has a small size
00868 //    if (((nXBlockSize == 1) && (nYBlockSize==1)) || (rect.size()<= 4)) {
00869 //      // read pixel by pixel
00870 //      for (i=rect.xtopleft;i<rect.xtopleft+rect.xsize();i++) {
00871 //       for (j=rect.ytopleft;j<rect.ytopleft+rect.ysize();j++) {
00872 //        k = (i-rect.xtopleft)*rect.ysize() + (j-rect.ytopleft);
00873 //        if (getValue(i,j,dataType,value)) {
00874 //          swapPixelValue(block,value,k,0);
00875 //          l++;
00876 //        } 
00877 //       }
00878 //      }
00879 //    } else {
00880 //      // read data by block by block way
00881 //      int        iXBlock, iYBlock;
00882 //      int        iBlock_xbegin, iBlock_ybegin;
00883 //      void* papyData;
00884 //      RasterRect interRect;
00885 // 
00886 //      // read block by block
00887 // 
00888 //      // number of blocks of the raster band
00889 //      nXBlocks = (band->GetXSize() + nXBlockSize - 1) / nXBlockSize;
00890 //      nYBlocks = (band->GetYSize() + nYBlockSize - 1) / nYBlockSize;
00891 //   
00892 //      // buffer contains current read block from band data
00893 //      papyData = CPLMalloc(dataSize * nXBlockSize * nYBlockSize);
00894 //   
00895 //      // iterate all blocks : iXBlock = horizontal block offset, iYBlock = vertical block offset
00896 //      for( iXBlock = 0; iXBlock < nXBlocks; iXBlock++ ) {
00897 //       for( iYBlock = 0; iYBlock < nYBlocks; iYBlock++ ) {
00898 // 
00899 //        // for each block, check if the wanted block data is inside the block
00900 //        // compute the xroot,yroot of the current block
00901 //        iBlock_xbegin = iXBlock*nXBlockSize;
00902 //        iBlock_ybegin = iYBlock*nYBlockSize;
00903 //      
00904 //        // if the current read block contains data for wanted block
00905 //        if (rect.intersects(RasterRect(iBlock_xbegin,iBlock_ybegin,nXBlockSize,nYBlockSize),interRect)) {
00906 //          // cout << "read block " << iXBlock << ":" << iYBlock << "rect :" << interRect << endl; 
00907 //          band->ReadBlock(iXBlock,iYBlock,papyData);
00908 //          // for each pixel in the intersection
00909 //          for (i=interRect.xtopleft;i<interRect.xtopleft+interRect.xsize();i++) {
00910 //           for (j=interRect.ytopleft;j<interRect.ytopleft+interRect.ysize();j++) {
00911 //            // from raster cell index (i,j), compute the index 
00912 //            k = (i-rect.xtopleft)*rect.ysize() + (j-rect.ytopleft);
00913 //            m = (i-iBlock_xbegin)*nYBlockSize + (j-iBlock_ybegin);
00914 //            // cout << "read pixel " << i << ":" << j << " k=" << k<< " m=" << m << endl; 
00915 //            swapPixelValue(block,papyData,k,m); 
00916 //            // increment the read pixel number
00917 //            l++;
00918 //           }
00919 //          }
00920 //        }
00921 //        if (l>=rect.size())
00922 //          continue;     
00923 //       }
00924 //       if (l>=rect.size())
00925 //        continue;
00926 //      }
00927 //   
00928 //      free(papyData);  
00929 //    }
00930 // 
00931 //    if (l!=rect.size()) 
00932 //      cout << "[RasterBand] Read block error, should read " << rect.size() << " values instead of " << l << endl;
00933 //   
00934 //    return block;
00935 //   }
00936 // 
00937    
00938 
00939   }
00940 }
00941 
00942 #endif /* LGL_RASTERBAND_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS