Skip to content

Commit

Permalink
Correctly georeference layout geopdf exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 20, 2019
1 parent edf81aa commit 3bb93fc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
5 changes: 5 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemmap.sip.in
Expand Up @@ -612,6 +612,11 @@ Removes a previously added rendered feature ``handler``.
.. seealso:: :py:func:`addRenderedFeatureHandler`

.. versionadded:: 3.10
%End

QTransform layoutToMapCoordsTransform() const;
%Docstring
Creates a transform from layout coordinates to map coordinates.
%End

protected:
Expand Down
41 changes: 41 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsabstractlayoutiterator.h"
#include "qgsfeedback.h"
#include "qgslayoutgeopdfexporter.h"
#include "qgslinestring.h"
#include <QImageWriter>
#include <QSize>
#include <QSvgGenerator>
Expand Down Expand Up @@ -595,6 +596,46 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( const QString &f
details.keywords = mLayout->project()->metadata().keywords();
}

if ( settings.appendGeoreference )
{
// setup georeferencing
QList< QgsLayoutItemMap * > maps;
mLayout->layoutItems( maps );
for ( QgsLayoutItemMap *map : qgis::as_const( maps ) )
{
QgsAbstractGeoPdfExporter::GeoReferencedSection georef;
georef.crs = map->crs();

const QPointF topLeft = map->mapToScene( QPointF( 0, 0 ) );
const QPointF topRight = map->mapToScene( QPointF( map->rect().width(), 0 ) );
const QPointF bottomLeft = map->mapToScene( QPointF( 0, map->rect().height() ) );
const QPointF bottomRight = map->mapToScene( QPointF( map->rect().width(), map->rect().height() ) );
const QgsLayoutPoint topLeftMm = mLayout->convertFromLayoutUnits( topLeft, QgsUnitTypes::LayoutMillimeters );
const QgsLayoutPoint topRightMm = mLayout->convertFromLayoutUnits( topRight, QgsUnitTypes::LayoutMillimeters );
const QgsLayoutPoint bottomLeftMm = mLayout->convertFromLayoutUnits( bottomLeft, QgsUnitTypes::LayoutMillimeters );
const QgsLayoutPoint bottomRightMm = mLayout->convertFromLayoutUnits( bottomRight, QgsUnitTypes::LayoutMillimeters );

georef.pageBoundsPolygon.setExteriorRing( new QgsLineString( QVector< QgsPointXY >() << QgsPointXY( topLeftMm.x(), topLeftMm.y() )
<< QgsPointXY( topRightMm.x(), topRightMm.y() )
<< QgsPointXY( bottomRightMm.x(), bottomRightMm.y() )
<< QgsPointXY( bottomLeftMm.x(), bottomLeftMm.y() )
<< QgsPointXY( topLeftMm.x(), topLeftMm.y() ) ) );

georef.controlPoints.reserve( 4 );
const QTransform t = map->layoutToMapCoordsTransform();
const QgsPointXY topLeftMap = t.map( topLeft );
const QgsPointXY topRightMap = t.map( topRight );
const QgsPointXY bottomLeftMap = t.map( bottomLeft );
const QgsPointXY bottomRightMap = t.map( bottomRight );

georef.controlPoints << QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( topLeftMm.x(), topLeftMm.y() ), topLeftMap );
georef.controlPoints << QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( topRightMm.x(), topRightMm.y() ), topRightMap );
georef.controlPoints << QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( bottomLeftMm.x(), bottomLeftMm.y() ), bottomLeftMap );
georef.controlPoints << QgsAbstractGeoPdfExporter::ControlPoint( QgsPointXY( bottomRightMm.x(), bottomRightMm.y() ), bottomRightMap );
details.georeferencedSections << georef;
}
}

details.includeFeatures = settings.includeGeoPdfFeatures;
details.useOgcBestPracticeFormatGeoreferencing = settings.useOgcBestPracticeFormatGeoreferencing;
details.useIso32000ExtensionFormatGeoreferencing = settings.useIso32000ExtensionFormatGeoreferencing;
Expand Down
10 changes: 5 additions & 5 deletions src/core/layout/qgslayoutitemmap.h
Expand Up @@ -549,6 +549,11 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
*/
void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );

/**
* Creates a transform from layout coordinates to map coordinates.
*/
QTransform layoutToMapCoordsTransform() const;

protected:

void draw( QgsLayoutItemRenderContext &context ) override;
Expand Down Expand Up @@ -731,11 +736,6 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
//! Returns first map overview or creates an empty one if none
const QgsLayoutItemMapOverview *constFirstMapOverview() const;

/**
* Creates a transform from layout coordinates to map coordinates.
*/
QTransform layoutToMapCoordsTransform() const;

/**
* Creates a list of label blocking regions for the map, which correspond to the
* map areas covered by other layout items marked as label blockers for this map.
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsabstractgeopdfexporter.cpp
Expand Up @@ -346,10 +346,11 @@ QString QgsAbstractGeoPdfExporter::createCompositionXml( const QList<ComponentLa


// georeferencing
int i = 0;
for ( const QgsAbstractGeoPdfExporter::GeoReferencedSection &section : details.georeferencedSections )
{
QDomElement georeferencing = doc.createElement( QStringLiteral( "Georeferencing" ) );
georeferencing.setAttribute( QStringLiteral( "id" ), QStringLiteral( "georeferenced" ) );
georeferencing.setAttribute( QStringLiteral( "id" ), QStringLiteral( "georeferenced_%1" ).arg( i++ ) );
georeferencing.setAttribute( QStringLiteral( "OGCBestPracticeFormat" ), details.useOgcBestPracticeFormatGeoreferencing ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
georeferencing.setAttribute( QStringLiteral( "ISO32000ExtensionFormat" ), details.useIso32000ExtensionFormatGeoreferencing ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );

Expand Down

0 comments on commit 3bb93fc

Please sign in to comment.