QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgsabstractgeopdfexporter.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsabstractgeopdfexporter.h
3 --------------------------
4 begin : August 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#ifndef QGSABSTRACTGEOPDFEXPORTER_H
17#define QGSABSTRACTGEOPDFEXPORTER_H
18
19#include "qgis_core.h"
22#include "qgsfeature.h"
23#include "qgslayertree.h"
24#include "qgspolygon.h"
25
26#include <QDateTime>
27#include <QList>
28#include <QMutex>
29#include <QPainter>
30#include <QString>
31#include <QTemporaryDir>
32
33#define SIP_NO_FILE
34
35using namespace Qt::StringLiterals;
36
37
38class QgsGeospatialPdfRenderedFeatureHandler;
39
40
42struct TreeNode
43{
44 QString id;
45 bool initiallyVisible = false;
46 QString name;
47 QString mutuallyExclusiveGroupId;
48 QString mapLayerId;
49 std::vector< std::unique_ptr< TreeNode > > children;
50 TreeNode *parent = nullptr;
51 bool isRootNode = false;
52
53 void addChild( std::unique_ptr< TreeNode > child )
54 {
55 child->parent = this;
56 children.emplace_back( std::move( child ) );
57 }
58
59 QDomElement toElement( QDomDocument &doc ) const
60 {
61 QDomElement layerElement = doc.createElement( u"Layer"_s );
62 layerElement.setAttribute( u"id"_s, id );
63 layerElement.setAttribute( u"name"_s, name );
64 layerElement.setAttribute( u"initiallyVisible"_s, initiallyVisible ? u"true"_s : u"false"_s );
65 if ( !mutuallyExclusiveGroupId.isEmpty() )
66 layerElement.setAttribute( u"mutuallyExclusiveGroupId"_s, mutuallyExclusiveGroupId );
67
68 for ( const auto &child : children )
69 {
70 layerElement.appendChild( child->toElement( doc ) );
71 }
72
73 return layerElement;
74 }
75
76 void toChildrenElements( QDomDocument &doc, QDomElement &layerTreeElem ) const
77 {
78 for ( const auto &child : children )
79 {
80 layerTreeElem.appendChild( child->toElement( doc ) );
81 }
82 }
83
84 QDomElement createIfLayerOnElement( QDomDocument &doc, QDomElement &contentElement ) const
85 {
86 QDomElement element = doc.createElement( u"IfLayerOn"_s );
87 element.setAttribute( u"layerId"_s, id );
88 contentElement.appendChild( element );
89 return element;
90 }
91
92 QDomElement createNestedIfLayerOnElements( QDomDocument &doc, QDomElement &contentElement ) const
93 {
94 TreeNode *currentParent = parent;
95 QDomElement finalElement = doc.createElement( u"IfLayerOn"_s );
96 finalElement.setAttribute( u"layerId"_s, id );
97
98 QDomElement currentElement = finalElement;
99 while ( currentParent && !currentParent->isRootNode )
100 {
101 QDomElement ifGroupOn = doc.createElement( u"IfLayerOn"_s );
102 ifGroupOn.setAttribute( u"layerId"_s, currentParent->id );
103 ifGroupOn.appendChild( currentElement );
104 currentElement = ifGroupOn;
105 currentParent = currentParent->parent;
106 }
107 contentElement.appendChild( currentElement );
108 return finalElement;
109 }
110};
112
113
134{
135 public:
142 static bool geospatialPDFCreationAvailable();
143
151
153
155
181
187 struct CORE_EXPORT ComponentLayerDetail
188 {
190 QString name;
191
193 QString mapLayerId;
194
196 QString group;
197
200
202 QPainter::CompositionMode compositionMode = QPainter::CompositionMode_SourceOver;
203
205 double opacity = 1.0;
206 };
207
230
232 {
239
246
249
251 QList< QgsAbstractGeospatialPdfExporter::ControlPoint > controlPoints;
252 };
253
261 void pushRenderedFeature( const QString &layerId, const QgsAbstractGeospatialPdfExporter::RenderedFeature &feature, const QString &group = QString() );
262
264 {
267
269 double dpi = 300;
270
272 QList< QgsAbstractGeospatialPdfExporter::GeoReferencedSection > georeferencedSections;
273
275 QString author;
276
278 QString producer;
279
281 QString creator;
282
285
287 QString subject;
288
290 QString title;
291
294
303
314
318 bool includeFeatures = true;
319
334 QMap< QString, QString > customLayerTreeGroups;
335
343 QMap< QString, QString > layerIdToPdfLayerTreeNameMap;
344
353 QMap< QString, bool > initialLayerVisibility;
354
366 QStringList layerOrder;
367
381
389 QSet< QString > mutuallyExclusiveGroups;
390
408 bool useLayerTreeConfig = false;
409 };
410
424 bool finalize( const QList< QgsAbstractGeospatialPdfExporter::ComponentLayerDetail > &components, const QString &destinationFile, const ExportDetails &details );
425
429 QString errorMessage() const { return mErrorMessage; }
430
434 QString generateTemporaryFilepath( const QString &filename ) const;
435
442 static bool compositionModeSupported( QPainter::CompositionMode mode );
443
444 protected:
449 {
451 QString name;
452
454 QString mapLayerId;
455
457 QString group;
458
461
464
467 };
468
476 static constexpr double DPI_72 = 72 SIP_SKIP;
477
478 private:
479 QMutex mMutex;
480 QMap< QString, QMap< QString, QgsFeatureList > > mCollatedFeatures;
481
485 virtual VectorComponentDetail componentDetailForLayerId( const QString &layerId ) = 0;
486
490 virtual QgsLayerTree *layerTree() const = 0;
491
492 QList< VectorComponentDetail > mVectorComponents;
493
494 QString mErrorMessage;
495 QTemporaryDir mTemporaryDir;
496
497
498 bool saveTemporaryLayers();
499
500 QString createCompositionXml( const QList< QgsAbstractGeospatialPdfExporter::ComponentLayerDetail > &components, const ExportDetails &details ) const;
501 void createMetadataXmlSection( QDomElement &compositionElem, QDomDocument &doc, const ExportDetails &details ) const;
502 void createPageDimensionXmlSection( QDomElement &pageElem, QDomDocument &doc, const double pageWidthPdfUnits, const double pageHeightPdfUnits ) const;
503 void createGeoreferencingXmlSection( QDomElement &pageElem, QDomDocument &doc, const ExportDetails &details, const double pageWidthPdfUnits, const double pageHeightPdfUnits ) const;
504 void createContentXmlSection(
505 QDomElement &contentElem,
506 QDomDocument &doc,
507 const QMap< QString, TreeNode * > &groupNameToTreeNode,
508 const QMap< QString, TreeNode * > &layerIdToTreeNode,
509 const QList<ComponentLayerDetail> &components,
510 const ExportDetails &details
511 ) const;
512
513 void createLayerTreeAndContentXmlSections( QDomElement &compositionElem, QDomElement &pageElem, QDomDocument &doc, const QList<ComponentLayerDetail> &components, const ExportDetails &details ) const;
514 void createLayerTreeAndContentXmlSectionsFromLayerTree(
515 const QgsLayerTree *layerTree, QDomElement &compositionElem, QDomElement &pageElem, QDomDocument &doc, const QList<ComponentLayerDetail> &components, const ExportDetails &details
516 ) const;
517
521 std::unique_ptr< TreeNode > createPdfTreeNodes( QMap< QString, TreeNode * > &groupNameToTreeNode, QMap< QString, TreeNode * > &layerIdToTreeNode, const QgsLayerTreeGroup *layerTreeGroup ) const;
522
526 static QString compositionModeToString( QPainter::CompositionMode mode );
527
530};
531
532#endif //QGSABSTRACTGEOPDFEXPORTER_H
static QString geospatialPDFAvailabilityExplanation()
Returns a user-friendly, translated string explaining why Geospatial PDF export support is not availa...
static constexpr double DPI_72
Hardcode DPI of 72 to get correct page sizes in outputs.
bool finalize(const QList< QgsAbstractGeospatialPdfExporter::ComponentLayerDetail > &components, const QString &destinationFile, const ExportDetails &details)
To be called after the rendering operation is complete.
void pushRenderedFeature(const QString &layerId, const QgsAbstractGeospatialPdfExporter::RenderedFeature &feature, const QString &group=QString())
Called multiple times during the rendering operation, whenever a feature associated with the specifie...
virtual ~QgsAbstractGeospatialPdfExporter()=default
QString errorMessage() const
Returns the last error message encountered during the export.
static bool geospatialPDFCreationAvailable()
Returns true if the current QGIS build is capable of Geospatial PDF support.
QMap< QString, QStringList > KeywordMap
Map of vocabulary string to keyword list.
Represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
A geometry is the spatial representation of a feature.
Layer tree group node serves as a container for layers and further groups.
Namespace with helper functions for layer tree operations.
Represents a 2D point.
Definition qgspointxy.h:62
Polygon geometry type.
Definition qgspolygon.h:37
A rectangle specified with double values.
#define SIP_SKIP
Definition qgis_sip.h:133
Contains details of a particular input component to be used during PDF composition.
QString mapLayerId
Associated map layer ID, or an empty string if this component layer is not associated with a map laye...
QString group
Optional group name, for arranging layers in top-level groups.
QString name
User-friendly name for the generated PDF layer.
QPainter::CompositionMode compositionMode
Component composition mode.
QString sourcePdfPath
File path to the (already created) PDF to use as the source for this component layer.
QgsPointXY pagePoint
Coordinate on the page of the control point, in millimeters.
QgsPointXY geoPoint
Georeferenced coordinate of the control point, in CRS units.
ControlPoint(const QgsPointXY &pagePoint, const QgsPointXY &geoPoint)
Constructor for ControlPoint, at the specified pagePoint (in millimeters) and geoPoint (in CRS units)...
QMap< QString, QString > customLayerTreeGroups
Optional map of map layer ID to custom logical layer tree group in created PDF file.
QMap< QString, bool > initialLayerVisibility
Optional map of map layer ID to initial visibility state.
QList< QgsAbstractGeospatialPdfExporter::GeoReferencedSection > georeferencedSections
List of georeferenced sections.
QStringList layerOrder
Optional list of layer IDs, in the order desired to appear in the generated Geospatial PDF file.
QMap< QString, QString > layerIdToPdfLayerTreeNameMap
Optional map of map layer ID to custom layer tree name to show in the created PDF file.
QgsAbstractMetadataBase::KeywordMap keywords
Metadata keyword map.
bool useOgcBestPracticeFormatGeoreferencing
true if OGC "best practice" format georeferencing should be used.
QStringList layerTreeGroupOrder
Specifies the ordering of layer tree groups in the generated Geospatial PDF file.
bool useLayerTreeConfig
If set to true, the layer tree from the QGIS project should be used when creating a Geospatial PDF.
QDateTime creationDateTime
Metadata creation datetime.
bool includeFeatures
true if feature vector information (such as attributes) should be exported.
bool useIso32000ExtensionFormatGeoreferencing
true if ISO32000 extension format georeferencing should be used.
QSet< QString > mutuallyExclusiveGroups
Contains a list of group names which should be considered as mutually exclusive.
QgsRectangle pageBoundsMm
Bounds of the georeferenced section on the page, in millimeters.
QgsCoordinateReferenceSystem crs
Coordinate reference system for georeferenced section.
QList< QgsAbstractGeospatialPdfExporter::ControlPoint > controlPoints
List of control points corresponding to this georeferenced section.
QgsPolygon pageBoundsPolygon
Bounds of the georeferenced section on the page, in millimeters, as a free-form polygon.
Contains information about a feature rendered inside the PDF.
QgsGeometry renderedBounds
Bounds, in PDF units, of rendered feature.
RenderedFeature(const QgsFeature &feature, const QgsGeometry &renderedBounds)
Constructor for RenderedFeature.
Contains information relating to a single PDF layer in the Geospatial PDF export.
QString name
User-friendly name for the generated PDF layer.
QString sourceVectorPath
File path to the (already created) vector dataset to use as the source for this component layer.
QString sourceVectorLayer
Layer name in vector dataset to use as the source.