QGIS API Documentation 3.41.0-Master (1deb1daf037)
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
18#include "qgsrendercontext.h"
19#include "qgslogger.h"
21#include "RTree.h"
22
23
25class QgsRenderedItemResultsSpatialIndex : public RTree<const QgsRenderedItemDetails *, float, 2, float>
26{
27 public:
28
29 explicit QgsRenderedItemResultsSpatialIndex( const QgsRectangle &maxBounds )
30 : mXMin( maxBounds.xMinimum() )
31 , mYMin( maxBounds.yMinimum() )
32 , mXRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.xMaximum() - maxBounds.xMinimum() ) )
33 , mYRes( ( std::numeric_limits< float >::max() - 1 ) / ( maxBounds.yMaximum() - maxBounds.yMinimum() ) )
34 , mMaxBounds( maxBounds )
35 , mUseScale( !maxBounds.isNull() )
36 {}
37
38 void insert( const QgsRenderedItemDetails *details, const QgsRectangle &bounds )
39 {
40 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
41 const float aMin[2]
42 {
43 scaledBounds[0], scaledBounds[ 1]
44 };
45 const float aMax[2]
46 {
47 scaledBounds[2], scaledBounds[ 3]
48 };
49 this->Insert(
50 aMin,
51 aMax,
52 details );
53 }
54
60 bool intersects( const QgsRectangle &bounds, const std::function< bool( const QgsRenderedItemDetails *details )> &callback ) const
61 {
62 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
63 const float aMin[2]
64 {
65 scaledBounds[0], scaledBounds[ 1]
66 };
67 const float aMax[2]
68 {
69 scaledBounds[2], scaledBounds[ 3]
70 };
71 this->Search(
72 aMin,
73 aMax,
74 callback );
75 return true;
76 }
77
78 private:
79 double mXMin = 0;
80 double mYMin = 0;
81 double mXRes = 1;
82 double mYRes = 1;
83 QgsRectangle mMaxBounds;
84 bool mUseScale = false;
85
86 std::array<float, 4> scaleBounds( const QgsRectangle &bounds ) const
87 {
88 if ( mUseScale )
89 return
90 {
91 static_cast< float >( ( std::max( bounds.xMinimum(), mMaxBounds.xMinimum() ) - mXMin ) / mXRes ),
92 static_cast< float >( ( std::max( bounds.yMinimum(), mMaxBounds.yMinimum() ) - mYMin ) / mYRes ),
93 static_cast< float >( ( std::min( bounds.xMaximum(), mMaxBounds.xMaximum() ) - mXMin ) / mXRes ),
94 static_cast< float >( ( std::min( bounds.yMaximum(), mMaxBounds.yMaximum() ) - mYMin ) / mYRes )
95 };
96 else
97 return
98 {
99 static_cast< float >( bounds.xMinimum() ),
100 static_cast< float >( bounds.yMinimum() ),
101 static_cast< float >( bounds.xMaximum() ),
102 static_cast< float >( bounds.yMaximum() )
103 };
104 }
105};
107
109 : 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
110 , mAnnotationItemsIndex( std::make_unique< QgsRenderedItemResultsSpatialIndex >( mExtent ) )
111{
112
113}
114
116
117QList<QgsRenderedItemDetails *> QgsRenderedItemResults::renderedItems() const
118{
119 QList< QgsRenderedItemDetails * > res;
120 for ( const auto &it : mDetails )
121 {
122 std::transform( it.second.begin(), it.second.end(), std::back_inserter( res ), []( const auto & detail )
123 {
124 return detail.get();
125 } );
126 }
127 return res;
128}
129
130QList<const QgsRenderedAnnotationItemDetails *> QgsRenderedItemResults::renderedAnnotationItemsInBounds( const QgsRectangle &bounds ) const
131{
132 QList<const QgsRenderedAnnotationItemDetails *> res;
133
134 mAnnotationItemsIndex->intersects( bounds, [&res]( const QgsRenderedItemDetails * details )->bool
135 {
136 res << qgis::down_cast< const QgsRenderedAnnotationItemDetails * >( details );
137 return true;
138 } );
139 return res;
140}
141
142void QgsRenderedItemResults::appendResults( const QList<QgsRenderedItemDetails *> &results, const QgsRenderContext &context )
143{
144 QgsCoordinateTransform layerToMapTransform = context.coordinateTransform();
145 layerToMapTransform.setBallparkTransformsAreAppropriate( true );
146 for ( QgsRenderedItemDetails *details : results )
147 {
148 try
149 {
150 const QgsRectangle transformedBounds = layerToMapTransform.transformBoundingBox( details->boundingBox() );
151 details->setBoundingBox( transformedBounds );
152 }
153 catch ( QgsCsException & )
154 {
155 QgsDebugError( QStringLiteral( "Could not transform rendered item's bounds to map CRS" ) );
156 }
157
158 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( details ) )
159 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
160
161
162 mDetails[ details->layerId() ].emplace_back( std::unique_ptr< QgsRenderedItemDetails >( details ) );
163 }
164}
165
166void QgsRenderedItemResults::transferResults( QgsRenderedItemResults *other, const QStringList &layerIds )
167{
168 for ( const QString &layerId : layerIds )
169 {
170 auto otherLayerIt = other->mDetails.find( layerId );
171 if ( otherLayerIt == other->mDetails.end() )
172 continue;
173
174 std::vector< std::unique_ptr< QgsRenderedItemDetails > > &source = otherLayerIt->second;
175
176 for ( std::unique_ptr< QgsRenderedItemDetails > &details : source )
177 {
178 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( details.get() ) )
179 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
180
181 mDetails[layerId].emplace_back( std::move( details ) );
182 }
183
184 other->mDetails.erase( otherLayerIt );
185 }
186}
187
189{
190 for ( auto layerIt = other->mDetails.begin(); layerIt != other->mDetails.end(); ++layerIt )
191 {
192 std::vector< std::unique_ptr< QgsRenderedItemDetails > > &dest = mDetails[layerIt->first];
193 dest.reserve( layerIt->second.size() );
194 for ( auto it = layerIt->second.begin(); it != layerIt->second.end(); ++it )
195 {
196 if ( QgsRenderedAnnotationItemDetails *annotationDetails = dynamic_cast< QgsRenderedAnnotationItemDetails * >( ( *it ).get() ) )
197 mAnnotationItemsIndex->insert( annotationDetails, annotationDetails->boundingBox() );
198
199 dest.emplace_back( std::move( *it ) );
200 }
201 }
202 other->mDetails.clear();
203}
204
205void QgsRenderedItemResults::eraseResultsFromLayers( const QStringList &layerIds )
206{
207 for ( const QString &layerId : layerIds )
208 {
209 auto it = mDetails.find( layerId );
210 if ( it != mDetails.end() )
211 mDetails.erase( it );
212 }
213}
214
215
Class for doing transforms between two map 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.
Stores collated details of rendered items during a map rendering operation.
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:40