PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/kstars/tools/modcalcgeodcoord.cpp

https://gitlab.com/g10h4ck/kstars
C++ | 395 lines | 273 code | 93 blank | 29 comment | 38 complexity | c84804afeb01f3d54e642665e4a4a05b MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /***************************************************************************
  2. modcalcgeodcoord.cpp - description
  3. -------------------
  4. begin : Tue Jan 15 2002
  5. copyright : (C) 2002 by Pablo de Vicente
  6. email : vicente@oan.es
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "modcalcgeodcoord.h"
  17. #include <QTextStream>
  18. #include <QFileDialog>
  19. #include <KMessageBox>
  20. #include <KLocalizedString>
  21. #include "dms.h"
  22. #include "geolocation.h"
  23. #include "kstars.h"
  24. #include "kstarsdata.h"
  25. #include "widgets/dmsbox.h"
  26. modCalcGeodCoord::modCalcGeodCoord(QWidget *parentSplit)
  27. : QFrame(parentSplit) {
  28. QStringList ellipsoidList;
  29. ellipsoidList << "IAU76" << "GRS80" << "MERIT83" << "WGS84" << "IERS89";
  30. setupUi(this);
  31. spheRadio->setChecked(true);
  32. ellipsoidBox->insertItems(5,ellipsoidList);
  33. geoPlace = new GeoLocation( dms(0), dms(0));
  34. showLongLat();
  35. setEllipsoid(0);
  36. show();
  37. connect(Clear, SIGNAL(clicked()), this, SLOT(slotClearGeoCoords()));
  38. connect(Compute, SIGNAL(clicked()), this, SLOT(slotComputeGeoCoords()));
  39. }
  40. modCalcGeodCoord::~modCalcGeodCoord(){
  41. delete geoPlace;
  42. }
  43. void modCalcGeodCoord::showLongLat(void)
  44. {
  45. KStarsData* data = KStarsData::Instance();
  46. LongGeoBox->show( data->geo()->lng() );
  47. LatGeoBox->show( data->geo()->lat() );
  48. AltGeoBox->setText( QString("0.0") );
  49. }
  50. void modCalcGeodCoord::setEllipsoid(int index) {
  51. geoPlace->changeEllipsoid(index);
  52. }
  53. void modCalcGeodCoord::getCartGeoCoords (void)
  54. {
  55. geoPlace->setXPos( XGeoBox->text().toDouble()*1000. );
  56. geoPlace->setYPos( YGeoBox->text().toDouble()*1000. );
  57. geoPlace->setZPos( ZGeoBox->text().toDouble()*1000. );
  58. }
  59. void modCalcGeodCoord::getSphGeoCoords (void)
  60. {
  61. geoPlace->setLong( LongGeoBox->createDms() );
  62. geoPlace->setLat( LatGeoBox->createDms() );
  63. geoPlace->setHeight( AltGeoBox->text().toDouble() );
  64. }
  65. void modCalcGeodCoord::slotClearGeoCoords (void)
  66. {
  67. geoPlace->setLong( dms(0.0) );
  68. geoPlace->setLat( dms(0.0) );
  69. geoPlace->setHeight( 0.0 );
  70. LatGeoBox->clearFields();
  71. LongGeoBox->clearFields();
  72. }
  73. void modCalcGeodCoord::slotComputeGeoCoords (void)
  74. {
  75. if(cartRadio->isChecked()) {
  76. getCartGeoCoords();
  77. showSpheGeoCoords();
  78. } else {
  79. getSphGeoCoords();
  80. showCartGeoCoords();
  81. }
  82. }
  83. void modCalcGeodCoord::showSpheGeoCoords(void)
  84. {
  85. LongGeoBox->show( geoPlace->lng() );
  86. LatGeoBox->show( geoPlace->lat() );
  87. AltGeoBox->setText( QLocale().toString( geoPlace->height(), 3) );
  88. }
  89. void modCalcGeodCoord::showCartGeoCoords(void)
  90. {
  91. XGeoBox->setText( QLocale().toString( geoPlace->xPos()/1000. ,6));
  92. YGeoBox->setText( QLocale().toString( geoPlace->yPos()/1000. ,6));
  93. ZGeoBox->setText( QLocale().toString( geoPlace->zPos()/1000. ,6));
  94. }
  95. void modCalcGeodCoord::geoCheck(void) {
  96. XGeoBoxBatch->setEnabled( false );
  97. XGeoCheckBatch->setChecked( false );
  98. YGeoBoxBatch->setEnabled( false );
  99. YGeoCheckBatch->setChecked( false );
  100. YGeoBoxBatch->setEnabled( false );
  101. YGeoCheckBatch->setChecked( false );
  102. xyzInputCoords = false;
  103. }
  104. void modCalcGeodCoord::xyzCheck(void) {
  105. LongGeoBoxBatch->setEnabled( false );
  106. LongGeoCheckBatch->setChecked( false );
  107. LatGeoBoxBatch->setEnabled( false );
  108. LatGeoCheckBatch->setChecked( false );
  109. AltGeoBoxBatch->setEnabled( false );
  110. AltGeoCheckBatch->setChecked( false );
  111. xyzInputCoords = true;
  112. }
  113. void modCalcGeodCoord::slotLongCheckedBatch(){
  114. if ( LongGeoCheckBatch->isChecked() ) {
  115. LongGeoBoxBatch->setEnabled( false );
  116. geoCheck();
  117. } else {
  118. LongGeoBoxBatch->setEnabled( true );
  119. }
  120. }
  121. void modCalcGeodCoord::slotLatCheckedBatch(){
  122. if ( LatGeoCheckBatch->isChecked() ) {
  123. LatGeoBoxBatch->setEnabled( false );
  124. geoCheck();
  125. } else {
  126. LatGeoBoxBatch->setEnabled( true );
  127. }
  128. }
  129. void modCalcGeodCoord::slotElevCheckedBatch(){
  130. if ( AltGeoCheckBatch->isChecked() ) {
  131. AltGeoBoxBatch->setEnabled( false );
  132. geoCheck();
  133. } else {
  134. AltGeoBoxBatch->setEnabled( true );
  135. }
  136. }
  137. void modCalcGeodCoord::slotXCheckedBatch(){
  138. if ( XGeoCheckBatch->isChecked() ) {
  139. XGeoBoxBatch->setEnabled( false );
  140. xyzCheck();
  141. } else {
  142. XGeoBoxBatch->setEnabled( true );
  143. }
  144. }
  145. void modCalcGeodCoord::slotYCheckedBatch(){
  146. if ( YGeoCheckBatch->isChecked() ) {
  147. YGeoBoxBatch->setEnabled( false );
  148. xyzCheck();
  149. } else {
  150. YGeoBoxBatch->setEnabled( true );
  151. }
  152. }
  153. void modCalcGeodCoord::slotZCheckedBatch(){
  154. if ( ZGeoCheckBatch->isChecked() ) {
  155. ZGeoBoxBatch->setEnabled( false );
  156. xyzCheck();
  157. } else {
  158. ZGeoBoxBatch->setEnabled( true );
  159. }
  160. }
  161. void modCalcGeodCoord::slotInputFile() {
  162. QString inputFileName;
  163. inputFileName = QFileDialog::getOpenFileName(0, QString(), QString());
  164. InputFileBoxBatch->setUrl( inputFileName );
  165. }
  166. void modCalcGeodCoord::slotOutputFile() {
  167. QString outputFileName;
  168. outputFileName = QFileDialog::getSaveFileName();
  169. OutputFileBoxBatch->setUrl( outputFileName );
  170. }
  171. void modCalcGeodCoord::slotRunBatch(void) {
  172. QString inputFileName;
  173. inputFileName = InputFileBoxBatch->url().toLocalFile();
  174. // We open the input file and read its content
  175. if ( QFile::exists(inputFileName) ) {
  176. QFile f( inputFileName );
  177. if ( !f.open( QIODevice::ReadOnly) ) {
  178. QString message = xi18n( "Could not open file %1.", f.fileName() );
  179. KMessageBox::sorry( 0, message, xi18n( "Could Not Open File" ) );
  180. inputFileName.clear();
  181. return;
  182. }
  183. // processLines(&f);
  184. QTextStream istream(&f);
  185. processLines(istream);
  186. // readFile( istream );
  187. f.close();
  188. } else {
  189. QString message = xi18n( "Invalid file: %1", inputFileName );
  190. KMessageBox::sorry( 0, message, xi18n( "Invalid file" ) );
  191. inputFileName.clear();
  192. InputFileBoxBatch->setUrl( inputFileName );
  193. return;
  194. }
  195. }
  196. void modCalcGeodCoord::processLines( QTextStream &istream ) {
  197. // we open the output file
  198. // QTextStream istream(&fIn);
  199. QString outputFileName;
  200. outputFileName = OutputFileBoxBatch->url().toLocalFile();
  201. QFile fOut( outputFileName );
  202. fOut.open(QIODevice::WriteOnly);
  203. QTextStream ostream(&fOut);
  204. QString line;
  205. QChar space = ' ';
  206. int i = 0;
  207. GeoLocation geoPl( dms(0), dms(0) );
  208. geoPl.setEllipsoid(0);
  209. double xB, yB, zB, hB;
  210. dms latB, longB;
  211. while ( ! istream.atEnd() ) {
  212. line = istream.readLine();
  213. line.trimmed();
  214. //Go through the line, looking for parameters
  215. QStringList fields = line.split( ' ' );
  216. i = 0;
  217. // Input coords are XYZ:
  218. if (xyzInputCoords) {
  219. // Read X and write in ostream if corresponds
  220. if(XGeoCheckBatch->isChecked() ) {
  221. xB = fields[i].toDouble();
  222. i++;
  223. } else
  224. xB = XGeoBoxBatch->text().toDouble() ;
  225. if ( AllRadioBatch->isChecked() )
  226. ostream << xB << space;
  227. else
  228. if(XGeoCheckBatch->isChecked() )
  229. ostream << xB << space;
  230. // Read Y and write in ostream if corresponds
  231. if(YGeoCheckBatch->isChecked() ) {
  232. yB = fields[i].toDouble();
  233. i++;
  234. } else
  235. yB = YGeoBoxBatch->text().toDouble() ;
  236. if ( AllRadioBatch->isChecked() )
  237. ostream << yB << space;
  238. else
  239. if(YGeoCheckBatch->isChecked() )
  240. ostream << yB << space;
  241. // Read Z and write in ostream if corresponds
  242. if(ZGeoCheckBatch->isChecked() ) {
  243. zB = fields[i].toDouble();
  244. i++;
  245. } else
  246. zB = ZGeoBoxBatch->text().toDouble();
  247. if ( AllRadioBatch->isChecked() )
  248. ostream << zB << space;
  249. else
  250. if(YGeoCheckBatch->isChecked() )
  251. ostream << zB << space;
  252. geoPl.setXPos( xB*1000.0 );
  253. geoPl.setYPos( yB*1000.0 );
  254. geoPl.setZPos( zB*1000.0 );
  255. ostream << geoPl.lng()->toDMSString() << space <<
  256. geoPl.lat()->toDMSString() << space <<
  257. geoPl.height() << endl;
  258. // Input coords. are Long, Lat and Height
  259. } else {
  260. // Read Longitude and write in ostream if corresponds
  261. if(LongGeoCheckBatch->isChecked() ) {
  262. longB = dms::fromString( fields[i],true);
  263. i++;
  264. } else
  265. longB = LongGeoBoxBatch->createDms(true);
  266. if ( AllRadioBatch->isChecked() )
  267. ostream << longB.toDMSString() << space;
  268. else
  269. if(LongGeoCheckBatch->isChecked() )
  270. ostream << longB.toDMSString() << space;
  271. // Read Latitude and write in ostream if corresponds
  272. if(LatGeoCheckBatch->isChecked() ) {
  273. latB = dms::fromString( fields[i], true);
  274. i++;
  275. } else
  276. latB = LatGeoBoxBatch->createDms(true);
  277. if ( AllRadioBatch->isChecked() )
  278. ostream << latB.toDMSString() << space;
  279. else
  280. if(LatGeoCheckBatch->isChecked() )
  281. ostream << latB.toDMSString() << space;
  282. // Read Height and write in ostream if corresponds
  283. if(AltGeoCheckBatch->isChecked() ) {
  284. hB = fields[i].toDouble();
  285. i++;
  286. } else
  287. hB = AltGeoBoxBatch->text().toDouble() ;
  288. if ( AllRadioBatch->isChecked() )
  289. ostream << hB << space;
  290. else
  291. if(AltGeoCheckBatch->isChecked() )
  292. ostream << hB << space;
  293. geoPl.setLong( longB );
  294. geoPl.setLat( latB );
  295. geoPl.setHeight( hB );
  296. ostream << geoPl.xPos()/1000.0 << space <<
  297. geoPl.yPos()/1000.0 << space <<
  298. geoPl.zPos()/1000.0 << endl;
  299. }
  300. }
  301. fOut.close();
  302. }