00001 /*************************************************************************** 00002 filewriter.cpp - description 00003 ------------------- 00004 begin : Tue May 13 2003 00005 copyright : (C) 2003 by Tim Sutton 00006 email : t.sutton@reading.ac.uk 00007 00008 ***************************************************************************/ 00009 00010 /*************************************************************************** 00011 * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 ***************************************************************************/ 00018 00019 #include "omgfilewriter.h" 00020 #include <iostream> 00021 00022 00023 OmgFileWriter::OmgFileWriter() 00024 00025 { 00026 mInputNoData=-9999.0; 00027 mOutputNoData=-9999.5; 00028 } 00029 00030 00031 OmgFileWriter::OmgFileWriter(QString theFileNameString, OmgFileWriter::FileType theFileFormat) 00032 { 00034 qDebug("Writing to file format %i",theFileFormat); 00035 //replace any spaces in the filename with underscores and 00036 //suffix the correct filename extension 00037 QString myFileNameString=theFileNameString; 00038 bool endOfStringFlag=false; 00039 while(!endOfStringFlag) 00040 { 00041 int myInt = myFileNameString.indexOf(" "); 00042 if(myInt != -1) //-1 means no match found 00043 { 00044 myFileNameString.replace(myInt,1,"_"); 00045 } 00046 else 00047 { 00048 endOfStringFlag=true; 00049 } 00050 } 00051 mFile.setFileName(theFileNameString); 00052 seperatorString=QString(" "); 00053 if (!mFile.open(QIODevice::WriteOnly)) 00054 { 00055 //std::cout << "OmgFileWriter::Cannot open file : " << myFileNameString << std::endl; 00056 isWriteableFlag=false; 00057 } 00058 else 00059 { 00060 mTextStream.setDevice(&mFile); 00061 fileNameString = myFileNameString; 00062 //std::cout << "OmgFileWriter::Opened file ... " << fileNameString << " successfully." << std::endl; 00063 isWriteableFlag=true; 00064 } 00065 } 00066 00067 OmgFileWriter::~OmgFileWriter() 00068 { 00069 } 00070 00071 bool OmgFileWriter::writeElement(float theElementFloat) 00072 { 00073 // Translate input no data to desired output nodata. This was originally implemented for the 00074 // following reason: 00075 // 00076 // Right there is a bit of kludging going on here: 00077 // 1) if you send mInputNoData out the output stream, it truncates it to -9999 00078 // 2) if gdal reads -9999 as the first cell, it assumes dataset is int 16, causeing all values 00079 // thereafter to be read as int16, losing any data after the decimal place 00080 // 3) using a decimal place wich isnt well represented by float32 will cause problems. 00081 // we initially used -9999.99 as no data, but when gdal reads this from the asc file again 00082 // it incorrectly receivese the value of -9999.8998433943 or similar. THis causes all the 00083 // stats for the file to be incorrect. So now we use -9999.5 as default 00084 // 4) in dataprocessor, comparisons of no data are made between the input file data (which is -9999 usually) 00085 // and the data processords idea of what no data shoud be. Consequently we need to rewrite nodata now. 00086 // 00087 if (theElementFloat==mInputNoData) { theElementFloat=mOutputNoData; } 00088 //if (mFile==0) 00089 //{ 00090 // return false; 00091 //} 00092 //if (mTextStream==0) 00093 //{ 00094 // return false; 00095 //} 00096 //write the number to the file 00097 mTextStream << theElementFloat << seperatorString; 00099 return true; 00100 } 00101 00102 bool OmgFileWriter::writeNoData() 00103 { 00104 mTextStream << mOutputNoData << seperatorString; 00106 return true; 00107 } 00108 00109 const QString OmgFileWriter::fileName() 00110 { 00111 return fileNameString; 00112 } 00113 00114 /* 00115 * This method sends a line break to the output file. 00116 */ 00117 bool OmgFileWriter::sendLineBreak() 00118 { 00119 //cout << "OmgFileWriter::writeElement Writing element to " << fileNameString << endl; 00120 mTextStream << QString("\n").toLocal8Bit(); 00122 return true; 00123 00124 } 00125 00126 void OmgFileWriter::close() 00127 { 00128 mFile.close(); 00129 } 00130 00131 bool OmgFileWriter::writeString(QString theQString) 00132 { 00133 //write the string to the file 00134 mTextStream << theQString; 00136 return true; 00137 00138 } 00139 00141 void OmgFileWriter::setInputNoData (float theValue) 00142 { 00143 mInputNoData=theValue; 00144 } 00145 00147 void OmgFileWriter::setOutputNoData (float theValue) 00148 { 00149 mOutputNoData=theValue; 00150 }