File indexing completed on 2024-04-21 08:30:47

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <overpassquery.h>
0008 #include <overpassquerymanager.h>
0009 
0010 #include <QCommandLineParser>
0011 #include <QCoreApplication>
0012 #include <QDebug>
0013 #include <QFile>
0014 #include <QJsonDocument>
0015 
0016 #include <iostream>
0017 
0018 #define S(x) QStringLiteral(x)
0019 
0020 int main(int argc, char **argv)
0021 {
0022     QCoreApplication app(argc, argv);
0023     QCommandLineParser parser;
0024     parser.setApplicationDescription(S("overpassql-cli"));
0025     parser.addHelpOption();
0026     QCommandLineOption queryOption({ S("q"), S("query") }, S("Overpass QL query to run"), S("query-string"));
0027     parser.addOption(queryOption);
0028     QCommandLineOption queryFileOption({ S("f"), S("query-file") }, S("File to read Overpass QL query from"), S("query-file"));
0029     parser.addOption(queryFileOption);
0030     QCommandLineOption bboxOption({ S("b"), S("bbox") }, S("Query bounding box"), S("x,y,w,h"));
0031     parser.addOption(bboxOption);
0032     QCommandLineOption tileSizeOption({ S("t"), S("tile-size") }, S("Query tile size"), S("w,h"));
0033     parser.addOption(tileSizeOption);
0034     parser.process(app);
0035 
0036     OSM::OverpassQueryManager mgr;
0037     OSM::OverpassQuery query;
0038 
0039     if (parser.isSet(queryFileOption)) {
0040         QFile f(parser.value(queryFileOption));
0041         if (!f.open(QFile::ReadOnly)) {
0042             std::cerr << "failed to read query file: " << qPrintable(f.errorString()) << std::endl;
0043             return 1;
0044         }
0045         query.setQuery(QString::fromUtf8(f.readAll()));
0046     } else {
0047         query.setQuery(parser.value(queryOption));
0048     }
0049 
0050     if (parser.isSet(bboxOption)) {
0051         const auto s = parser.value(bboxOption).split(QLatin1Char(','));
0052         if (s.size() != 4) {
0053             std::cerr << "invalid bbox format" << std::endl;
0054             return 1;
0055         }
0056         QRectF bbox(s[0].toDouble(), s[1].toDouble(), s[2].toDouble(), s[3].toDouble());
0057         query.setBoundingBox(bbox);
0058     }
0059 
0060     if (parser.isSet(tileSizeOption)) {
0061         const auto s = parser.value(tileSizeOption).split(QLatin1Char(','));
0062         if (s.size() != 2) {
0063             std::cerr << "invalid tile size format" << std::endl;
0064             return 1;
0065         }
0066         QSizeF tileSize(s[0].toDouble(), s[1].toDouble());
0067         query.setTileSize(tileSize);
0068     }
0069 
0070     QObject::connect(&query, &OSM::OverpassQuery::finished, [&query, &app]() {
0071         if (query.error() != OSM::OverpassQuery::NoError) {
0072             std::cerr << "query error" << std::endl;
0073             app.exit(1);
0074         }
0075 
0076         // ### for testing only
0077         const auto &res = query.result();
0078         for (const auto &node : res.nodes) {
0079             qDebug() << "Node" << node.id << node.coordinate.latitude << node.coordinate.longitude;
0080             for (const auto &tag : node.tags) {
0081                 qDebug() << "  tag" << tag.key.name() << tag.value;
0082             }
0083         }
0084         for (const auto &rel : res.relations) {
0085             qDebug() << "Relation" << rel.id << rel.bbox.min.latitude << rel.bbox.min.longitude << rel.bbox.max.latitude << rel.bbox.max.longitude;
0086             for (const auto &mem : rel.members) {
0087                 qDebug() << "  member" << mem.id << (int)mem.type() << mem.role().name();
0088             }
0089             for (const auto &tag : rel.tags) {
0090                 qDebug() << "  tag" << tag.key.name() << tag.value;
0091             }
0092         }
0093 
0094         app.quit();
0095     });
0096     mgr.execute(&query);
0097 
0098     return app.exec();
0099 }