File indexing completed on 2024-05-19 03:53:24

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Mohammed Nafees <nafees.technocool@gmail.com>
0004 //
0005 
0006 
0007 #include <QFile>
0008 #include <QDebug>
0009 #include <QCoreApplication>
0010 #include <QStringList>
0011 
0012 #include <cmath>
0013 #include <iostream>
0014 
0015 int main(int argc, char *argv[])
0016 {
0017     QCoreApplication app(argc, argv);
0018 
0019     QFile dsoData( "dso.dat" );
0020     if ( !dsoData.open( QFile::ReadOnly ) ) {
0021         std::cout << "File dso.dat not found in current path. Exiting." << std::endl;
0022         return 1;
0023     }
0024 
0025     QFile file( "dso.kml" );
0026     file.open( QIODevice::WriteOnly );
0027     QTextStream out( &file );
0028 
0029     out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
0030         << "<kml xmlns=\"http://www.opengis.net/kml/2.2\" hint=\"target=sky\"> \n"
0031         << "<Document> \n"
0032         << "    <Style id=\"iconStyle\"> \n"
0033         << "        <IconStyle> \n"
0034         << "            <Icon> \n"
0035         << "                <href>deepsky.png</href> \n"
0036         << "            </Icon> \n"
0037         << "        </IconStyle> \n"
0038         << "    </Style> \n";
0039 
0040     QTextStream in( &dsoData );
0041 
0042     QString line;
0043     qreal longitude;
0044     qreal latitude;
0045 
0046     while ( !in.atEnd() ) {
0047         line = in.readLine();
0048 
0049         // Check for null line at end of file
0050         if ( line.isNull() ) {
0051             continue;
0052         }
0053 
0054         // Ignore Comment lines in header and
0055         // between dso entries
0056         if (line.startsWith(QLatin1Char('#'))) {
0057             continue;
0058         }
0059 
0060         QStringList entries = line.split( QLatin1Char( ',' ) );
0061 
0062         QString id = entries.at( 0 );
0063         QString longName = entries.at(7);
0064 
0065         double raH = entries.at( 1 ).toDouble();
0066         double raM = entries.at( 2 ).toDouble();
0067         double raS = entries.at( 3 ).toDouble();
0068         double decH = entries.at( 4 ).toDouble();
0069         double decM = entries.at( 5 ).toDouble();
0070         double decS = entries.at( 6 ).toDouble();
0071 
0072         double ra = ( raH + raM / 60.0 + raS / 3600.0 ) * 15.0;
0073         double dec;
0074 
0075         if ( decH >= 0.0 ) {
0076             dec = decH + decM /60.0 + decS / 3600.0;
0077         }
0078         else {
0079             dec = decH - decM / 60.0 - decS / 3600.0;
0080         }
0081 
0082         longitude = ra - 180.0;
0083         latitude = dec;
0084 
0085         out << "    <Placemark> \n"
0086             << "        <name>" << id << "</name> \n"
0087             << "        <description>" << longName << "</description> \n"
0088             << "        <styleUrl>#iconStyle</styleUrl> \n"
0089             << "        <Point> \n"
0090             << "            <coordinates>" << longitude << "," << latitude << "</coordinates> \n"
0091             << "        </Point> \n"
0092             << "    </Placemark> \n";
0093     }
0094 
0095     out << "</Document> \n"
0096         << "</kml> \n";
0097 
0098     dsoData.close();
0099     file.close();
0100 
0101     app.exit();
0102 }
0103 
0104