26using namespace Qt::StringLiterals;
30void QgsLocationBasedAlgorithm::addPredicateParameter()
32 auto predicateParam = std::make_unique<QgsProcessingParameterEnum>( u
"PREDICATE"_s, QObject::tr(
"Where the features (geometric predicate)" ), predicateOptionsList(),
true, QVariant::fromValue( QList<int>() << 0 ) );
34 QVariantMap predicateMetadata;
35 QVariantMap widgetMetadata;
36 widgetMetadata.insert( u
"useCheckBoxes"_s,
true );
37 widgetMetadata.insert( u
"columns"_s, 2 );
38 predicateMetadata.insert( u
"widget_wrapper"_s, widgetMetadata );
39 predicateParam->setMetadata( predicateMetadata );
41 addParameter( predicateParam.release() );
44QgsLocationBasedAlgorithm::Predicate QgsLocationBasedAlgorithm::reversePredicate( QgsLocationBasedAlgorithm::Predicate predicate )
const
69QStringList QgsLocationBasedAlgorithm::predicateOptionsList()
const
71 return QStringList() << QObject::tr(
"intersect" )
72 << QObject::tr(
"contain" )
73 << QObject::tr(
"disjoint" )
74 << QObject::tr(
"equal" )
75 << QObject::tr(
"touch" )
76 << QObject::tr(
"overlap" )
77 << QObject::tr(
"are within" )
78 << QObject::tr(
"cross" );
84 if ( mTargetFeatureCount == 0 )
88 if ( mIntersectFeatureCount == 0 && !selectedPredicates.contains( Disjoint ) )
91 if ( mTargetFeatureCount > 0 && mIntersectFeatureCount > 0 && mTargetFeatureCount < mIntersectFeatureCount )
94 processByIteratingOverTargetSource( context, targetSource, intersectSource, selectedPredicates, handleFeatureFunction, onlyRequireTargetIds, feedback, skipTargetFeatureIds );
104 processByIteratingOverIntersectSource( context, targetSource, intersectSource, selectedPredicates, handleFeatureFunction, onlyRequireTargetIds, feedback, skipTargetFeatureIds );
111 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
115 if ( onlyRequireTargetIds )
119 double step = mTargetFeatureCount > 0 ? 100.0 /
static_cast<double>( mTargetFeatureCount ) : 1;
122 std::unique_ptr<QgsGeometryEngine> engine;
129 if ( skipTargetFeatureIds.contains( f.
id() ) )
141 bool isMatch =
false;
142 bool isDisjoint =
true;
151 engine->prepareGeometry();
154 for (
int predicate : selectedPredicates )
156 switch (
static_cast<Predicate
>( predicate ) )
193 foundSet.insert( f.
id() );
194 handleFeatureFunction( f );
198 if ( isDisjoint && selectedPredicates.contains( Disjoint ) )
200 foundSet.insert( f.
id() );
201 handleFeatureFunction( f );
212 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
217 QList<Predicate> predicates;
218 predicates.reserve( selectedPredicates.count() );
219 for (
int i : selectedPredicates )
221 predicates << reversePredicate( static_cast<Predicate>( i ) );
225 if ( predicates.contains( Disjoint ) )
231 double step = mIntersectFeatureCount > 0 ? 100.0 /
static_cast<double>( mIntersectFeatureCount ) : 1;
234 std::unique_ptr<QgsGeometryEngine> engine;
247 if ( onlyRequireTargetIds )
257 if ( skipTargetFeatureIds.contains( testFeature.
id() ) )
262 if ( foundSet.contains( testFeature.
id() ) )
267 if ( predicates.count() == 1 && predicates.at( 0 ) == Disjoint && !disjointSet.contains( testFeature.
id() ) )
276 engine->prepareGeometry();
279 bool isMatch =
false;
281 for ( Predicate predicate : std::as_const( predicates ) )
294 disjointSet.remove( testFeature.
id() );
319 foundSet.insert( testFeature.
id() );
320 handleFeatureFunction( testFeature );
328 if ( predicates.contains( Disjoint ) )
330 disjointSet = disjointSet.subtract( foundSet );
332 if ( onlyRequireTargetIds )
338 handleFeatureFunction( f );
348void QgsSelectByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
350 QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
351 << QObject::tr(
"adding to current selection" )
352 << QObject::tr(
"selecting within current selection" )
353 << QObject::tr(
"removing from current selection" );
356 addPredicateParameter();
359 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Modify current selection by" ), methods,
false, 0 ) );
362QString QgsSelectByLocationAlgorithm::name()
const
364 return u
"selectbylocation"_s;
372QString QgsSelectByLocationAlgorithm::displayName()
const
374 return QObject::tr(
"Select by location" );
377QStringList QgsSelectByLocationAlgorithm::tags()
const
379 return QObject::tr(
"select,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
382QString QgsSelectByLocationAlgorithm::group()
const
384 return QObject::tr(
"Vector selection" );
387QString QgsSelectByLocationAlgorithm::groupId()
const
389 return u
"vectorselection"_s;
392QString QgsSelectByLocationAlgorithm::shortHelpString()
const
394 return QObject::tr(
"This algorithm creates a selection in a vector layer. The criteria for selecting "
395 "features is based on the spatial relationship between each feature and the features in an additional layer." );
398QString QgsSelectByLocationAlgorithm::shortDescription()
const
400 return QObject::tr(
"Creates a selection in a vector layer based on the spatial relationship with features in an additional layer." );
403QgsSelectByLocationAlgorithm *QgsSelectByLocationAlgorithm::createInstance()
const
405 return new QgsSelectByLocationAlgorithm();
410 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
415 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, u
"INTERSECT"_s, context ) );
416 if ( !intersectSource )
419 const QList<int> selectedPredicates = parameterAsEnums( parameters, u
"PREDICATE"_s, context );
422 auto addToSelection = [&](
const QgsFeature &feature ) {
423 selectedIds.insert( feature.id() );
428 mIntersectFeatureCount = intersectSource->
featureCount();
436 auto selectLayerSelected = std::make_unique<QgsVectorLayerSelectedFeatureSource>( selectLayer );
437 mTargetCrs = selectLayerSelected->sourceCrs();
438 mTargetFeatureCount = selectLayerSelected->featureCount();
439 process( context, selectLayerSelected.get(), intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
444 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback, selectLayer->
selectedFeatureIds() );
447 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
453 results.insert( u
"OUTPUT"_s, parameters.value( u
"INPUT"_s ) );
462void QgsExtractByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
465 addPredicateParameter();
471QString QgsExtractByLocationAlgorithm::name()
const
473 return u
"extractbylocation"_s;
476QString QgsExtractByLocationAlgorithm::displayName()
const
478 return QObject::tr(
"Extract by location" );
481QStringList QgsExtractByLocationAlgorithm::tags()
const
483 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
486QString QgsExtractByLocationAlgorithm::group()
const
488 return QObject::tr(
"Vector selection" );
491QString QgsExtractByLocationAlgorithm::groupId()
const
493 return u
"vectorselection"_s;
496QString QgsExtractByLocationAlgorithm::shortHelpString()
const
498 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
499 "input layer. The criteria for adding features to the resulting layer is defined "
500 "based on the spatial relationship between each feature and the features in an additional layer." );
503QString QgsExtractByLocationAlgorithm::shortDescription()
const
505 return QObject::tr(
"Creates a vector layer that only contains features matching a spatial relationship with the features in an additional layer." );
508QgsExtractByLocationAlgorithm *QgsExtractByLocationAlgorithm::createInstance()
const
510 return new QgsExtractByLocationAlgorithm();
515 std::unique_ptr<QgsFeatureSource> input( parameterAsSource( parameters, u
"INPUT"_s, context ) );
518 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, u
"INTERSECT"_s, context ) );
519 if ( !intersectSource )
522 mTargetCrs = input->sourceCrs();
523 mTargetFeatureCount = input->featureCount();
524 mIntersectFeatureCount = intersectSource->
featureCount();
531 std::unique_ptr<QgsFeatureSource> input( parameterAsSource( parameters, u
"INPUT"_s, context ) );
534 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, u
"INTERSECT"_s, context ) );
535 if ( !intersectSource )
538 const QList<int> selectedPredicates = parameterAsEnums( parameters, u
"PREDICATE"_s, context );
540 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, input->fields(), input->wkbType(), mTargetCrs ) );
545 auto addToSink = [&](
const QgsFeature &feature ) {
550 process( context, input.get(), intersectSource.get(), selectedPredicates, addToSink,
false, feedback );
555 results.insert( u
"OUTPUT"_s, dest );
@ VectorAnyGeometry
Any vector layer with geometry.
@ NotPresent
No spatial index exists for the source.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ NotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
SelectBehavior
Specifies how a selection should be applied.
@ SetSelection
Set selection, removing any existing selection.
@ AddToSelection
Add selection to current selection.
@ IntersectSelection
Modify current selection to include only select features which match.
@ RemoveFromSelection
Remove from current selection.
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.
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 & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
An interface for objects which provide features via a getFeatures method.
virtual Qgis::SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source,...
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
virtual long long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
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...
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
A rectangle specified with double values.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
QgsCoordinateReferenceSystem sourceCrs() const final
Returns the coordinate reference system for features in the source.
QSet< QgsFeatureId > QgsFeatureIds