TeDecoderSPR.cpp

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 
00024 #include "TeException.h"
00025 #include "TeDecoderSPR.h"
00026 #include "TeAsciiFile.h"
00027 #include "TeUtils.h"
00028 
00029 #include <cstring>
00030 #include <cstdlib>
00031 #include <fstream>
00032 using std::ifstream;
00033 using std::ofstream;
00034 
00035 TeDecoderSPR::TeDecoderSPR() : 
00036         TeDecoderSmartMem(),
00037         isModified_(false)
00038 {       
00039         params_.decoderIdentifier_ = "SPR";     
00040 }
00041 
00042 TeDecoderSPR::TeDecoderSPR (const TeRasterParams& par) : 
00043         TeDecoderSmartMem(),
00044         isModified_(false)
00045 {       
00046         params_.errorMessage_.clear();
00047         if (par.fileName_.empty())
00048                 return;
00049         params_ = par;
00050         params_.decoderIdentifier_ = "SPR";
00051         if (params_.mode_ == 'w' || params_.mode_ == 'r')
00052                 readParameters();
00053 }
00054 
00055 TeDecoderSPR::~TeDecoderSPR()
00056 {
00057         if ( isModified_ && (params_.mode_ == 'w' || params_.mode_ == 'c'))
00058         {                       // save contents to disk
00059                 try
00060                 {
00061                         TeAsciiFile sFile(params_.fileName_,"w+");
00062                         if (writeParameters(sFile))
00063                                 saveData(sFile);
00064                 }
00065                 catch (...)
00066                 {
00067                 }
00068         }
00069         TeDecoderSmartMem::clear();
00070 }
00071 
00072 bool
00073 TeDecoderSPR::clear()
00074 {
00075         if ( isModified_ && (params_.mode_ == 'w' || params_.mode_ == 'c'))
00076         {                       // save contents to disk
00077                 try
00078                 {
00079                         TeAsciiFile sFile(params_.fileName_,"w+");
00080                         if (writeParameters(sFile))
00081                                 saveData(sFile);
00082                 }
00083                 catch (...)
00084                 {
00085                 }
00086                 isModified_ = false;
00087         }
00088         TeDecoderSmartMem::clear();
00089         return true;
00090 }
00091 
00092 bool 
00093 TeDecoderSPR::readParameters()
00094 {
00095         try
00096         {
00097                 TeAsciiFile     pFile (params_.fileName_);
00098                 string name;
00099 
00100                 // check if there is the GRIDDEF section
00101                 while (pFile.isNotAtEOF())
00102                 {
00103                         name = pFile.readString();
00104                         if (name[0] =='/' && name[1] == '/')    // skip comments
00105                                 pFile.findNewLine();
00106                         else if (name == "GRIDDEF")
00107                                 break;
00108                 }
00109                 if (!pFile.isNotAtEOF())                                        // unexpected end of file
00110                         return false;
00111                 
00112                 // read INFO section
00113                 double x1, y2;
00114                 int ncols;
00115                 double dummy;
00116 
00117                 // Expected format to grid definition:
00118                 // GRIDDEF  <ncols>  <nlins>  <X1>  <Y2>  <resX>  <resY>  <nodatavalue>
00119                 params_.ncols_ = ncols = pFile.readInt();
00120                 params_.nlines_ = pFile.readInt();
00121                 x1 = pFile.readFloat();
00122                 y2 = pFile.readFloat();
00123                 params_.resx_ = pFile.readFloat();
00124                 params_.resy_ = pFile.readFloat();
00125                 params_.nBands(1);
00126                 dummy = pFile.readFloat();
00127                 params_.useDummy_ = true;
00128                 params_.setDummy(dummy);
00129 
00130                 do
00131                 {
00132                         name = pFile.readString();
00133                         if (name[0] =='/' && name[1] == '/')    // skip comments
00134                                 pFile.findNewLine();
00135                 } while (pFile.isNotAtEOF() && name != "GRIDCLASS" && name != "INFO_END");
00136                 
00137                 if (name == "GRIDCLASS" )
00138                 {
00139                         params_.setPhotometric(TeRasterParams::TePallete);
00140                         params_.setDataType(TeUNSIGNEDCHAR);
00141 
00142                         // Expected format GRIDCLASS  <nclass>  <ncolorR>  <ncolorG>  <ncolorB>  <nameclass>
00143                         params_.lutr_.clear();
00144                         params_.lutg_.clear();
00145                         params_.lutb_.clear();
00146 
00147                         params_.lutr_.insert(params_.lutr_.begin(),256,0);
00148                         params_.lutg_.insert(params_.lutg_.begin(),256,0);
00149                         params_.lutb_.insert(params_.lutb_.begin(),256,0);
00150                         
00151                         vector<string> lnames;
00152                         name = pFile.readString();
00153                         while (pFile.isNotAtEOF() && name != "INFO_END") // loop through all class definitions
00154                         {
00155                                 int index = atoi(name.c_str());
00156                                 params_.lutr_[index] = pFile.readInt(); 
00157                                 params_.lutg_[index] = pFile.readInt(); 
00158                                 params_.lutb_[index] = pFile.readInt(); 
00159                                 lnames.clear();
00160                                 pFile.readStringList(lnames);   // class name                   
00161                                 name = pFile.readString();       // next index          
00162                         } 
00163 
00164                         if (!pFile.isNotAtEOF())        // unexpected end of file
00165                                 return false;
00166                 }
00167                 else if (name == "INFO_END")
00168                 {
00169                         params_.setDataType(TeDOUBLE);
00170                         params_.setPhotometric(TeRasterParams::TeMultiBand);
00171                 }
00172                 params_.topLeftResolutionSize(x1,y2,params_.resx_,params_.resy_,
00173                                                       params_.ncols_,params_.nlines_,false);
00174                 TeProjection* pp = new TeNoProjection();
00175                 params_.projection(pp);
00176                 delete pp;
00177         }
00178         catch(...)
00179         {
00180                 return false;
00181         }
00182         return true;
00183 }
00184 
00185 bool
00186 TeDecoderSPR::readFile(const string& filename)
00187 {
00188         string valstring;
00189         try 
00190         {
00191                 TeAsciiFile     pFile (filename);
00192                 string name;
00193                 while (pFile.isNotAtEOF() && name != "INFO_END")
00194                 {
00195                         name = pFile.readString();
00196                         if (name[0] =='/' && name[1] == '/')    // skip comments
00197                                 pFile.findNewLine();
00198                 }
00199                 if (!pFile.isNotAtEOF())                                        // unexpected end of file
00200                         return false;
00201 
00202                 for (int lin = 0; lin < params_.nlines_; lin++)
00203                 {
00204                         for (int col = 0; col < params_.ncols_; col++)
00205                         {
00206                                 valstring = pFile.readString();
00207                                 setElement(col,lin,atof(valstring.c_str()));
00208                         }
00209                 }
00210         }
00211         catch(...)
00212         {
00213                 return false;
00214         }
00215         return true;
00216 }
00217 
00218 void
00219 TeDecoderSPR::init()
00220 {
00221         params_.status_= TeRasterParams::TeNotReady;
00222         int nb = params_.nBands();
00223         if (nb != 1 )
00224         {
00225                 params_.errorMessage_ = "Ascii SPRING can store raster with only 1 band.";
00226                 return;
00227         }
00228 
00229         // 1 - check if file exists and has the necessary permission
00230         if (params_.mode_ == 'c')       // creating a new file
00231         {
00232                 ofstream dataFile(params_.fileName_.c_str()); // try to (re)create the file
00233                 if (!dataFile)
00234                 {
00235                         params_.errorMessage_ = "Fail to (re)create the raster the file:" + params_.fileName_;
00236                         return;
00237                 }
00238         }
00239         else if (params_.mode_ == 'w')
00240         {
00241                 ifstream dataFile(params_.fileName_.c_str()); // check if file exists
00242                 if (!dataFile)
00243                 {
00244                         params_.errorMessage_ = "Fail to open the raster the file:" + params_.fileName_;
00245                         return; 
00246                 }
00247                 dataFile.close();
00248         }
00249         else if (params_.mode_ == 'r')
00250         {
00251                 ifstream dataFile(params_.fileName_.c_str()); // check if file exists
00252                 if (!dataFile)
00253                 {
00254                         params_.errorMessage_ = "Fail to open the raster the file:" + params_.fileName_;
00255                         return; 
00256                 }
00257                 dataFile.close();       
00258         }
00259 
00260         // 2 - check if there is memory enough to handle the data
00261         TeDecoderSmartMem::init();              
00262         if (params_.status_ == TeRasterParams::TeNotReady)
00263                 return;
00264 
00265         // 3 - try to read the data
00266         if (params_.mode_ == 'c')       // creating a new file
00267         {
00268                 try
00269                 {
00270                         params_.status_ = TeRasterParams::TeNotReady;
00271                         TeAsciiFile sFile(params_.fileName_,"w+");
00272                         if (!writeParameters(sFile) || !saveData(sFile))
00273                         {
00274                                 params_.errorMessage_ = "Fail to write the raster to file";
00275                                 return;
00276                         }
00277                         params_.status_ = TeRasterParams::TeReadyToWrite;
00278                 }
00279                 catch (...)
00280                 {
00281                         params_.errorMessage_ = "Fail to write the raster to file";
00282                 }
00283         }
00284         else if (params_.mode_ == 'w')
00285         {
00286                 if (!readFile(params_.fileName_))
00287                 {
00288                         params_.status_ = TeRasterParams::TeNotReady;
00289                         params_.errorMessage_ = "Fail to read the raster the file:" + params_.fileName_;
00290                         return; 
00291                 }
00292                 params_.status_ = TeRasterParams::TeReadyToWrite;
00293         }
00294         else if (params_.mode_ == 'r')
00295         {
00296                 if (!readFile(params_.fileName_))
00297                 {
00298                         params_.status_ = TeRasterParams::TeNotReady;
00299                         params_.errorMessage_ = "Fail to read the raster the file:" + params_.fileName_;
00300                         return; 
00301                 }
00302                 params_.status_ = TeRasterParams::TeReadyToRead;        
00303         }
00304 }
00305 
00306 bool 
00307 TeDecoderSPR::writeParameters(TeAsciiFile& pFile)
00308 {
00309         string name;
00310         try
00311         {
00312                 TeBox box = params_.boundingBox();
00313                 name = "GRIDREG\nINFO\n";
00314                 pFile.writeString(name);
00315                 name = "//Formato GRIDDEF  <ncols>  <nlins>  <X1>  <Y2>  <resX>  <resY>  <nodatavalue>\n";
00316                 pFile.writeString(name);
00317                 name = "GRIDDEF ";
00318                 pFile.writeString(name);
00319                 name = Te2String(params_.ncols_) + " " + Te2String(params_.nlines_) + " ";
00320                 pFile.writeString(name);
00321                 name = Te2String(box.x1_,6) + " " + Te2String(box.y2_,6) + " ";
00322                 pFile.writeString(name);
00323                 name = Te2String(params_.resx_,6) + " " + Te2String(params_.resy_,6) + " ";
00324                 pFile.writeString(name);
00325                 name = Te2String(params_.dummy_[0],6) + "\n";
00326                 pFile.writeString(name);
00327 
00328                 if (params_.photometric_[0] == TeRasterParams::TePallete)
00329                 {
00330                         name = "//Formato GRIDCLASS  <nclass>  <ncolorR>  <ncolorG>  <ncolorB>  <nameclass>\n";
00331                         pFile.writeString(name);
00332                         name = "GRIDCLASS\n";
00333                         pFile.writeString(name);
00334                         for (unsigned int nc=0; nc<params_.lutr_.size(); ++nc)
00335                         {
00336                                 name = Te2String(nc) + " " + Te2String(params_.lutr_[nc]);
00337                                 name += " " + Te2String(params_.lutg_[nc]) + " " + Te2String(params_.lutb_[nc]);
00338                                 name += " c" + Te2String(nc) + "\n";
00339                                 pFile.writeString(name);
00340                         }
00341                 }
00342                 name = "INFO_END\n";
00343                 pFile.writeString(name);
00344         }
00345         catch (...)
00346         {
00347                 return false;
00348         }
00349         return true;
00350 }
00351 
00352 bool 
00353 TeDecoderSPR::saveData(TeAsciiFile& pFile)
00354 {
00355         bool isi = (params_.dataType_[0] != TeDOUBLE) && (params_.dataType_[0] != TeFLOAT);
00356 
00357         FILE* fp = pFile.FilePtr();
00358         char fmt[100];
00359         if (isi)
00360                 strcpy(fmt,"%.0f ");
00361         else
00362                 strcpy(fmt,"%f ");
00363         try
00364         {
00365                 double d;
00366                 for (int l=0; l<params_.nlines_; ++l)
00367                 {
00368                         for (int c=0; c<params_.ncols_; ++c)
00369                         {
00370                                 TeDecoderSmartMem::getElement(c,l,d);
00371                                 fprintf(fp,fmt,d);
00372                         }
00373                         pFile.writeNewLine();
00374                 }
00375                 string spc = "END\n";
00376                 pFile.writeString(spc);
00377         }
00378         catch(...)
00379         {
00380                 return false;
00381         }
00382         return true;
00383 }
00384 
00385 bool 
00386 TeDecoderSPR::setElement (int col,int lin, double val, int band)
00387 {
00388         if (TeDecoderSmartMem::setElement(col,lin,val,band))
00389         {
00390                 isModified_ = true;
00391                 return true;
00392         }
00393         else
00394                 return false;
00395 }
00396 
00397 
00398 TeDecoderSPRFactory::TeDecoderSPRFactory(const string& name): 
00399         TeDecoderFactory(name) 
00400 {
00401         TeDecoderFactory::instanceName2Dec()["SPR"] = "SPR";
00402 }

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