File indexing completed on 2024-03-24 03:53:34

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Torsten Rahn <tackat@kde.org>
0004 //
0005 
0006 
0007 #include <QCoreApplication>
0008 #include <QDataStream>
0009 #include <QFile>
0010 #include <QStringList>
0011 #include <QTextStream>
0012 #include <QDebug>
0013 
0014 int main(int argc, char *argv[])
0015 {
0016     const qreal INT2SVG = 216.0 / 10800.0;
0017     const qreal INT2DEG = 180.0 / 10800.0;
0018 
0019     QCoreApplication  app(argc, argv);
0020 
0021     qDebug( " Syntax: pnt2svg [-i pnt-sourcefile -o svg-targetfile -cn clipNorth -cs clipSouth -cw clipWest -ce clipEast]" );
0022 
0023     QString inputFilename("PBORDERS.PNT");
0024     int inputIndex = app.arguments().indexOf("-i");
0025     if (inputIndex > 0 && inputIndex + 1 < argc )
0026         inputFilename = app.arguments().at( inputIndex + 1 );
0027 
0028     QString outputFilename("output.svg");
0029     int outputIndex = app.arguments().indexOf("-o");
0030     if (outputIndex > 0 && outputIndex + 1 < argc )
0031         outputFilename = app.arguments().at( outputIndex + 1 );
0032 
0033     qreal clipNorth = 90.0;
0034     int clipNorthIndex = app.arguments().indexOf("-cn");
0035     if (clipNorthIndex > 0 && clipNorthIndex + 1 < argc )
0036         clipNorth = app.arguments().at( clipNorthIndex + 1 ).toDouble();
0037 
0038     qreal clipSouth = -90.0;
0039     int clipSouthIndex = app.arguments().indexOf("-cs");
0040     if (clipSouthIndex > 0 && clipSouthIndex + 1 < argc )
0041         clipSouth = app.arguments().at( clipSouthIndex + 1 ).toDouble();
0042 
0043     qreal clipEast = 180.0;
0044     int clipEastIndex = app.arguments().indexOf("-ce");
0045     if (clipEastIndex > 0 && clipEastIndex + 1 < argc )
0046         clipEast = app.arguments().at( clipEastIndex + 1 ).toDouble();
0047 
0048     qreal clipWest = -180.0;
0049     int clipWestIndex = app.arguments().indexOf("-cw");
0050     if (clipWestIndex > 0 && clipWestIndex + 1 < argc )
0051         clipWest = app.arguments().at( clipWestIndex + 1 ).toDouble();
0052 
0053     qDebug() << "input filename:" << inputFilename;
0054     qDebug() << "output filename:" << outputFilename;
0055     qDebug() << "clipNorth:" << clipNorth;
0056     qDebug() << "clipSouth:" << clipSouth;
0057     qDebug() << "clipWest:" << clipWest;
0058     qDebug() << "clipEast:" << clipEast;
0059 
0060     // INPUT
0061     QStringList pathList;
0062     QString pathString;
0063     QString closePolygon;
0064     QString styleString;
0065     int    count = 0;
0066     int    origHeader = 0;
0067     int    pathIndex = 0;
0068 
0069     QFile  file( inputFilename );
0070 
0071     if ( file.open( QIODevice::ReadOnly ) ) {
0072         QDataStream stream( &file );  // read the data serialized from the file
0073         stream.setByteOrder( QDataStream::LittleEndian );
0074 
0075         short  header;
0076         short  iLat;
0077         short  iLon;
0078 
0079         bool clipped = false;
0080 
0081         while( !stream.atEnd() ){
0082             stream >> header >> iLat >> iLon;
0083             // Transforming Range of Coordinates to iLat [0,ARCMINUTE] , iLon [0,2 * ARCMINUTE]
0084 
0085             if ( header > 5 ) {
0086                 // Find out whether the Polyline is a river or a closed polygon
0087                 if ( ( header >= 7000 && header < 8000 )
0088                      || ( header >= 9000 && header < 20000 ) ) {
0089                     closePolygon=" \"";
0090                     styleString = QString(" fill=\"none\" stroke=\"black\" stroke-width=\"0.02\"");
0091                 }
0092                 else {
0093                     closePolygon=" z \"";
0094                     styleString = QString(" fill=\"lightgrey\" stroke=\"black\" stroke-width=\"0.02\"");
0095                 }
0096                 // Finish old path
0097                 if (!pathString.isEmpty() && !clipped) {
0098                     pathString += closePolygon;
0099                     pathString += styleString;
0100                     pathString += QString(" id=\"path%1_%2\" />").arg(pathIndex).arg(origHeader);
0101                     pathList.append(pathString);
0102                 }
0103                 // Start new path
0104                 origHeader = header;
0105                 clipped = false;
0106                 pathString = QString("<path d=\"M %1, %2").arg((qreal)(iLon) * INT2SVG + 216 ).arg(-(qreal)(iLat) * INT2SVG + 108);
0107                 ++pathIndex;
0108             }
0109             else {
0110                 pathString += QString(" L %1, %2").arg((qreal)(iLon) * INT2SVG + 216 ).arg(-(qreal)(iLat) * INT2SVG + 108);
0111             }
0112             ++count;
0113 
0114             if ((qreal)(iLat) * INT2DEG > clipNorth || (qreal)(iLat) * INT2DEG < clipSouth)
0115                 clipped = true;
0116             if ((qreal)(iLon) * INT2DEG > clipEast || (qreal)(iLon) * INT2DEG < clipWest)
0117                 clipped = true;
0118         }
0119 
0120         // Finish old path
0121         if (!pathString.isEmpty() && !clipped) {
0122             pathString += closePolygon;
0123             pathString += QString(" id=\"path%1_%2\" />").arg(count).arg(origHeader);
0124                     if (!pathString.isEmpty()) pathList.append(pathString);
0125         }
0126 
0127         file.close();
0128     }
0129     else {
0130         qDebug() << "ERROR: Source file not found!";
0131     }
0132 
0133 
0134     // OUTPUT
0135     QFile data(outputFilename);
0136     if (data.open(QFile::WriteOnly | QFile::Truncate)) {
0137         QTextStream out(&data);
0138 
0139         out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << endl;
0140         out << "<svg width=\"432.00000px\" height=\"216.00000px\">" << endl;
0141         for ( const QString & path: pathList)
0142             out << path << endl;
0143         out << "</svg>" << endl;
0144         qDebug() << "Done!";
0145         data.close();
0146     }
0147     else {
0148         qDebug() << "ERROR: Couldn't write output file to disc!";
0149     }
0150 
0151     app.exit();
0152 }