Skip to content

Commit

Permalink
Add method to collate all collected features from different maps but …
Browse files Browse the repository at this point in the history
…the same layer
  • Loading branch information
nyalldawson committed Aug 17, 2019
1 parent 2dbd526 commit afca6ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -546,6 +546,9 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( const QString &f
ExportResult result = printPrivate( printer, p, false, settings.dpi, settings.rasterizeWholeImage );
p.end();

if ( geoPdfExporter )
geoPdfExporter->finalize();

bool shouldAppendGeoreference = settings.appendGeoreference && mLayout && mLayout->referenceMap() && mLayout->referenceMap()->page() == 0;
if ( settings.appendGeoreference || settings.exportMetadata )
{
Expand Down
21 changes: 21 additions & 0 deletions src/core/layout/qgslayoutgeopdfexporter.cpp
Expand Up @@ -102,3 +102,24 @@ QMap<QString, QVector<QgsLayoutGeoPdfExporter::RenderedFeature> > QgsLayoutGeoPd
return mMapHandlers.value( map )->renderedFeatures;
}

void QgsLayoutGeoPdfExporter::finalize()
{
// collate all the features from different maps which belong to the same layer, replace their geometries with the rendered feature bounds
for ( auto mapIt = mMapHandlers.constBegin(); mapIt != mMapHandlers.constEnd(); ++mapIt )
{
QgsGeoPdfRenderedFeatureHandler *handler = mapIt.value();
for ( auto layerIt = handler->renderedFeatures.constBegin(); layerIt != handler->renderedFeatures.constEnd(); ++layerIt )
{
const QString layerId = layerIt.key();
const QVector< QgsLayoutGeoPdfExporter::RenderedFeature > features = layerIt.value();
for ( auto featureIt = features.constBegin(); featureIt != features.constEnd(); ++featureIt )
{
// replace feature geometry with transformed rendered bounds
QgsFeature f = featureIt->feature;
f.setGeometry( featureIt->renderedBounds );
mCollatedFeatures[ layerId ].append( f );
}
}
}
}

8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutgeopdfexporter.h
Expand Up @@ -86,11 +86,19 @@ class CORE_EXPORT QgsLayoutGeoPdfExporter
*/
QMap< QString, QVector< QgsLayoutGeoPdfExporter::RenderedFeature > > renderedFeatures( QgsLayoutItemMap *map ) const;

/**
* To be called after the rendering operation is complete.
*/
void finalize();

private:

QgsLayout *mLayout = nullptr;
QHash< QgsLayoutItemMap *, QgsGeoPdfRenderedFeatureHandler * > mMapHandlers;

QMap< QString, QgsFeatureList > mCollatedFeatures;

friend class TestQgsLayoutGeoPdfExport;
};

#endif //QGSLAYOUTGEOPDFEXPORTER_H
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgslayoutgeopdfexport.cpp
Expand Up @@ -179,6 +179,14 @@ void TestQgsLayoutGeoPdfExport::testCollectingFeatures()
QCOMPARE( pointFeature3b.attribute( 4 ).toInt(), 1 );
QCOMPARE( pointFeature3b.attribute( 5 ).toInt(), 2 );
QCOMPARE( pointGeometry3b.asWkt( 1 ), QStringLiteral( "Polygon ((167 102, 178.2 102, 178.2 113.3, 167 113.3, 167 102))" ) );

// finalize and test collation
geoPdfExporter.finalize();

QMap< QString, QgsFeatureList > collatedFeatures = geoPdfExporter.mCollatedFeatures;
QCOMPARE( collatedFeatures.count(), 2 );
QCOMPARE( collatedFeatures.value( pointsLayer->id() ).count(), 32 );
QCOMPARE( collatedFeatures.value( linesLayer->id() ).count(), 6 );
}

QGSTEST_MAIN( TestQgsLayoutGeoPdfExport )
Expand Down

0 comments on commit afca6ef

Please sign in to comment.