@@ -62,6 +62,12 @@ class TestQgsVectorFileWriter: public QObject
62
62
public:
63
63
TestQgsVectorFileWriter ();
64
64
65
+ void _testExportToGpx ( const QString &geomTypeName,
66
+ const QString &wkt,
67
+ const QString &expectedLayerName,
68
+ const QString &inputLayerName = QStringLiteral( " test" ),
69
+ const QStringList &layerOptions = QStringList() );
70
+
65
71
private slots:
66
72
void initTestCase ();// will be called before the first testfunction is executed.
67
73
void init () {} // will be called before each testfunction is executed.
@@ -84,6 +90,20 @@ class TestQgsVectorFileWriter: public QObject
84
90
void prepareWriteAsVectorFormat ();
85
91
// ! Test regression #21714 (Exported GeoPackages have wrong field definitions)
86
92
void testTextFieldLength ();
93
+ // ! Test https://issues.qgis.org/issues/22005
94
+ void testExportToGpxPoint ();
95
+ // ! Test https://issues.qgis.org/issues/22005
96
+ void testExportToGpxPointTrackPoints ();
97
+ // ! Test https://issues.qgis.org/issues/22005
98
+ void testExportToGpxPointRoutePoints ();
99
+ // ! Test https://issues.qgis.org/issues/22005
100
+ void testExportToGpxLineString ();
101
+ // ! Test https://issues.qgis.org/issues/22005
102
+ void testExportToGpxLineStringForceTrack ();
103
+ // ! Test https://issues.qgis.org/issues/22005
104
+ void testExportToGpxMultiLineString ();
105
+ // ! Test https://issues.qgis.org/issues/22005
106
+ void testExportToGpxMultiLineStringForceRoute ();
87
107
88
108
private:
89
109
// a little util fn used by all tests
@@ -524,5 +544,111 @@ void TestQgsVectorFileWriter::testTextFieldLength()
524
544
525
545
}
526
546
547
+ void TestQgsVectorFileWriter::_testExportToGpx ( const QString &geomTypeName,
548
+ const QString &wkt,
549
+ const QString &expectedLayerName,
550
+ const QString &inputLayerName,
551
+ const QStringList &layerOptions )
552
+ {
553
+ QTemporaryFile tmpFile ( QDir::tempPath () + " /test_qgsvectorfilewriter_testExportToGpx" + geomTypeName + " _XXXXXX.gpx" );
554
+ tmpFile.open ();
555
+ QString fileName ( tmpFile.fileName ( ) );
556
+ QString memLayerDef ( geomTypeName );
557
+ if ( inputLayerName == QLatin1String ( " track_points" ) )
558
+ {
559
+ memLayerDef += QStringLiteral ( " ?field=track_fid:int&field=track_seg_id:int" );
560
+ }
561
+ else if ( inputLayerName == QLatin1String ( " route_points" ) )
562
+ {
563
+ memLayerDef += QStringLiteral ( " ?field=route_fid:int" );
564
+ }
565
+ QgsVectorLayer vl ( memLayerDef, " test" , " memory" );
566
+ QgsFeature f { vl.fields () };
567
+ if ( inputLayerName == QLatin1String ( " track_points" ) )
568
+ {
569
+ f.setAttribute ( 0 , 1 );
570
+ f.setAttribute ( 1 , 1 );
571
+ }
572
+ else if ( inputLayerName == QLatin1String ( " route_points" ) )
573
+ {
574
+ f.setAttribute ( 0 , 1 );
575
+ }
576
+ f.setGeometry ( QgsGeometry::fromWkt ( wkt ) );
577
+ QVERIFY ( vl.startEditing () );
578
+ QVERIFY ( vl.addFeature ( f ) );
579
+ QgsVectorFileWriter::SaveVectorOptions options;
580
+ options.driverName = " GPX" ;
581
+ options.layerName = inputLayerName;
582
+ options.layerOptions = layerOptions;
583
+ QString outLayerName;
584
+ QgsVectorFileWriter::WriterError error ( QgsVectorFileWriter::writeAsVectorFormat (
585
+ &vl,
586
+ fileName,
587
+ options,
588
+ nullptr , // newFilename
589
+ nullptr , // errorMessage
590
+ &outLayerName ) );
591
+ QCOMPARE ( error, QgsVectorFileWriter::WriterError::NoError );
592
+ QCOMPARE ( outLayerName, expectedLayerName );
593
+ QgsVectorLayer vl2 ( QStringLiteral ( " %1|layername=%2" ).arg ( fileName ).arg ( outLayerName ), " src_test" , " ogr" );
594
+ QVERIFY ( vl2.isValid () );
595
+ QCOMPARE ( vl2.featureCount (), 1L );
596
+ }
597
+
598
+ void TestQgsVectorFileWriter::testExportToGpxPoint ()
599
+ {
600
+ _testExportToGpx ( QStringLiteral ( " Point" ),
601
+ QStringLiteral ( " point(9 45)" ),
602
+ QStringLiteral ( " waypoints" ) );
603
+ }
604
+
605
+ void TestQgsVectorFileWriter::testExportToGpxPointTrackPoints ()
606
+ {
607
+ _testExportToGpx ( QStringLiteral ( " Point" ),
608
+ QStringLiteral ( " point(9 45)" ),
609
+ QStringLiteral ( " track_points" ),
610
+ QStringLiteral ( " track_points" ) );
611
+ }
612
+
613
+ void TestQgsVectorFileWriter::testExportToGpxPointRoutePoints ()
614
+ {
615
+ _testExportToGpx ( QStringLiteral ( " Point" ),
616
+ QStringLiteral ( " point(9 45)" ),
617
+ QStringLiteral ( " route_points" ),
618
+ QStringLiteral ( " route_points" ) );
619
+ }
620
+
621
+ void TestQgsVectorFileWriter::testExportToGpxLineString ()
622
+ {
623
+ _testExportToGpx ( QStringLiteral ( " LineString" ),
624
+ QStringLiteral ( " linestring(9 45,10 46)" ),
625
+ QStringLiteral ( " routes" ) );
626
+ }
627
+
628
+ void TestQgsVectorFileWriter::testExportToGpxLineStringForceTrack ()
629
+ {
630
+ _testExportToGpx ( QStringLiteral ( " LineString" ),
631
+ QStringLiteral ( " linestring(9 45,10 46)" ),
632
+ QStringLiteral ( " tracks" ),
633
+ QStringLiteral ( " test" ),
634
+ QStringList () << QStringLiteral ( " FORCE_GPX_TRACK=YES" ) );
635
+ }
636
+
637
+ void TestQgsVectorFileWriter::testExportToGpxMultiLineString ()
638
+ {
639
+ _testExportToGpx ( QStringLiteral ( " MultiLineString" ),
640
+ QStringLiteral ( " multilinestring((9 45,10 46))" ),
641
+ QStringLiteral ( " tracks" ) );
642
+ }
643
+
644
+ void TestQgsVectorFileWriter::testExportToGpxMultiLineStringForceRoute ()
645
+ {
646
+ _testExportToGpx ( QStringLiteral ( " MultiLineString" ),
647
+ QStringLiteral ( " multilinestring((9 45,10 46))" ),
648
+ QStringLiteral ( " routes" ),
649
+ QStringLiteral ( " test" ),
650
+ QStringList () << QStringLiteral ( " FORCE_GPX_ROUTE=YES" ) );
651
+ }
652
+
527
653
QGSTEST_MAIN ( TestQgsVectorFileWriter )
528
654
#include " testqgsvectorfilewriter.moc"
0 commit comments