File indexing completed on 2024-04-28 03:50:32

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0004 
0005 #include "LogRunner.h"
0006 
0007 #include "GeoDataDocument.h"
0008 #include "GeoDataLineString.h"
0009 #include "GeoDataPlacemark.h"
0010 #include "MarbleDebug.h"
0011 
0012 #include <QFile>
0013 
0014 namespace Marble
0015 {
0016 
0017 LogRunner::LogRunner( QObject *parent ) :
0018     ParsingRunner( parent )
0019 {
0020 }
0021 
0022 LogRunner::~LogRunner()
0023 {
0024 }
0025 
0026 GeoDataDocument *LogRunner::parseFile(const QString &fileName, DocumentRole role, QString &errorString)
0027 {
0028     QFile file( fileName );
0029     if ( !file.exists() ) {
0030         errorString = QStringLiteral("File %1 does not exist").arg(fileName);
0031         mDebug() << errorString;
0032         return nullptr;
0033     }
0034 
0035     file.open( QIODevice::ReadOnly );
0036     QTextStream stream( &file );
0037 
0038     GeoDataLineString *const track = new GeoDataLineString;
0039 
0040     GeoDataPlacemark *const placemark = new GeoDataPlacemark;
0041     placemark->setGeometry( track );
0042 
0043     GeoDataDocument *document = new GeoDataDocument();
0044     document->setDocumentRole( role );
0045     document->append( placemark );
0046 
0047     int count = 0;
0048     bool error = false;
0049     while( !stream.atEnd() || error ){
0050         const QString line = stream.readLine();
0051         const QStringList list = line.split(QLatin1Char(','));
0052 
0053         if ( list.size() != 7 ) {
0054             mDebug() << "Aborting due to error in line" << count << ". Line was:" << line;
0055             error = true;
0056             break;
0057         }
0058 
0059         const QString strLat = list[0];
0060         const QString strLon = list[1];
0061         const QString strElevation = list[2];
0062         const QString strSpeed = list[3];
0063         const QString strCourse = list[4];
0064         const QString strHdop = list[5];
0065         const QString strTime = list[6];
0066 
0067         if ( strLat.isEmpty() || strLon.isEmpty() || strElevation.isEmpty() ) {
0068             continue;
0069         }
0070 
0071         bool okLat, okLon, okAlt = false;
0072         const qreal lat = strLat.toDouble( &okLat );
0073         const qreal lon = strLon.toDouble( &okLon );
0074         const qreal alt = strElevation.toDouble( &okAlt );
0075 
0076         if ( !okLat || !okLon || !okAlt ) {
0077             continue;
0078         }
0079 
0080         GeoDataCoordinates coord( lon, lat, alt, GeoDataCoordinates::Degree );
0081         track->append( coord );
0082     }
0083 
0084     file.close();
0085     if ( track->size() == 0 || error ) {
0086         delete document;
0087         document = nullptr;
0088         return nullptr;
0089     }
0090 
0091     document->setFileName( fileName );
0092     return document;
0093 }
0094 
0095 }
0096 
0097 #include "moc_LogRunner.cpp"