File indexing completed on 2024-07-21 12:04:49

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
0005 //
0006 
0007 
0008 // #include <zlib.h>
0009 
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 #include <QFile>
0013 #include <QStringList>
0014 
0015 
0016 QString escapeXml( const QString &str )
0017 {
0018     QString xml = str;
0019     xml.replace(QLatin1Char('&'), QStringLiteral("&amp;"));
0020     xml.replace(QLatin1Char('<'), QStringLiteral("&lt;"));
0021     xml.replace(QLatin1Char('>'), QStringLiteral("&gt;"));
0022     xml.replace(QLatin1Char('\''), QStringLiteral("&apos;"));
0023     xml.replace(QLatin1Char('"'), QStringLiteral("&quot;"));
0024 
0025     return xml;
0026 }
0027 
0028 
0029 int main(int argc, char *argv[])
0030 {
0031     QString  sourcefilename;
0032     QString  targetfilename;
0033 
0034     QCoreApplication  app( argc, argv );
0035 
0036     for ( int i = 1; i < argc; ++i ) {
0037         if ( strcmp( argv[ i ], "-o" ) != 0 )
0038             continue;
0039 
0040         targetfilename = QString(argv[i+1]);
0041         sourcefilename = QString(argv[i+2]);
0042 
0043         qDebug() << "Source: " << sourcefilename;
0044         qDebug() << "Target: " << targetfilename;
0045 
0046         QFile  sourcefile( sourcefilename );
0047         sourcefile.open( QIODevice::ReadOnly );
0048 
0049         // Read the data serialized from the file.
0050         QTextStream  sourcestream( &sourcefile );
0051         sourcestream.setCodec("UTF-8");
0052 
0053         QFile  targetfile( targetfilename );
0054         targetfile.open( QIODevice::ReadWrite );
0055 
0056         QTextStream  targetstream( &targetfile );
0057         targetstream.setCodec("UTF-8");
0058 
0059         // gzFile gzDoc = gzopen( targetfilename.toLatin1(), "w");
0060         // QTextStream targetstream( new QString() );
0061 
0062         targetstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
0063                      << "<kml xmlns=\"http://earth.google.com/kml/2.1\"> \n"
0064                      << "<Document> \n";
0065 
0066         targetstream << "    <Schema name=\"MarblePlacemark\" parent=\"Placemark\"> \n";
0067         targetstream << "       <SimpleField name=\"pop\" type=\"int\"></SimpleField> \n";
0068         targetstream << "       <SimpleField name=\"role\" type=\"string\"></SimpleField> \n";
0069         targetstream << "    </Schema> \n";
0070 
0071         targetstream << "\n";
0072 
0073         QString  rawline;
0074         QString  nameString;
0075         QString  latString;
0076         QString  lonString;
0077         QString  popString;
0078         QString  roleString;
0079         QString  description;
0080         float    lat;
0081         float    lon;
0082         int          population;
0083         QStringList  splitline;
0084 
0085         while ( !sourcestream.atEnd() ) {
0086 
0087             rawline = sourcestream.readLine();
0088 //            if ( !rawline.startsWith("\"E\"|\"m\"|\"" ) ) {
0089             if ( !rawline.startsWith(QLatin1String( "\"V\"|\"V\"|\"" ) ) ) {
0090                 continue;
0091             }
0092             rawline.replace(QStringLiteral("\"|"), QStringLiteral("|"));
0093             rawline.replace(QStringLiteral("|\""), QStringLiteral("|"));
0094             if (rawline.startsWith(QLatin1Char('\"')) && rawline.endsWith(QLatin1Char('\"'))) {
0095                 rawline = rawline.left( rawline.length() - 1 );
0096                 rawline = rawline.right( rawline.length() - 2 );
0097             }
0098 
0099             splitline = rawline.split(QLatin1Char('|'));
0100 
0101             nameString  = splitline[2];
0102             latString   = splitline[3];
0103             lonString   = splitline[4];
0104             popString   = splitline[10];
0105             roleString  = splitline[18];
0106             description = splitline[19];
0107 
0108             // if (roleString == QLatin1String("SF")) continue;
0109 
0110             QString marbleRoleString = QLatin1String( "o" );
0111 
0112             if (roleString == QLatin1String("AA") || roleString == QLatin1String("SF")) marbleRoleString = "c";
0113             if (   roleString == QLatin1String("ME") || roleString == QLatin1String("OC")
0114                 || roleString == QLatin1String("LC") || roleString == QLatin1String("SI")) marbleRoleString = "a";
0115             if (roleString == QLatin1String("MO")) marbleRoleString = "m";
0116             if (roleString == QLatin1String("VA")) marbleRoleString = "v";
0117 
0118             population = (int) ( 1000.0 * popString.toFloat() );
0119 
0120             lon = lonString.toFloat();
0121 
0122             if ( lon > 180.0 ) lon = lon - 360.0;
0123 
0124             if ( rawline.startsWith(QLatin1String( "\"M\"|\"M\"|\"" ) ) || rawline.startsWith("\"V\"|\"V\"|\"" ) ) {
0125                 lon = -lon;
0126             }
0127 
0128             lat = latString.toFloat();
0129 
0130             description.remove(QLatin1Char('"'));
0131 
0132             targetstream << "    <MarblePlacemark> \n";
0133             targetstream << "        <name>" << escapeXml( nameString ) << "</name> \n";
0134             targetstream << "        <role>" << escapeXml( marbleRoleString ) << "</role> \n";
0135             targetstream << "        <pop>"
0136                          << escapeXml( QString::number( population ) ) << "</pop> \n";
0137             targetstream << "        <description>" << escapeXml( description ) << "</description> \n";
0138             targetstream << "        <Point>\n"
0139                          << "            <coordinates>"
0140                          << escapeXml( QString::number( lon ) )
0141                          << ","
0142                          << escapeXml( QString::number( lat ) )
0143                          << "</coordinates> \n"
0144                          << "        </Point> \n";
0145             targetstream << "    </MarblePlacemark> \n";
0146         }
0147 
0148         targetstream << "</Document> \n"
0149                      << "</kml> \n";
0150         qDebug("...");
0151 
0152         sourcefile.close();
0153         targetfile.close();
0154 
0155         qDebug("Finished!");
0156         return 0;
0157     }
0158 
0159     qDebug(" iau2kml -o targetfile sourcefile");
0160     app.exit();
0161 }