00001 #include "GtoSConverter.h" 00002 00003 #include <qmessagebox.h> 00004 #include <iostream> 00005 #include <sstream> 00006 00007 00008 GtoSConverter::GtoSConverter( GLIMSProject *proj ){ 00009 mProj = proj; 00010 mImage = ImageInterface::getInterface(mProj); 00011 //mImage = new ImageInterface(mProj); 00012 } 00013 00014 GtoSConverter::~GtoSConverter(){ 00015 ImageInterface::destroyInterface(); 00016 } 00017 00018 void GtoSConverter::convert(std::string path){ 00019 outputFilePath = path ; 00020 getSelectedShapeList(); 00021 getSelectedShapes(); 00022 std::string str = getConversion(); 00023 // writeConversion(str, path); 00024 00025 // ImageInterface::destroyInterface(); 00026 } 00027 00028 00029 00030 void GtoSConverter::convert(std::string path, std::string delim){ 00031 outputFilePath = path ; 00032 getSelectedShapeList(); 00033 getSelectedShapes(); 00034 std::string str = getConversion(delim); 00035 // writeConversion(str, path); 00036 } 00037 00038 00039 void GtoSConverter::getSelectedShapeList(){ 00040 00041 00042 //get the lindata 00043 GLIMSLineData* linedata = &(mProj->getDataset())->getLineData(); 00044 00045 //ensure it's not a point type 00046 if(linedata->getType() == "POINT"){ 00047 QMessageBox::information( 0, "GLIMSview", "You must have a line or polygon selected." ); 00048 00049 return; 00050 } 00051 00052 //get the vector of shapes 00053 std::vector<Shape *> shapes = linedata->getLLSet(); 00054 00055 //get the list of selected shapes 00056 SelectionSet& selSetRef = linedata->getSelSet(); 00057 00058 if(selSetRef.size()<1){ 00059 QMessageBox::information( 0, "GLIMSview", "You must have at least one shape selected." ); 00060 return; 00061 } 00062 00063 //for each selected shape, add it to our list 00064 for (int i=0; i< (int)(selSetRef.size()); i++){ 00065 selectedShapes.push_back(shapes[selSetRef[i].obj]); 00066 } 00067 00068 } 00069 00070 void GtoSConverter::getSelectedShapes(){ 00071 00072 if( (selectedShapes.size()>0)){ 00073 00074 for(unsigned int i = 0 ; i < selectedShapes.size() ; i++) { 00075 00076 Shape &curShape = *selectedShapes[i]; 00077 Line& curLine = dynamic_cast<Line&>(curShape); 00078 double hx = 0 ; 00079 double lx = 0 ; 00080 double hy = 0 ; 00081 double ly = 0 ; 00082 double hlat, llat, hlon, llon; 00083 00084 std::vector<ImageCube::ThreeDPoint> points; 00085 00086 for(unsigned int j = 0 ; j < curLine.size() ; j++) { 00087 00088 double x = curLine[j].x; 00089 double y = curLine[j].y; 00090 00091 ((mProj->getImage())->getFormat())->getXY(0, x, y); 00092 00093 //a hack... 00094 //since GLIMS uses the upper left corner as a reference, 00095 //and I had assumed the middle of a pixel, we have to 00096 //shift everything over by half a pixel 00097 x-=0.5; 00098 y-=0.5; 00099 00100 00101 //check that we haven't gone over the edge 00102 //just set to the edge if we do 00103 if(x < 0) x=0.0; 00104 else if(x> mImage->getWidth()) x= (mImage->getWidth())-1; 00105 00106 if(y <0) y=0.0; 00107 else if(y> mImage->getHeight()) y = (mImage->getHeight())-1; 00108 00109 00110 if(j==0){//if it's the first run, prime max and mins 00111 hx = lx = hlat = llat = x; 00112 hy = ly = hlon = llon = y; 00113 } else {//not the first run, compare max and mins 00114 if(x>hx) hx = x; 00115 else if(x<lx) lx = x; 00116 00117 if(y>hy) hy = y; 00118 else if(y<ly) ly = y; 00119 00120 } 00121 00122 ImageCube::ThreeDPoint tpoint; 00123 tpoint.x = x; 00124 tpoint.y = y; 00125 tpoint.z = 0; 00126 points.push_back(tpoint); 00127 00128 } 00129 00130 //if we're close to the low end of a pixel, round down 00131 //highs are always rounded up 00132 if(ly!=0) ly -=0.25; 00133 if(lx!=0) lx -=0.25; 00134 00135 ImageSelectionCube *tcube = new ImageSelectionCube((int)(lx), (int)(ly), (((int)(hx+0.5))-((int)lx))+1, (((int)(hy+0.5))-((int)ly))+1); 00136 00137 llat = lx; 00138 llon = ly; 00139 hlat = hx; 00140 hlon = hy; 00141 00142 //translate to lat/lon, use the first layer as a reference 00143 ((mProj->getImage())->getFormat())->getLL(0, llat, llon); 00144 ((mProj->getImage())->getFormat())->getLL(0, hlat, hlon); 00145 00146 //add the layers, and setup the selection 00147 tcube->addBands(mImage->getCubeLatLong(llat, llon, 0, hlat, hlon, (((mProj->getImage())->getFormat())->getNumBands())-1)); 00148 //tcube->addBands(mImage->getCube((int)(lx), (int)(ly), 0, (((int)(hx+0.5))-((int)lx))+1, (((int)(hy+0.5))-((int)ly))+1, (((mProj->getImage())->getFormat())->getNumBands())-1)); 00149 00150 tcube->setSelection(points); 00151 00152 selectedCubes.push_back(tcube); 00153 00154 } 00155 } 00156 } 00157 00158 std::string GtoSConverter::getConversion(){ 00159 std::string str = ""; 00160 00161 //just grab the string for each cube 00162 for(unsigned int i = 0 ; i < selectedCubes.size() ; i++) { 00163 str.append(selectedCubes[i]->getAsString()); 00164 str.append("\n"); 00165 } 00166 return str; 00167 } 00168 00169 std::string GtoSConverter::getConversion(std::string delim){ 00170 std::string str = ""; 00171 std::stringstream ss; 00172 00173 std::vector<string> names = mImage->getBandNames(); 00174 00175 ss << "X" << delim << " Y" << delim << " "; 00176 00177 for(int j=0; j<mImage->getBands(); j++){ 00178 ss << names[j] << delim << " "; 00179 } 00180 00181 ss << "\n"; 00182 00183 str = ss.str(); 00184 00185 std::ofstream f( outputFilePath.c_str() ); 00186 if ( f ) { 00187 QString qstr = QString(str.c_str()); 00188 00189 f.write( (const char*)qstr, qstr.length() ); 00190 f.flush(); 00191 00192 //just grab the string for each cube 00193 for(unsigned int i = 0 ; i < selectedCubes.size() ; i++) { 00194 //str.append(selectedCubes[i]->getAsString(delim)); 00195 selectedCubes[i]->getAsString(delim, f) ; 00196 //str.append("\n"); 00197 } 00198 } 00199 f.close(); 00200 00201 return str; 00202 } 00203 00204 void GtoSConverter::writeConversion(std::string str, std::string path){ 00205 std::ofstream f( path.c_str() ); 00206 if ( f ) { 00207 QString qstr = QString(str.c_str()); 00208 00209 f.write( (const char*)qstr, qstr.length() ); 00210 f.flush(); 00211 } 00212 f.close(); 00213 } 00214 00215
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |