File indexing completed on 2024-03-24 15:18:06

0001 /*
0002     SPDX-FileCopyrightText: 2002 Pablo de Vicente <vicente@oan.es>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "modcalcgeodcoord.h"
0008 
0009 #include "dms.h"
0010 #include "geolocation.h"
0011 #include "kstars.h"
0012 #include "ksnotification.h"
0013 #include "kstarsdata.h"
0014 
0015 #include <QTextStream>
0016 #include <QFileDialog>
0017 
0018 modCalcGeodCoord::modCalcGeodCoord(QWidget *parentSplit) : QFrame(parentSplit)
0019 {
0020     QStringList ellipsoidList;
0021 
0022     ellipsoidList << "IAU76"
0023                   << "GRS80"
0024                   << "MERIT83"
0025                   << "WGS84"
0026                   << "IERS89";
0027 
0028     setupUi(this);
0029 
0030     spheRadio->setChecked(true);
0031     ellipsoidBox->insertItems(5, ellipsoidList);
0032     geoPlace.reset(new GeoLocation(dms(0), dms(0)));
0033     showLongLat();
0034     setEllipsoid(0);
0035     show();
0036 
0037     connect(Clear, SIGNAL(clicked()), this, SLOT(slotClearGeoCoords()));
0038     connect(Compute, SIGNAL(clicked()), this, SLOT(slotComputeGeoCoords()));
0039 }
0040 
0041 void modCalcGeodCoord::showLongLat(void)
0042 {
0043     KStarsData *data = KStarsData::Instance();
0044 
0045     LongGeoBox->show(data->geo()->lng());
0046     LatGeoBox->show(data->geo()->lat());
0047     AltGeoBox->setText(QString("0.0"));
0048 }
0049 
0050 void modCalcGeodCoord::setEllipsoid(int index)
0051 {
0052     geoPlace->changeEllipsoid(index);
0053 }
0054 
0055 void modCalcGeodCoord::getCartGeoCoords(void)
0056 {
0057     geoPlace->setXPos(XGeoBox->text().toDouble() * 1000.);
0058     geoPlace->setYPos(YGeoBox->text().toDouble() * 1000.);
0059     geoPlace->setZPos(ZGeoBox->text().toDouble() * 1000.);
0060 }
0061 
0062 void modCalcGeodCoord::getSphGeoCoords(void)
0063 {
0064     geoPlace->setLong(LongGeoBox->createDms());
0065     geoPlace->setLat(LatGeoBox->createDms());
0066     geoPlace->setElevation(AltGeoBox->text().toDouble());
0067 }
0068 
0069 void modCalcGeodCoord::slotClearGeoCoords(void)
0070 {
0071     geoPlace->setLong(dms(0.0));
0072     geoPlace->setLat(dms(0.0));
0073     geoPlace->setElevation(0.0);
0074     LatGeoBox->clearFields();
0075     LongGeoBox->clearFields();
0076 }
0077 
0078 void modCalcGeodCoord::slotComputeGeoCoords(void)
0079 {
0080     if (cartRadio->isChecked())
0081     {
0082         getCartGeoCoords();
0083         showSpheGeoCoords();
0084     }
0085     else
0086     {
0087         getSphGeoCoords();
0088         showCartGeoCoords();
0089     }
0090 }
0091 
0092 void modCalcGeodCoord::showSpheGeoCoords(void)
0093 {
0094     LongGeoBox->show(geoPlace->lng());
0095     LatGeoBox->show(geoPlace->lat());
0096     AltGeoBox->setText(QLocale().toString(geoPlace->elevation(), 3));
0097 }
0098 
0099 void modCalcGeodCoord::showCartGeoCoords(void)
0100 {
0101     XGeoBox->setText(QLocale().toString(geoPlace->xPos() / 1000., 6));
0102     YGeoBox->setText(QLocale().toString(geoPlace->yPos() / 1000., 6));
0103     ZGeoBox->setText(QLocale().toString(geoPlace->zPos() / 1000., 6));
0104 }
0105 
0106 void modCalcGeodCoord::geoCheck(void)
0107 {
0108     XGeoBoxBatch->setEnabled(false);
0109     XGeoCheckBatch->setChecked(false);
0110     YGeoBoxBatch->setEnabled(false);
0111     YGeoCheckBatch->setChecked(false);
0112     YGeoBoxBatch->setEnabled(false);
0113     YGeoCheckBatch->setChecked(false);
0114     xyzInputCoords = false;
0115 }
0116 
0117 void modCalcGeodCoord::xyzCheck(void)
0118 {
0119     LongGeoBoxBatch->setEnabled(false);
0120     LongGeoCheckBatch->setChecked(false);
0121     LatGeoBoxBatch->setEnabled(false);
0122     LatGeoCheckBatch->setChecked(false);
0123     AltGeoBoxBatch->setEnabled(false);
0124     AltGeoCheckBatch->setChecked(false);
0125     xyzInputCoords = true;
0126 }
0127 
0128 void modCalcGeodCoord::slotLongCheckedBatch()
0129 {
0130     if (LongGeoCheckBatch->isChecked())
0131     {
0132         LongGeoBoxBatch->setEnabled(false);
0133         geoCheck();
0134     }
0135     else
0136     {
0137         LongGeoBoxBatch->setEnabled(true);
0138     }
0139 }
0140 
0141 void modCalcGeodCoord::slotLatCheckedBatch()
0142 {
0143     if (LatGeoCheckBatch->isChecked())
0144     {
0145         LatGeoBoxBatch->setEnabled(false);
0146         geoCheck();
0147     }
0148     else
0149     {
0150         LatGeoBoxBatch->setEnabled(true);
0151     }
0152 }
0153 
0154 void modCalcGeodCoord::slotElevCheckedBatch()
0155 {
0156     if (AltGeoCheckBatch->isChecked())
0157     {
0158         AltGeoBoxBatch->setEnabled(false);
0159         geoCheck();
0160     }
0161     else
0162     {
0163         AltGeoBoxBatch->setEnabled(true);
0164     }
0165 }
0166 
0167 void modCalcGeodCoord::slotXCheckedBatch()
0168 {
0169     if (XGeoCheckBatch->isChecked())
0170     {
0171         XGeoBoxBatch->setEnabled(false);
0172         xyzCheck();
0173     }
0174     else
0175     {
0176         XGeoBoxBatch->setEnabled(true);
0177     }
0178 }
0179 
0180 void modCalcGeodCoord::slotYCheckedBatch()
0181 {
0182     if (YGeoCheckBatch->isChecked())
0183     {
0184         YGeoBoxBatch->setEnabled(false);
0185         xyzCheck();
0186     }
0187     else
0188     {
0189         YGeoBoxBatch->setEnabled(true);
0190     }
0191 }
0192 
0193 void modCalcGeodCoord::slotZCheckedBatch()
0194 {
0195     if (ZGeoCheckBatch->isChecked())
0196     {
0197         ZGeoBoxBatch->setEnabled(false);
0198         xyzCheck();
0199     }
0200     else
0201     {
0202         ZGeoBoxBatch->setEnabled(true);
0203     }
0204 }
0205 void modCalcGeodCoord::slotInputFile()
0206 {
0207     const QString inputFileName = QFileDialog::getOpenFileName(KStars::Instance(), QString(), QString());
0208     if (!inputFileName.isEmpty())
0209         InputFileBoxBatch->setUrl(QUrl::fromLocalFile(inputFileName));
0210 }
0211 
0212 void modCalcGeodCoord::slotOutputFile()
0213 {
0214     const QString outputFileName = QFileDialog::getSaveFileName();
0215     if (!outputFileName.isEmpty())
0216         OutputFileBoxBatch->setUrl(QUrl::fromLocalFile(outputFileName));
0217 }
0218 
0219 void modCalcGeodCoord::slotRunBatch(void)
0220 {
0221     const QString inputFileName = InputFileBoxBatch->url().toLocalFile();
0222 
0223     // We open the input file and read its content
0224 
0225     if (QFile::exists(inputFileName))
0226     {
0227         QFile f(inputFileName);
0228         if (!f.open(QIODevice::ReadOnly))
0229         {
0230             QString message = i18n("Could not open file %1.", f.fileName());
0231             KSNotification::sorry(message, i18n("Could Not Open File"));
0232             return;
0233         }
0234 
0235         //      processLines(&f);
0236         QTextStream istream(&f);
0237         processLines(istream);
0238         //      readFile( istream );
0239         f.close();
0240     }
0241     else
0242     {
0243         QString message = i18n("Invalid file: %1", inputFileName);
0244         KSNotification::sorry(message, i18n("Invalid file"));
0245         InputFileBoxBatch->setUrl(QUrl());
0246     }
0247 }
0248 
0249 void modCalcGeodCoord::processLines(QTextStream &istream)
0250 {
0251     // we open the output file
0252 
0253     //  QTextStream istream(&fIn);
0254     const QString outputFileName = OutputFileBoxBatch->url().toLocalFile();
0255     QFile fOut(outputFileName);
0256     fOut.open(QIODevice::WriteOnly);
0257     QTextStream ostream(&fOut);
0258 
0259     QString line;
0260     QChar space = ' ';
0261     int i       = 0;
0262     GeoLocation geoPl(dms(0), dms(0));
0263     geoPl.setEllipsoid(0);
0264 
0265     double xB, yB, zB, hB;
0266     dms latB, longB;
0267 
0268     while (!istream.atEnd())
0269     {
0270         line = istream.readLine();
0271         line = line.trimmed();
0272 
0273         //Go through the line, looking for parameters
0274 
0275         QStringList fields = line.split(' ');
0276 
0277         i = 0;
0278 
0279         // Input coords are XYZ:
0280 
0281         if (xyzInputCoords)
0282         {
0283             // Read X and write in ostream if corresponds
0284 
0285             if (XGeoCheckBatch->isChecked())
0286             {
0287                 xB = fields[i].toDouble();
0288                 i++;
0289             }
0290             else
0291                 xB = XGeoBoxBatch->text().toDouble();
0292 
0293             if (AllRadioBatch->isChecked())
0294                 ostream << xB << space;
0295             else if (XGeoCheckBatch->isChecked())
0296                 ostream << xB << space;
0297 
0298             // Read Y and write in ostream if corresponds
0299 
0300             if (YGeoCheckBatch->isChecked())
0301             {
0302                 yB = fields[i].toDouble();
0303                 i++;
0304             }
0305             else
0306                 yB = YGeoBoxBatch->text().toDouble();
0307 
0308             if (AllRadioBatch->isChecked())
0309                 ostream << yB << space;
0310             else if (YGeoCheckBatch->isChecked())
0311                 ostream << yB << space;
0312             // Read Z and write in ostream if corresponds
0313 
0314             if (ZGeoCheckBatch->isChecked())
0315             {
0316                 zB = fields[i].toDouble();
0317                 i++;
0318             }
0319             else
0320                 zB = ZGeoBoxBatch->text().toDouble();
0321 
0322             if (AllRadioBatch->isChecked())
0323                 ostream << zB << space;
0324             else if (YGeoCheckBatch->isChecked())
0325                 ostream << zB << space;
0326 
0327             geoPl.setXPos(xB * 1000.0);
0328             geoPl.setYPos(yB * 1000.0);
0329             geoPl.setZPos(zB * 1000.0);
0330             ostream << geoPl.lng()->toDMSString() << space << geoPl.lat()->toDMSString() << space << geoPl.elevation()
0331                     << '\n';
0332 
0333             // Input coords. are Long, Lat and Height
0334         }
0335         else
0336         {
0337             // Read Longitude and write in ostream if corresponds
0338 
0339             if (LongGeoCheckBatch->isChecked())
0340             {
0341                 longB = dms::fromString(fields[i], true);
0342                 i++;
0343             }
0344             else
0345                 longB = LongGeoBoxBatch->createDms();
0346 
0347             if (AllRadioBatch->isChecked())
0348                 ostream << longB.toDMSString() << space;
0349             else if (LongGeoCheckBatch->isChecked())
0350                 ostream << longB.toDMSString() << space;
0351 
0352             // Read Latitude and write in ostream if corresponds
0353 
0354             if (LatGeoCheckBatch->isChecked())
0355             {
0356                 latB = dms::fromString(fields[i], true);
0357                 i++;
0358             }
0359             else
0360                 latB = LatGeoBoxBatch->createDms();
0361 
0362             if (AllRadioBatch->isChecked())
0363                 ostream << latB.toDMSString() << space;
0364             else if (LatGeoCheckBatch->isChecked())
0365                 ostream << latB.toDMSString() << space;
0366 
0367             // Read Height and write in ostream if corresponds
0368 
0369             if (AltGeoCheckBatch->isChecked())
0370             {
0371                 hB = fields[i].toDouble();
0372                 i++;
0373             }
0374             else
0375                 hB = AltGeoBoxBatch->text().toDouble();
0376 
0377             if (AllRadioBatch->isChecked())
0378                 ostream << hB << space;
0379             else if (AltGeoCheckBatch->isChecked())
0380                 ostream << hB << space;
0381 
0382             geoPl.setLong(longB);
0383             geoPl.setLat(latB);
0384             geoPl.setElevation(hB);
0385 
0386             ostream << geoPl.xPos() / 1000.0 << space << geoPl.yPos() / 1000.0 << space << geoPl.zPos() / 1000.0
0387                     << '\n';
0388         }
0389     }
0390 
0391     fOut.close();
0392 }