File indexing completed on 2024-07-07 14:12:15

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "SqlWriter.h"
0007 #include "pbf/PbfParser.h"
0008 #include "xml/XmlParser.h"
0009 
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 #include <QFileInfo>
0013 
0014 #include <QMessageLogContext>
0015 
0016 using namespace Marble;
0017 
0018 enum DebugLevel {
0019     Debug,
0020     Info,
0021     Mute
0022 };
0023 
0024 DebugLevel debugLevel = Info;
0025 
0026 void debugOutput( QtMsgType type, const QMessageLogContext &context, const QString &msg )
0027 {
0028     switch ( type ) {
0029     case QtDebugMsg:
0030         if ( debugLevel == Debug ) {
0031             qDebug() << "Debug: " << context.file << ":" << context.line << " " << msg;
0032         }
0033         break;
0034     case QtInfoMsg:
0035     case QtWarningMsg:
0036         if ( debugLevel < Mute ) {
0037             qDebug() << "Info: " << context.file << ":" << context.line << " " << msg;
0038         }
0039         break;
0040     case QtCriticalMsg:
0041         if ( debugLevel < Mute ) {
0042             qDebug() << "Warning: " << context.file << ":" << context.line << " " << msg;
0043         }
0044         break;
0045     case QtFatalMsg:
0046         if ( debugLevel < Mute ) {
0047             qDebug() << "Fatal: " << context.file << ":" << context.line << " " << msg;
0048             abort();
0049         }
0050     }
0051 }
0052 
0053 void usage()
0054 {
0055     qDebug() << "Usage: [options] osm-addresses [options] input.osm.pbf|input.osm output.sqlite output.kml";
0056     qDebug() << "\tOptions affect verbosity and store additional metadata in output.kml:";
0057     qDebug() << "\t-q quiet";
0058     qDebug() << "\t-v debug output";
0059     qDebug() << "\t--version aVersion";
0060     qDebug() << "\t--name aName";
0061     qDebug() << "\t--date aDate";
0062     qDebug() << "\t--payload aFilename";
0063 }
0064 
0065 int main( int argc, char *argv[] )
0066 {
0067     if ( argc < 4 ) {
0068         usage();
0069         return 1;
0070     }
0071 
0072     QCoreApplication app( argc, argv );
0073 
0074     QString inputFile = argv[argc-3];
0075     QString outputSqlite = argv[argc-2];
0076     QString outputKml = argv[argc-1];
0077     QString name;
0078     QString version;
0079     QString date;
0080     QString transport;
0081     QString payload;
0082     for ( int i=1; i<argc-3; ++i ) {
0083         QString arg( argv[i] );
0084         if (arg == QLatin1String("-v")) {
0085             debugLevel = Debug;
0086         } else if (arg == QLatin1String("-q")) {
0087             debugLevel = Mute;
0088         } else if (arg == QLatin1String("--name")) {
0089             name = argv[++i];
0090         } else if (arg == QLatin1String("--version")) {
0091             version = argv[++i];
0092         } else if (arg == QLatin1String("--date")) {
0093             date = argv[++i];
0094         } else if (arg == QLatin1String("--transport")) {
0095             transport = argv[++i];
0096         } else if (arg == QLatin1String("--payload")) {
0097             payload = argv[++i];
0098         } else {
0099             usage();
0100             return 1;
0101         }
0102     }
0103 
0104     qInstallMessageHandler( debugOutput );
0105 
0106     QFileInfo file( inputFile );
0107     if ( !file.exists() ) {
0108         qDebug() << "File " << file.absoluteFilePath() << " does not exist. Exiting.";
0109         return 2;
0110     }
0111 
0112     OsmParser* parser = nullptr;
0113     if ( file.fileName().endsWith( QLatin1String( ".osm" ) ) ) {
0114         parser = new XmlParser;
0115     } else if ( file.fileName().endsWith( QLatin1String( ".pbf" ) ) ) {
0116         parser = new PbfParser;
0117     } else {
0118         qDebug() << "Unsupported file format: " << file.fileName();
0119         return 3;
0120     }
0121 
0122     Q_ASSERT( parser );
0123     SqlWriter sql( outputSqlite );
0124     parser->addWriter( &sql );
0125     parser->read( file, name );
0126     parser->writeKml( name, version, date, transport, payload, outputKml );
0127 }