diff --git a/src/core/qgsabstractgeopdfexporter.cpp b/src/core/qgsabstractgeopdfexporter.cpp index d54953fe7d02..91ce4f5ef5d1 100644 --- a/src/core/qgsabstractgeopdfexporter.cpp +++ b/src/core/qgsabstractgeopdfexporter.cpp @@ -369,30 +369,33 @@ QString QgsAbstractGeoPdfExporter::createCompositionXml( const QList #include "qgsfeature.h" #include "qgsabstractmetadatabase.h" +#include "qgspolygon.h" #define SIP_NO_FILE @@ -153,9 +154,21 @@ class CORE_EXPORT QgsAbstractGeoPdfExporter struct GeoReferencedSection { - //! Bounds of the georeferenced section on the page, in millimeters + + /** + * Bounds of the georeferenced section on the page, in millimeters. + * + * \note if pageBoundsPolygon is specified then this setting is ignored. + */ QgsRectangle pageBoundsMm; + /** + * Bounds of the georeferenced section on the page, in millimeters, as a free-form polygon. + * + * If specified, this will be used instead of pageBoundsMm. + */ + QgsPolygon pageBoundsPolygon; + //! Coordinate reference system for georeferenced section QgsCoordinateReferenceSystem crs; diff --git a/tests/src/core/testqgsgeopdfexport.cpp b/tests/src/core/testqgsgeopdfexport.cpp index 2740ffd07ec3..b08056c4f30a 100644 --- a/tests/src/core/testqgsgeopdfexport.cpp +++ b/tests/src/core/testqgsgeopdfexport.cpp @@ -58,6 +58,7 @@ class TestQgsGeoPdfExport : public QObject void testComposition(); void testMetadata(); void testGeoref(); + void testGeorefPolygon(); void testGroups(); private: @@ -352,6 +353,51 @@ void TestQgsGeoPdfExport::testGeoref() QCOMPARE( cp1.attribute( QStringLiteral( "y" ) ), QStringLiteral( "-2.83465" ) ); } +void TestQgsGeoPdfExport::testGeorefPolygon() +{ + // test georeferencing a region using polygon bounds + TestGeoPdfExporter geoPdfExporter; + + QList< QgsAbstractGeoPdfExporter::ComponentLayerDetail > renderedLayers; + QgsAbstractGeoPdfExporter::ExportDetails details; + + // with points + QgsAbstractGeoPdfExporter::GeoReferencedSection section; + section.crs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4283" ) ); + section.pageBoundsMm = QgsRectangle( 0, 0, 253.2, 222.25 ); + QgsPolygon p; + p.fromWkt( QStringLiteral( "Polygon((30 5, 250 15, 240 200, 50 190, 30 5))" ) ); + section.pageBoundsPolygon = p; + section.controlPoints.append( QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( 0, 0 ), QgsPointXY( -122.4, 53.6 ) ) ); + section.controlPoints.append( QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( 253.2, 0 ), QgsPointXY( -78, 53.6 ) ) ); + section.controlPoints.append( QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( 253.2, 222.25 ), QgsPointXY( -78, 14.6 ) ) ); + section.controlPoints.append( QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( 0, 222.25 ), QgsPointXY( -122.4, 14.6 ) ) ); + details.georeferencedSections << section; + + QString composition = geoPdfExporter.createCompositionXml( renderedLayers, details ); + QgsDebugMsg( composition ); + QDomDocument doc; + doc.setContent( composition ); + QCOMPARE( doc.elementsByTagName( QStringLiteral( "SRS" ) ).at( 0 ).toElement().text(), QStringLiteral( "EPSG:4283" ) ); + QCOMPARE( doc.elementsByTagName( QStringLiteral( "BoundingPolygon" ) ).at( 0 ).toElement().text(), QStringLiteral( "Polygon ((30 5, 250 15, 240 200, 50 190, 30 5))" ) ); + + QDomNodeList cps = doc.elementsByTagName( QStringLiteral( "ControlPoint" ) ); + QCOMPARE( cps.count(), 4 ); + QDomElement cp1; + for ( int i = 0; i < 4; ++i ) + { + if ( cps.at( i ).toElement().attribute( QStringLiteral( "GeoX" ) ) == QStringLiteral( "-122.4" ) + && cps.at( i ).toElement().attribute( QStringLiteral( "GeoY" ) ) == QStringLiteral( "53.6" ) ) + { + cp1 = cps.at( i ).toElement(); + break; + } + } + QVERIFY( !cp1.isNull() ); + QCOMPARE( cp1.attribute( QStringLiteral( "x" ) ), QStringLiteral( "0" ) ); + QCOMPARE( cp1.attribute( QStringLiteral( "y" ) ), QStringLiteral( "-2.83465" ) ); +} + void TestQgsGeoPdfExport::testGroups() { TestGeoPdfExporter geoPdfExporter;