File indexing completed on 2024-11-10 12:16:43
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 // SPDX-FileCopyrightText: 2010 Harshit Jain <hjain.itbhu@gmail.com> 0006 // 0007 0008 0009 #include <QCoreApplication> 0010 #include <QDebug> 0011 #include <QFile> 0012 #include <QStringList> 0013 0014 0015 QString escapeXml( const QString &str ) 0016 { 0017 QString xml = str; 0018 xml.replace(QLatin1Char('&'), QStringLiteral("&")); 0019 xml.replace(QLatin1Char('<'), QStringLiteral("<")); 0020 xml.replace(QLatin1Char('>'), QStringLiteral(">")); 0021 xml.replace(QLatin1Char('\''), QStringLiteral("'")); 0022 xml.replace(QLatin1Char('"'), QStringLiteral(""")); 0023 0024 return xml; 0025 } 0026 0027 0028 int main(int argc, char *argv[]) 0029 { 0030 QCoreApplication app( argc, argv ); 0031 0032 for ( int i = 1; i < argc; ++i ) { 0033 if ( strcmp( argv[ i ], "-o" ) != 0 ) 0034 continue; 0035 0036 const QString targetfilename = QString( argv[i+1] ); 0037 const QString sourcefilename = QString( argv[i+2] ); 0038 const QString supportfilename = QString( argv[i+3] ); 0039 const QString timezonefilename = QString( argv[i+4] ); 0040 0041 qDebug() << "Source: " << sourcefilename; 0042 qDebug() << "Support: " << supportfilename; 0043 qDebug() << "Target: " << targetfilename; 0044 qDebug() << "Timezone: " << timezonefilename; 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::WriteOnly ); 0055 0056 QTextStream targetstream( &targetfile ); 0057 targetstream.setCodec("UTF-8"); 0058 0059 QFile supportfile( supportfilename ); 0060 supportfile.open( QIODevice::ReadOnly ); 0061 0062 QTextStream supportstream( &supportfile ); 0063 supportstream.setCodec("UTF-8"); 0064 0065 QFile timezonefile( timezonefilename ); 0066 timezonefile.open( QIODevice::ReadOnly ); 0067 0068 QTextStream timezonestream( &timezonefile ); 0069 timezonestream.setCodec("UTF-8"); 0070 0071 // gzFile gzDoc = gzopen( targetfilename.toLatin1(), "w"); 0072 // QTextStream targetstream( new QString() ); 0073 0074 targetstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" 0075 << "<kml xmlns=\"http://www.opengis.net/kml/2.2\"> \n" 0076 << "<Document> \n"; 0077 QString state; 0078 QString gmt; 0079 QString dst; 0080 0081 while ( !sourcestream.atEnd() ) { 0082 0083 const QString rawline = sourcestream.readLine(); 0084 const QStringList splitline = rawline.split(QLatin1Char('\t')); 0085 0086 const QString name = splitline[1]; 0087 const QString latstring = splitline[4]; 0088 const QString lngstring = splitline[5]; 0089 const QString role = splitline[7]; 0090 const QString country = splitline[8]; 0091 const QString statecode = splitline[10]; 0092 const QString popstring = splitline[14]; 0093 const QString elestring = splitline[16]; 0094 const QString timezone = splitline[17]; 0095 0096 supportstream.seek(0); 0097 while ( !supportstream.atEnd() ) { 0098 const QString supportrawline = supportstream.readLine(); 0099 const QStringList supportsplitline = supportrawline.split(QLatin1Char('\t')); 0100 if (supportsplitline[0] == (country + QLatin1Char('.') +statecode)) { 0101 state = supportsplitline[1]; 0102 break; 0103 } 0104 } 0105 0106 timezonestream.seek(0); 0107 timezonestream.readLine(); 0108 while ( !timezonestream.atEnd() ) { 0109 const QString timezonerawline = timezonestream.readLine(); 0110 const QStringList timezonesplitline = timezonerawline.split(QLatin1Char('\t')); 0111 0112 if( timezonesplitline[1] == timezone ) 0113 { 0114 gmt = timezonesplitline[2]; 0115 dst = timezonesplitline[3]; 0116 break; 0117 } 0118 } 0119 0120 const int gmtoffset = ( int ) ( gmt.toFloat() * 100 ); 0121 const int dstoffset = ( int ) ( dst.toFloat() * 100 ) - gmtoffset; 0122 0123 if (role != QLatin1String("PPLX")) { 0124 targetstream << " <Placemark> \n"; 0125 targetstream << " <name>" << escapeXml( name ) << "</name> \n"; 0126 targetstream << " <state>" << escapeXml( state ) << "</state> \n"; 0127 targetstream << " <CountryNameCode>" << escapeXml( country.toUpper() ) << "</CountryNameCode>\n"; 0128 targetstream << " <role>" << escapeXml( role ) << "</role> \n"; 0129 targetstream << " <pop>" 0130 << escapeXml( popstring ) << "</pop> \n"; 0131 targetstream << " <Point>\n" 0132 << " <coordinates>" 0133 << escapeXml( lngstring ) 0134 << "," 0135 << escapeXml( latstring ) 0136 << "," 0137 << escapeXml( elestring ) 0138 << "</coordinates> \n" 0139 << " </Point> \n"; 0140 targetstream << " <ExtendedData>\n" 0141 << " <Data name=\"gmt\">\n" 0142 << " <value>" << escapeXml( QString::number( gmtoffset ) ) << "</value>\n" 0143 << " </Data>\n"; 0144 if( dstoffset ) 0145 { 0146 targetstream << " <Data name=\"dst\">\n" 0147 << " <value>" << escapeXml( QString::number( dstoffset) ) << "</value>\n" 0148 << " </Data>\n"; 0149 } 0150 targetstream << " </ExtendedData>\n"; 0151 targetstream << " </Placemark> \n"; 0152 } 0153 } 0154 0155 targetstream << "</Document> \n" 0156 << "</kml> \n"; 0157 qDebug("Putting"); 0158 0159 // gzputs( gzDoc, targetstream.readAll().toUtf8() ); 0160 // gzclose( gzDoc ); 0161 0162 sourcefile.close(); 0163 targetfile.close(); 0164 supportfile.close(); 0165 timezonefile.close(); 0166 qDebug("Finished!"); 0167 return 0; 0168 } 0169 0170 qDebug(" asc2kml -o targetfile sourcefile supporfile timezonefile"); 0171 app.exit(); 0172 }