QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsrendereditemresults.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrendereditemresults.cpp
3 -------------------
4 begin : August 2021
5 copyright : (C) Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "RTree.h"
19#include "qgslogger.h"
20#include "qgsrendercontext.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29class QgsRenderedItemResultsSpatialIndex : public RTree<const QgsRenderedItemDetails *, float, 2, float>
30{
31 public:
32 explicit QgsRenderedItemResultsSpatialIndex( const QgsRectangle &maxBounds )
33 : mXMin( maxBounds.xMinimum() )
34 , mYMin( maxBounds.yMinimum() )
35 , mXRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.xMaximum() - maxBounds.xMinimum() ) )
36 , mYRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.yMaximum() - maxBounds.yMinimum() ) )
37 , mMaxBounds( maxBounds )
38 , mUseScale( !maxBounds.isNull() )
39 {}
40
41 void insert( const QgsRenderedItemDetails *details, const QgsRectangle &bounds )
42 {
43 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
44 const float aMin[2] { scaledBounds[0], scaledBounds[1] };
45 const float aMax[2] { scaledBounds[2], scaledBounds[3] };
46 this->Insert( aMin, aMax, details );
47 }
48
54 bool intersects( const QgsRectangle &bounds, const std::function< bool( const QgsRenderedItemDetails *details )> &callback ) const
55 {
56 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
57 const float aMin[2] { scaledBounds[0], scaledBounds[1] };
58 const float aMax[2] { scaledBounds[2], scaledBounds[3] };
59 this->Search( aMin, aMax, callback );
60 return true;
61 }
62
63 private:
64 double mXMin = 0;
65 double mYMin = 0;
66 double mXRes = 1;
67 double mYRes = 1;
68 QgsRectangle mMaxBounds;
69 bool mUseScale = false;
70
71 std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
72 {
73 if ( mUseScale )
74 return {
75 static_cast< float >( ( std::max( bounds.xMinimum(), mMaxBounds.xMinimum() ) - mXMin ) / mXRes ),
76 static_cast< float >( ( std::max( bounds.yMinimum(), mMaxBounds.yMinimum() ) - mYMin ) / mYRes ),
77 static_cast< float >( ( std::min( bounds.xMaximum(), mMaxBounds.xMaximum() ) - mXMin ) / mXRes ),
78 static_cast< float >( ( std::min( bounds.yMaximum(), mMaxBounds.yMaximum() ) - mYMin ) / mYRes )
79 };
80 else
81 return { static_cast< float >( bounds.xMinimum() ), static_cast< float >( bounds.yMinimum() ), static_cast< float >( bounds.xMaximum() ), static_cast< float >( bounds.yMaximum() ) };
82 }
83};
85
87 : mExtent( extent.buffered( std::max( extent.width(), extent.height() ) * 1000 ) ) // RTree goes crazy if we insert geometries outside the bounds, so buffer them right out to be safe
88 , mAnnotationItemsIndex( std::make_unique< QgsRenderedItemResultsSpatialIndex >( mExtent ) )
89{}
90
92
93QList<QgsRenderedItemDetails *> QgsRenderedItemResults::renderedItems() const
94{
95 QList< QgsRenderedItemDetails * > res;
96 for ( const auto &it : mDetails )
97 {
98 std::transform( it.second.begin(), it.second.end(), std::back_inserter( res ), []( const auto &detail ) { return detail.get(); } );
99 }
100 return res;
101}
102
103QList<const QgsRenderedAnnotationItemDetails *> QgsRenderedItemResults::renderedAnnotationItemsInBounds( const QgsRectangle &bounds ) const
104{
105 QList<const QgsRenderedAnnotationItemDetails *> res;
106
107 mAnnotationItemsIndex->intersects( bounds, [&res]( const QgsRenderedItemDetails *details ) -> bool {
108 res << qgis::down_cast< const QgsRenderedAnnotationItemDetails * >( details );
109 return true;
110 } );
111 return res;
112}
113
114void QgsRenderedItemResults::appendResults( const QList<QgsRenderedItemDetails *> &results, const QgsRenderContext &context )
115{
116 QgsCoordinateTransform layerToMapTransform = context.coordinateTransform();
117 layerToMapTransform.setBallparkTransformsAreAppropriate( true );
118 for ( QgsRenderedItemDetails *details : results )
119 {
120 try
121 {
122 const QgsRectangle transformedBounds = layerToMapTransform.transformBoundingBox( details->boundingBox() );
123 details->setBoundingBox( transformedBounds );
124 }
125 catch ( QgsCsException & )
126 {
127 QgsDebugError( u"Could not transform rendered item's bounds to map CRS"_s );
128 }
129
130 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( details ) )
131 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
132
133
134 mDetails[details->layerId()].emplace_back( std::unique_ptr< QgsRenderedItemDetails >( details ) );
135 }
136}
137
138void QgsRenderedItemResults::transferResults( QgsRenderedItemResults *other, const QStringList &layerIds )
139{
140 for ( const QString &layerId : layerIds )
141 {
142 auto otherLayerIt = other->mDetails.find( layerId );
143 if ( otherLayerIt == other->mDetails.end() )
144 continue;
145
146 std::vector< std::unique_ptr< QgsRenderedItemDetails > > &source = otherLayerIt->second;
147
148 for ( std::unique_ptr< QgsRenderedItemDetails > &details : source )
149 {
150 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( details.get() ) )
151 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
152
153 mDetails[layerId].emplace_back( std::move( details ) );
154 }
155
156 other->mDetails.erase( otherLayerIt );
157 }
158}
159
161{
162 for ( auto layerIt = other->mDetails.begin(); layerIt != other->mDetails.end(); ++layerIt )
163 {
164 std::vector< std::unique_ptr< QgsRenderedItemDetails > > &dest = mDetails[layerIt->first];
165 dest.reserve( layerIt->second.size() );
166 for ( auto it = layerIt->second.begin(); it != layerIt->second.end(); ++it )
167 {
168 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( ( *it ).get() ) )
169 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
170
171 dest.emplace_back( std::move( *it ) );
172 }
173 }
174 other->mDetails.clear();
175}
176
177void QgsRenderedItemResults::eraseResultsFromLayers( const QStringList &layerIds )
178{
179 for ( const QString &layerId : layerIds )
180 {
181 auto it = mDetails.find( layerId );
182 if ( it != mDetails.end() )
183 mDetails.erase( it );
184 }
185}
Handles coordinate transforms between two coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
Contains information about the context of a rendering operation.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Contains information about a rendered annotation item.
Base class for detailed information about a rendered item.
void transferResults(QgsRenderedItemResults *other, const QStringList &layerIds)
Transfers all results from an other QgsRenderedItemResults object where the items have layer IDs matc...
QList< const QgsRenderedAnnotationItemDetails * > renderedAnnotationItemsInBounds(const QgsRectangle &bounds) const
Returns a list with details of the rendered annotation items within the specified bounds.
QgsRenderedItemResults(const QgsRectangle &extent=QgsRectangle())
Constructor for QgsRenderedItemResults.
QList< QgsRenderedItemDetails * > renderedItems() const
Returns a list of all rendered items.
void eraseResultsFromLayers(const QStringList &layerIds)
Erases results from layers matching those in the specified list of layers IDs.
void appendResults(const QList< QgsRenderedItemDetails * > &results, const QgsRenderContext &context)
Appends rendered item details to the results object.
#define QgsDebugError(str)
Definition qgslogger.h:59