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 {
00024 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
00039
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
00051
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
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
00133
00134
00135
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
00164 double getDecompositionLimit();
00165
00166
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
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
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 };
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
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
00349 values = NULL;
00350
00351
00352 xsize=dataset->GetRasterXSize();
00353 ysize=dataset->GetRasterYSize();
00354
00355
00356 double thisGeoTransform[6];
00357 dataset->GetGeoTransform( thisGeoTransform );
00358 absWEResolution = fabs(thisGeoTransform[1]);
00359 absNSResolution = fabs(thisGeoTransform[5]);
00360
00361
00362
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
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
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
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
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
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
00546
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
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
00570 v.reserve(rect.xsize()*rect.ysize());
00571
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
00641
00642 T previousMeanValue = 0;
00643 T currentMeanValue = 0;
00644 T curValue = 0;
00645 T smallestValue = (T)1;
00646 T highestValue = 0;
00647
00648 T upBorderLimit = (T)probabilityLag;
00649 T downBorderLimit = (T)probabilityLag;
00650 T zeroValue = (T)0;
00651
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
00657 curValue = get(RasterCellIndex(i,j));
00658 if (curValue < smallestValue)
00659 { smallestValue = curValue; }
00660 if (curValue > highestValue)
00661 { highestValue = curValue; }
00662
00663 if (Nmeans > 0)
00664 {
00665 currentMeanValue = (currentMeanValue * ((T)Nmeans/(T)(Nmeans+1))) + (curValue / ((T)(Nmeans+1)));
00666 } else {
00667 currentMeanValue = curValue;
00668 previousMeanValue = currentMeanValue;
00669 }
00670 Nmeans++;
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 tmp = (currentMeanValue - (T)probabilityLag);
00682 if ( tmp < zeroValue)
00683 { tmp = zeroValue; }
00684 downBorderLimit = smallestValue - tmp;
00685
00686 if (downBorderLimit < zeroValue) { return false; }
00687
00688 tmp = (currentMeanValue + (T)probabilityLag);
00689 if ( tmp > (T)1)
00690 { tmp = (T)1; }
00691 upBorderLimit = tmp - highestValue;
00692
00693 if (upBorderLimit < zeroValue) { return false; }
00694
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
00705 if (sigmaFactor <= 0.0)
00706 {
00707 return false;
00708 }
00709
00710 if (sigmaFactor > 3.0)
00711 {
00712 return true;
00713 }
00714
00715 int i,j;
00716 int Nmeans = 0;
00717 T tmp = 0;
00718
00719 T currentMeanValue = 0;
00720 T sigmaVal = 0;
00721
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
00727 currentMeanValue = currentMeanValue + get(RasterCellIndex(i,j));
00728 Nmeans++;
00729 }
00730 }
00731
00732 if (Nmeans > 0)
00733 {
00734 currentMeanValue = currentMeanValue / ((T)Nmeans);
00735 } else {
00736 return false;
00737 }
00738
00739
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
00745 tmp = currentMeanValue - get(RasterCellIndex(i,j));
00746 sigmaVal = sigmaVal + tmp*tmp;
00747 }
00748 }
00749 sigmaVal = sqrt(sigmaVal/((T)Nmeans));
00750
00751
00752 if (sigmaVal <= (probabilityLag/100.0))
00753 {
00754
00755 return true;
00756 }
00757
00758 if (sigmaVal > probabilityLag)
00759 {
00760
00761 return false;
00762 }
00763
00764
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
00798 if (rect.size() <= 0)
00799 {
00800 return result;
00801 }
00802
00803
00804 std::vector<T> data;
00805 get(rect,data);
00806
00807
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
00824 if (rect.size() <= 0)
00825 {
00826 return result;
00827 }
00828
00829
00830 std::vector<T> data;
00831 get(rect,data);
00832
00833
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
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939 }
00940 }
00941
00942 #endif