17#include "moc_qgsmaphittest.cpp"
45 : mSettings( settings )
56 tmpImage.setDotsPerMeterX(
static_cast< int >( std::round( mapSettings.
outputDpi() * 25.4 ) ) );
57 tmpImage.setDotsPerMeterY(
static_cast< int >( std::round( mapSettings.
outputDpi() * 25.4 ) ) );
58 QPainter painter( &tmpImage );
63 const QList< QgsMapLayer * > layers = mSettings.
layers();
85 SymbolSet &usedSymbols = mHitTest[vl->
id()];
86 SymbolSet &usedSymbolsRuleKey = mHitTestRuleKey[vl->
id()];
92 std::unique_ptr< QgsVectorLayerFeatureSource > source = std::make_unique< QgsVectorLayerFeatureSource >( vl );
93 runHitTestFeatureSource( source.get(),
95 usedSymbols, usedSymbolsRuleKey, context,
104 return mHitTestRuleKey;
108QMap<QString, QList<QString> > QgsMapHitTest::resultsPy()
const
110 QMap<QString, QList<QString> > res;
111 for (
auto it = mHitTestRuleKey.begin(); it != mHitTestRuleKey.end(); ++it )
113 res.insert( it.key(), qgis::setToList( it.value() ) );
121 if ( !symbol || !layer )
124 auto it = mHitTest.constFind( layer->
id() );
125 if ( it == mHitTest.constEnd() )
136 auto it = mHitTestRuleKey.constFind( layer->
id() );
137 if ( it == mHitTestRuleKey.constEnd() )
140 return it->contains( ruleKey );
144 const QString &layerId,
147 SymbolSet &usedSymbols,
148 SymbolSet &usedSymbolsRuleKey,
153 std::unique_ptr< QgsFeatureRenderer > r( renderer->
clone() );
155 r->startRender( context, fields );
158 if ( r->canSkipRender() )
160 r->stopRender( context );
165 QSet< QString > remainingKeysToFind = r->legendKeys();
166 if ( remainingKeysToFind.empty() )
168 r->stopRender( context );
176 const QString rendererFilterExpression = r->filter( fields );
177 if ( !rendererFilterExpression.isEmpty() )
184 QSet<QString> requiredAttributes = r->usedAttributes( context );
194 r->stopRender( context );
199 if (
auto it = layerFilterExpressions.constFind( layerId ); it != layerFilterExpressions.constEnd() )
201 const QString expression = *it;
205 requiredAttributes.unite( expr.referencedColumns() );
211 std::unique_ptr< QgsGeometryEngine > polygonEngine;
214 if ( transformedPolygon.
isNull() )
223 polygonEngine->prepareGeometry();
229 r->stopRender( context );
236 usedSymbolsRuleKey.clear();
257 const QSet< QString > legendKeysForFeature = r->legendKeysForFeature( f, context );
258 for (
const QString &legendKey : legendKeysForFeature )
260 usedSymbolsRuleKey.insert( legendKey );
261 remainingKeysToFind.remove( legendKey );
264 if ( moreSymbolsPerFeature )
266 const QgsSymbolList originalSymbolsForFeature = r->originalSymbolsForFeature( f, context );
267 for (
QgsSymbol *s : originalSymbolsForFeature )
275 QgsSymbol *s = r->originalSymbolForFeature( f, context );
280 if ( remainingKeysToFind.empty() )
296 , mSettings( settings )
307QMap<QString, QList<QString> > QgsMapHitTestTask::resultsPy()
const
309 QMap<QString, QList<QString> > res;
310 for (
auto it = mResults.begin(); it != mResults.end(); ++it )
312 res.insert( it.key(), qgis::setToList( it.value() ) );
318void QgsMapHitTestTask::prepare()
322 const QList< QgsMapLayer * > layers = mSettings.
layers();
344 PreparedLayerData layerData;
345 layerData.source = std::make_unique< QgsVectorLayerFeatureSource >( vl );
346 layerData.layerId = vl->
id();
347 layerData.fields = vl->
fields();
350 layerData.extent = extent;
353 mPreparedData.emplace_back( std::move( layerData ) );
367 mFeedback = std::make_unique< QgsFeedback >();
370 std::unique_ptr< QgsMapHitTest > hitTest = std::make_unique< QgsMapHitTest >( mSettings );
376 tmpImage.setDotsPerMeterX(
static_cast< int >( std::round( mapSettings.
outputDpi() * 25.4 ) ) );
377 tmpImage.setDotsPerMeterY(
static_cast< int >( std::round( mapSettings.
outputDpi() * 25.4 ) ) );
378 QPainter painter( &tmpImage );
384 std::size_t layerIdx = 0;
385 const std::size_t totalCount = mPreparedData.size();
386 for (
auto &layerData : mPreparedData )
388 mFeedback->setProgress(
static_cast< double >( layerIdx ) /
static_cast< double >( totalCount ) * 100.0 );
389 if ( mFeedback->isCanceled() )
392 QgsMapHitTest::SymbolSet &usedSymbols = hitTest->mHitTest[layerData.layerId];
393 QgsMapHitTest::SymbolSet &usedSymbolsRuleKey = hitTest->mHitTestRuleKey[layerData.layerId];
396 context.
setExtent( layerData.extent.boundingBox() );
401 hitTest->runHitTestFeatureSource( layerData.source.get(),
404 layerData.renderer.get(),
413 mResults = hitTest->mHitTestRuleKey;
@ SkipVisibilityCheck
If set, the standard visibility check should be skipped.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
Base class that can be used for any class that is capable of returning features.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Abstract base class for all 2D vector feature renderers.
@ MoreSymbolsPerFeature
May use more than one symbol to render a feature: symbolsForFeature() will return them.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the iterator to check if it should be cance...
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
Contains settings relating to filtering the contents of QgsLayerTreeModel and views.
QgsMapSettings & mapSettings()
Returns the map settings used to filter the legend content.
QList< QgsMapLayer * > layers() const
Returns the layers which should be shown in the legend.
Qgis::LayerTreeFilterFlags flags() const
Returns the filter flags.
QMap< QString, QString > layerFilterExpressions() const
Returns the map of layer IDs to legend filter expression.
void setFilterPolygon(const QgsGeometry &polygon)
Sets the optional filter polygon, used when testing for symbols to show in the legend.
void setFlags(Qgis::LayerTreeFilterFlags flags)
Sets the filter flags.
void setLayerFilterExpressions(const QMap< QString, QString > &expressions)
Sets the map of layer IDs to legend filter expression.
QgsGeometry combinedVisibleExtentForLayer(const QgsMapLayer *layer)
Returns the combined visible extent for a layer.
QMap< QString, QSet< QString > > results() const
Returns the hit test results, which are a map of layer ID to visible symbol legend keys.
QgsMapHitTestTask(const QgsLayerTreeFilterSettings &settings)
Constructor for QgsMapHitTestTask, using the specified filter settings.
bool run() override
Performs the task's operation.
PRIVATE void cancel() override
Notifies the task that it should terminate.
void run()
Runs the map hit test.
PRIVATE bool symbolVisible(QgsSymbol *symbol, QgsVectorLayer *layer) const
Tests whether a symbol is visible for a specified layer.
bool legendKeyVisible(const QString &ruleKey, QgsVectorLayer *layer) const
Tests whether a given legend key is visible for a specified layer.
QgsMapHitTest(const QgsMapSettings &settings, const QgsGeometry &polygon=QgsGeometry(), const QgsMapHitTest::LayerFilterExpression &layerFilterExpression=QgsMapHitTest::LayerFilterExpression())
QMap< QString, QSet< QString > > results() const
Returns the hit test results, which are a map of layer ID to visible symbol legend keys.
QMap< QString, QString > LayerFilterExpression
Maps an expression string to a layer id.
Restore overridden layer style on destruction.
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
Base class for all map layer types.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
The QgsMapSettings class contains configuration for rendering of the map.
double scale() const
Returns the calculated map scale.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
QMap< QString, QString > layerStyleOverrides() const
Returns the map of map layer style overrides (key: layer ID, value: style name) where a different sty...
Contains information about the context of a rendering operation.
void setCoordinateTransform(const QgsCoordinateTransform &t)
Sets the current coordinate transform for the context.
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
static QString symbolProperties(QgsSymbol *symbol)
Returns a string representing the symbol.
Abstract base class for all rendered symbols.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Abstract base class for long running background tasks.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
virtual void cancel()
Notifies the task that it should terminate.
Represents a vector layer which manages a vector based data sets.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QList< QgsSymbol * > QgsSymbolList