File indexing completed on 2024-04-21 03:50:43

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Mohammed Nafees <nafees.technocool@gmail.com>
0004 //
0005 
0006 #include "MarbleGlobal.h"
0007 
0008 #include <QFile>
0009 #include <QDebug>
0010 #include <QCoreApplication>
0011 #include <QStringList>
0012 #include <QHash>
0013 #include <QPair>
0014 
0015 #include <cmath>
0016 
0017 using namespace Marble;
0018 
0019 int main(int argc, char *argv[])
0020 {
0021     QCoreApplication app(argc, argv);
0022 
0023     QFile file( "constellations.kml" );
0024     file.open( QIODevice::WriteOnly );
0025     QTextStream out( &file );
0026 
0027     out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
0028         << "<kml xmlns=\"http://www.opengis.net/kml/2.2\" hint=\"target=sky\"> \n"
0029         << "<Document> \n"
0030         << "    <Style id=\"lineStyle1\"> \n"
0031         << "        <LineStyle> \n"
0032         << "            <color>ffffffff</color> \n"
0033         << "        </LineStyle> \n"
0034         << "    </Style> \n"
0035         << "    <Style id=\"lineStyle2\"> \n"
0036         << "        <LineStyle> \n"
0037         << "            <color>ffff0000</color> \n"
0038         << "        </LineStyle> \n"
0039         << "    </Style> \n"
0040         << "    <Style id=\"iconStyle\"> \n"
0041         << "        <IconStyle> \n"
0042         << "            <Icon> \n"
0043         << "                <href></href> \n"
0044         << "            </Icon> \n"
0045         << "        </IconStyle> \n"
0046         << "    </Style> \n";
0047 
0048     QFile starsData( "catalog.dat" );
0049     QFile constellationsData( "constellations.dat" );
0050 
0051     if ( starsData.open( QFile::ReadOnly ) && constellationsData.open( QFile::ReadOnly ) ) {
0052 
0053         QTextStream streamConstellations( &constellationsData );
0054         QTextStream streamStars( &starsData );
0055 
0056         QHash<int, QPair<qreal, qreal> > hash;
0057 
0058         QString starsLine;
0059         int index;
0060         qreal longitude;
0061         qreal latitude;
0062         QPair<qreal, qreal> pair;
0063         do {
0064             starsLine = streamStars.readLine();
0065             index = starsLine.mid( 0, 4 ).toInt();
0066 
0067             QString recString = starsLine.mid( 75, 6 );
0068             double raHH = recString.mid( 0, 2 ).toDouble();
0069             double raMM = recString.mid( 2, 2 ).toDouble();
0070             double raSS = recString.mid( 4, 2 ).toDouble();
0071 
0072             longitude = ( raHH + raMM / 60.0 + raSS / 3600.0 ) * 15.0 - 180.0;
0073 
0074             QString decString = starsLine.mid( 83, 7 );
0075             double deSign = decString.startsWith(QLatin1Char('-')) ? -1.0 : 1.0;
0076             double deHH = decString.mid( 1, 2 ).toDouble();
0077             double deMM = decString.mid( 3, 2 ).toDouble();
0078             double deSS = decString.mid( 5, 2 ).toDouble();
0079 
0080             double deValue = deSign * ( deHH + deMM / 60.0 + deSS / 3600.0 );
0081 
0082             latitude = deValue;
0083 
0084             pair.first = longitude;
0085             pair.second = latitude;
0086 
0087             hash.insert( index, pair );
0088         } while ( !starsLine.isNull() );
0089 
0090         QString name;
0091         QString indexList;
0092         QStringList starIndexes;
0093         while ( !streamConstellations.atEnd() ) {
0094             name = streamConstellations.readLine();
0095 
0096             // Check for null line at end of file
0097             if ( name.isNull() ) {
0098                 continue;
0099             }
0100 
0101             // Ignore Comment lines in header and
0102             // between constellation entries
0103             if (name.startsWith(QLatin1Char('#'))) {
0104                 continue;
0105             }
0106 
0107             indexList = streamConstellations.readLine();
0108 
0109             // Make sure we have a valid name and indexList
0110             if ( indexList.isNull() ) {
0111                 break;
0112             }
0113 
0114             out << "    <Placemark> \n"
0115                 << "        <styleUrl>#lineStyle1</styleUrl> \n"
0116                 << "        <MultiGeometry> \n"
0117                 << "            <LineString> \n"
0118                 << "                <coordinates> \n";
0119 
0120             starIndexes = indexList.split(QLatin1Char(' '));
0121             QList<qreal> x;
0122             QList<qreal> y;
0123             QList<qreal> z;
0124             int numberOfStars = 0;
0125             for ( int i = 0; i < starIndexes.size(); ++i ) {
0126                 if (starIndexes.at(i) == QLatin1String("-1")) {
0127                     out << "                </coordinates> \n"
0128                         << "            </LineString> \n"
0129                         << "            <LineString> \n"
0130                         << "                <coordinates> \n";
0131                 } else if (starIndexes.at(i) == QLatin1String("-2")) {
0132                     out << "                </coordinates> \n"
0133                         << "            </LineString> \n"
0134                         << "        </MultiGeometry> \n"
0135                         << "    </Placemark> \n"
0136                         << "    <Placemark> \n"
0137                         << "        <styleUrl>#lineStyle2</styleUrl> \n"
0138                         << "        <MultiGeometry> \n"
0139                         << "            <LineString> \n"
0140                         << "                <coordinates> \n";
0141                 } else if (starIndexes.at(i) == QLatin1String("-3")) {
0142                     out << "                </coordinates> \n"
0143                         << "            </LineString> \n"
0144                         << "        </MultiGeometry> \n"
0145                         << "    </Placemark> \n"
0146                         << "    <Placemark> \n"
0147                         << "        <styleUrl>#lineStyle1</styleUrl> \n"
0148                         << "        <MultiGeometry> \n"
0149                         << "            <LineString> \n"
0150                         << "                <coordinates> \n";
0151                 }
0152                 QHash<int, QPair<qreal, qreal> >::const_iterator j = hash.constFind( starIndexes.at(i).toInt() );
0153                 while( j != hash.constEnd() && j.key() == starIndexes.at(i).toInt() ) {
0154                     out << "                " << j.value().first << "," << j.value().second << " \n";
0155                     x.append( cos( j.value().first * DEG2RAD ) * cos( j.value().second * DEG2RAD ) );
0156                     y.append( sin( j.value().first * DEG2RAD ) * cos( j.value().second * DEG2RAD ) );
0157                     z.append( sin( j.value().second * DEG2RAD ) );
0158                     ++numberOfStars;
0159                     ++j;
0160                 }
0161             }
0162 
0163             out << "                </coordinates> \n"
0164                 << "            </LineString> \n"
0165                 << "        </MultiGeometry> \n"
0166                 << "    </Placemark> \n";
0167 
0168             qreal xMean = 0.0;
0169             qreal yMean = 0.0;
0170             qreal zMean = 0.0;
0171             for ( int s = 0; s < numberOfStars; ++s ) {
0172                 xMean += x.at(s);
0173                 yMean += y.at(s);
0174                 zMean += z.at(s);
0175             }
0176 
0177             xMean = xMean / numberOfStars;
0178             yMean = yMean / numberOfStars;
0179             zMean = zMean / numberOfStars;
0180 
0181             qreal labelLongitude = RAD2DEG * atan2( yMean, xMean );
0182             qreal labelLatitude = RAD2DEG * atan2( zMean, sqrt( xMean * xMean + yMean * yMean ) );
0183 
0184             out << "    <Placemark> \n"
0185                 << "        <styleUrl>#iconStyle</styleUrl> \n"
0186                 << "        <name>" << name << "</name> \n"
0187                 << "        <Point> \n"
0188                 << "            <coordinates>" << labelLongitude << "," << labelLatitude << "</coordinates> \n"
0189                 << "        </Point> \n"
0190                 << "    </Placemark> \n";
0191         }
0192     }
0193 
0194     out << "</Document> \n"
0195         << "</kml> \n";
0196 
0197     constellationsData.close();
0198     starsData.close();
0199     file.close();
0200 
0201     app.exit();
0202 }
0203