26void QgsLocationBasedAlgorithm::addPredicateParameter()
28 auto predicateParam = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"PREDICATE" ), QObject::tr(
"Where the features (geometric predicate)" ), predicateOptionsList(),
true, QVariant::fromValue( QList<int>() << 0 ) );
30 QVariantMap predicateMetadata;
31 QVariantMap widgetMetadata;
32 widgetMetadata.insert( QStringLiteral(
"useCheckBoxes" ),
true );
33 widgetMetadata.insert( QStringLiteral(
"columns" ), 2 );
34 predicateMetadata.insert( QStringLiteral(
"widget_wrapper" ), widgetMetadata );
35 predicateParam->setMetadata( predicateMetadata );
37 addParameter( predicateParam.release() );
40QgsLocationBasedAlgorithm::Predicate QgsLocationBasedAlgorithm::reversePredicate( QgsLocationBasedAlgorithm::Predicate predicate )
const
65QStringList QgsLocationBasedAlgorithm::predicateOptionsList()
const
67 return QStringList() << QObject::tr(
"intersect" )
68 << QObject::tr(
"contain" )
69 << QObject::tr(
"disjoint" )
70 << QObject::tr(
"equal" )
71 << QObject::tr(
"touch" )
72 << QObject::tr(
"overlap" )
73 << QObject::tr(
"are within" )
74 << QObject::tr(
"cross" );
80 if ( mTargetFeatureCount == 0 )
84 if ( mIntersectFeatureCount == 0 && !selectedPredicates.contains( Disjoint ) )
87 if ( mTargetFeatureCount > 0 && mIntersectFeatureCount > 0 && mTargetFeatureCount < mIntersectFeatureCount )
90 processByIteratingOverTargetSource( context, targetSource, intersectSource, selectedPredicates, handleFeatureFunction, onlyRequireTargetIds, feedback, skipTargetFeatureIds );
100 processByIteratingOverIntersectSource( context, targetSource, intersectSource, selectedPredicates, handleFeatureFunction, onlyRequireTargetIds, feedback, skipTargetFeatureIds );
107 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
111 if ( onlyRequireTargetIds )
115 double step = mTargetFeatureCount > 0 ? 100.0 /
static_cast<double>( mTargetFeatureCount ) : 1;
118 std::unique_ptr<QgsGeometryEngine> engine;
125 if ( skipTargetFeatureIds.contains( f.
id() ) )
137 bool isMatch =
false;
138 bool isDisjoint =
true;
147 engine->prepareGeometry();
150 for (
int predicate : selectedPredicates )
152 switch (
static_cast<Predicate
>( predicate ) )
189 foundSet.insert( f.
id() );
190 handleFeatureFunction( f );
194 if ( isDisjoint && selectedPredicates.contains( Disjoint ) )
196 foundSet.insert( f.
id() );
197 handleFeatureFunction( f );
208 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
213 QList<Predicate> predicates;
214 predicates.reserve( selectedPredicates.count() );
215 for (
int i : selectedPredicates )
217 predicates << reversePredicate( static_cast<Predicate>( i ) );
221 if ( predicates.contains( Disjoint ) )
227 double step = mIntersectFeatureCount > 0 ? 100.0 /
static_cast<double>( mIntersectFeatureCount ) : 1;
230 std::unique_ptr<QgsGeometryEngine> engine;
243 if ( onlyRequireTargetIds )
253 if ( skipTargetFeatureIds.contains( testFeature.
id() ) )
258 if ( foundSet.contains( testFeature.
id() ) )
263 if ( predicates.count() == 1 && predicates.at( 0 ) == Disjoint && !disjointSet.contains( testFeature.
id() ) )
272 engine->prepareGeometry();
275 bool isMatch =
false;
277 for ( Predicate predicate : std::as_const( predicates ) )
290 disjointSet.remove( testFeature.
id() );
315 foundSet.insert( testFeature.
id() );
316 handleFeatureFunction( testFeature );
324 if ( predicates.contains( Disjoint ) )
326 disjointSet = disjointSet.subtract( foundSet );
328 if ( onlyRequireTargetIds )
334 handleFeatureFunction( f );
344void QgsSelectByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
346 QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
347 << QObject::tr(
"adding to current selection" )
348 << QObject::tr(
"selecting within current selection" )
349 << QObject::tr(
"removing from current selection" );
352 addPredicateParameter();
355 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Modify current selection by" ), methods,
false, 0 ) );
358QString QgsSelectByLocationAlgorithm::name()
const
360 return QStringLiteral(
"selectbylocation" );
368QString QgsSelectByLocationAlgorithm::displayName()
const
370 return QObject::tr(
"Select by location" );
373QStringList QgsSelectByLocationAlgorithm::tags()
const
375 return QObject::tr(
"select,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
378QString QgsSelectByLocationAlgorithm::group()
const
380 return QObject::tr(
"Vector selection" );
383QString QgsSelectByLocationAlgorithm::groupId()
const
385 return QStringLiteral(
"vectorselection" );
388QString QgsSelectByLocationAlgorithm::shortHelpString()
const
390 return QObject::tr(
"This algorithm creates a selection in a vector layer. The criteria for selecting "
391 "features is based on the spatial relationship between each feature and the features in an additional layer." );
394QString QgsSelectByLocationAlgorithm::shortDescription()
const
396 return QObject::tr(
"Creates a selection in a vector layer based on the spatial relationship with features in an additional layer." );
399QgsSelectByLocationAlgorithm *QgsSelectByLocationAlgorithm::createInstance()
const
401 return new QgsSelectByLocationAlgorithm();
406 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT" ), context );
411 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
412 if ( !intersectSource )
415 const QList<int> selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
418 auto addToSelection = [&](
const QgsFeature &feature ) {
419 selectedIds.insert( feature.id() );
424 mIntersectFeatureCount = intersectSource->
featureCount();
432 auto selectLayerSelected = std::make_unique<QgsVectorLayerSelectedFeatureSource>( selectLayer );
433 mTargetCrs = selectLayerSelected->sourceCrs();
434 mTargetFeatureCount = selectLayerSelected->featureCount();
435 process( context, selectLayerSelected.get(), intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
440 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback, selectLayer->
selectedFeatureIds() );
443 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
449 results.insert( QStringLiteral(
"OUTPUT" ), parameters.value( QStringLiteral(
"INPUT" ) ) );
458void QgsExtractByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
461 addPredicateParameter();
467QString QgsExtractByLocationAlgorithm::name()
const
469 return QStringLiteral(
"extractbylocation" );
472QString QgsExtractByLocationAlgorithm::displayName()
const
474 return QObject::tr(
"Extract by location" );
477QStringList QgsExtractByLocationAlgorithm::tags()
const
479 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
482QString QgsExtractByLocationAlgorithm::group()
const
484 return QObject::tr(
"Vector selection" );
487QString QgsExtractByLocationAlgorithm::groupId()
const
489 return QStringLiteral(
"vectorselection" );
492QString QgsExtractByLocationAlgorithm::shortHelpString()
const
494 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
495 "input layer. The criteria for adding features to the resulting layer is defined "
496 "based on the spatial relationship between each feature and the features in an additional layer." );
499QString QgsExtractByLocationAlgorithm::shortDescription()
const
501 return QObject::tr(
"Creates a vector layer that only contains features matching a spatial relationship with the features in an additional layer." );
504QgsExtractByLocationAlgorithm *QgsExtractByLocationAlgorithm::createInstance()
const
506 return new QgsExtractByLocationAlgorithm();
511 std::unique_ptr<QgsFeatureSource> input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
514 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
515 if ( !intersectSource )
518 mTargetCrs = input->sourceCrs();
519 mTargetFeatureCount = input->featureCount();
520 mIntersectFeatureCount = intersectSource->
featureCount();
527 std::unique_ptr<QgsFeatureSource> input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
530 std::unique_ptr<QgsFeatureSource> intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
531 if ( !intersectSource )
534 const QList<int> selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
536 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, input->fields(), input->wkbType(), mTargetCrs ) );
541 auto addToSink = [&](
const QgsFeature &feature ) {
546 process( context, input.get(), intersectSource.get(), selectedPredicates, addToSink,
false, feedback );
551 results.insert( QStringLiteral(
"OUTPUT" ), 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