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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0004 // SPDX-FileCopyrightText: 2013 Dennis Nienhüser <nienhueser@kde.org>
0005 //
0006 
0007 // A simple tool to read a .kml file and write it back to a .cache file
0008 
0009 #include <ParsingRunnerManager.h>
0010 #include <PluginManager.h>
0011 #include <MarbleClock.h>
0012 #include <GeoDataDocument.h>
0013 #include <GeoDataFolder.h>
0014 #include <GeoDataPlacemark.h>
0015 #include <GeoDataExtendedData.h>
0016 #include <GeoDataData.h>
0017 
0018 #include <QApplication>
0019 #include <QDebug>
0020 #include <QFile>
0021 #include <iostream>
0022 #include <QDataStream>
0023 
0024 using namespace std;
0025 using namespace Marble;
0026 
0027 const quint32 MarbleMagicNumber = 0x31415926;
0028 
0029 void savePlacemarks( QDataStream &out, const GeoDataContainer *container, MarbleClock* clock )
0030 {
0031     qreal lon;
0032     qreal lat;
0033     qreal alt;
0034 
0035     const QVector<GeoDataPlacemark*> placemarks = container->placemarkList();
0036     QVector<GeoDataPlacemark*>::const_iterator it = placemarks.constBegin();
0037     QVector<GeoDataPlacemark*>::const_iterator const end = placemarks.constEnd();
0038     for (; it != end; ++it ) {
0039         out << (*it)->name();
0040         (*it)->coordinate().geoCoordinates( lon, lat, alt );
0041 
0042         // Use double to provide a single cache file format across architectures
0043         out << (double)(lon) << (double)(lat) << (double)(alt);
0044         out << QString( (*it)->role() );
0045         out << QString( (*it)->description() );
0046         out << QString( (*it)->countryCode() );
0047         out << QString( (*it)->state() );
0048         out << (double) (*it)->area();
0049         out << (qint64) (*it)->population();
0050         out << ( qint16 ) ( (*it)->extendedData().value("gmt").value().toInt() );
0051         out << ( qint8 ) ( (*it)->extendedData().value("dst").value().toInt() );
0052     }
0053 
0054     const QVector<GeoDataFolder*> folders = container->folderList();
0055     QVector<GeoDataFolder*>::const_iterator cont = folders.constBegin();
0056     QVector<GeoDataFolder*>::const_iterator endcont = folders.constEnd();
0057     for (; cont != endcont; ++cont ) {
0058             savePlacemarks( out, *cont, clock );
0059     }
0060 }
0061 
0062 void saveFile( const QString& filename, GeoDataDocument* document )
0063 {
0064     QFile file( filename );
0065     if ( !file.open( QIODevice::WriteOnly ) ) {
0066         qDebug() << "Can't open" << filename << "for writing";
0067         return;
0068     }
0069     QDataStream out( &file );
0070 
0071     // Write a header with a "magic number" and a version
0072     // out << (quint32)0xA0B0C0D0;
0073     out << (quint32)MarbleMagicNumber;
0074     out << (qint32)015;
0075 
0076     out.setVersion( QDataStream::Qt_4_2 );
0077 
0078     savePlacemarks( out, document, new MarbleClock );
0079 }
0080 
0081 int main(int argc, char** argv)
0082 {
0083     QApplication app(argc,argv);
0084 
0085     QString inputFilename;
0086     int inputIndex = app.arguments().indexOf( "-i" );
0087     if ( inputIndex > 0 && inputIndex + 1 < argc ) {
0088         inputFilename = app.arguments().at( inputIndex + 1 );
0089     } else {
0090         qDebug( " Syntax: kml2cache -i sourcefile [-o cache-targetfile]" );
0091         return 1;
0092     }
0093 
0094     QString outputFilename = "output.cache";
0095     int outputIndex = app.arguments().indexOf("-o");
0096     if ( outputIndex > 0 && outputIndex + 1 < argc )
0097         outputFilename = app.arguments().at( outputIndex + 1 );
0098 
0099     ParsingRunnerManager* manager = new ParsingRunnerManager( new PluginManager );
0100     GeoDataDocument* document = manager->openFile( inputFilename );
0101     if (!document) {
0102         qDebug() << "Could not parse input file. No error message available unfortunately";
0103         return 2;
0104     }
0105 
0106     saveFile( outputFilename, document );
0107 }