File indexing completed on 2024-10-13 03:34:45

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 //
0006 
0007 
0008 #include "svgxmlhandler.h"
0009 #include <QDebug>
0010 #include <QDataStream>
0011 
0012 SVGXmlHandler::SVGXmlHandler(const QString& targetfile)
0013 {
0014     m_header      = 8000;
0015     m_pointnum    = 0;
0016     m_initialized = false;
0017     m_targetfile  = targetfile;
0018 }
0019 
0020 bool SVGXmlHandler::startElement(const QString& nspace,
0021                                  const QString& localName,
0022                                  const QString& qName,
0023                                  const QXmlAttributes &atts)
0024 {
0025     Q_UNUSED(nspace)
0026     Q_UNUSED(localName)
0027 
0028     qDebug();
0029     if (qName == QLatin1String("g")) {
0030         qDebug( "Parsing Data ..." );
0031         m_initialized = true;
0032     }
0033 
0034     if (qName == QLatin1String("path") && m_initialized) {
0035         QString  coordinates = atts.value( "d" );
0036 
0037         QStringList  stringlist;
0038         coordinates.chop(2);
0039         stringlist << coordinates.mid(1).split(QLatin1Char('L'));
0040         // The last element is the first element
0041         //      stringlist.removeLast();
0042         bool     firstheader = true;
0043         QFile    file( m_targetfile );
0044 
0045         // Read the data serialized from the file.
0046         file.open( QIODevice::Append );
0047         QDataStream stream( &file );
0048         stream.setByteOrder( QDataStream::LittleEndian );
0049 
0050         int  count = 0;
0051         qDebug() << "Starting to write path" << atts.value( "id" );
0052         for ( const QString& str: stringlist ) {
0053             // qDebug()<<str;
0054             float  x;
0055             float  y;
0056             x = str.section(QLatin1Char(','), 0, 0).toFloat();
0057             y = str.section(QLatin1Char(','), 1, 1).toFloat();
0058             
0059             // qDebug() << "x:" << x << "y:" << y;
0060             
0061             short  header;
0062             short  lat;
0063             short  lng; 
0064 
0065             if ( firstheader ) {
0066                 header      = m_header;
0067                 firstheader = false;
0068             }
0069             else {
0070                 if ( stringlist.size() > 14 ) {
0071                     if ( count % 9 == 0 ) 
0072                         header = 5;
0073                     else if ( count % 5 == 0 )
0074                         header = 3;
0075                     else if ( count % 2 == 0 )
0076                         header = 2;
0077                     else
0078                         header = 1;
0079                 }
0080                 else if ( stringlist.size() > 6 ) {
0081                     if ( count % 2 == 0 )
0082                         header = 3;
0083                     else
0084                         header = 1;
0085                 }
0086                 else {
0087                     header = 2;
0088                 }
0089             }
0090             if ( count == stringlist.size() - 1 )
0091                 header = 5;
0092 
0093             lng =  (int)( x * 50 - 10800 );
0094             lat = -(int)( y * 50 - 5400 );
0095 
0096             qDebug() << "lng:" << lng << "lat:" << lat << "header:"
0097                      << header << "node#:" << m_pointnum;
0098 
0099             stream << header << lat << lng; 
0100             m_pointnum++;   
0101             count++;        
0102         }
0103         
0104         m_header++;
0105     }
0106 
0107     return true;
0108 }