24 void QgsLocationBasedAlgorithm::addPredicateParameter()
27 QObject::tr(
"Where the features (geometric predicate)" ),
28 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() );
40 QgsLocationBasedAlgorithm::Predicate QgsLocationBasedAlgorithm::reversePredicate( QgsLocationBasedAlgorithm::Predicate predicate )
const
65 QStringList 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" );
79 const QList< int > &selectedPredicates,
80 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
81 bool onlyRequireTargetIds,
89 processByIteratingOverTargetSource( context, targetSource, intersectSource,
90 selectedPredicates, handleFeatureFunction,
91 onlyRequireTargetIds, feedback );
101 processByIteratingOverIntersectSource( context, targetSource, intersectSource,
102 selectedPredicates, handleFeatureFunction,
103 onlyRequireTargetIds, feedback );
109 const QList< int > &selectedPredicates,
110 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
111 bool onlyRequireTargetIds,
115 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
119 if ( onlyRequireTargetIds )
126 std::unique_ptr< QgsGeometryEngine > engine;
142 bool isMatch =
false;
143 bool isDisjoint =
true;
152 engine->prepareGeometry();
155 for (
int predicate : selectedPredicates )
157 switch (
static_cast< Predicate
>( predicate ) )
194 foundSet.insert( f.
id() );
195 handleFeatureFunction( f );
199 if ( isDisjoint && selectedPredicates.contains( Disjoint ) )
201 foundSet.insert( f.
id() );
202 handleFeatureFunction( f );
212 const QList< int > &selectedPredicates,
213 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
214 bool onlyRequireTargetIds,
218 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
223 QList< Predicate > predicates;
224 predicates.reserve( selectedPredicates.count() );
225 for (
int i : selectedPredicates )
227 predicates << reversePredicate( static_cast< Predicate >( i ) );
231 if ( predicates.contains( Disjoint ) )
240 std::unique_ptr< QgsGeometryEngine > engine;
253 if ( onlyRequireTargetIds )
263 if ( foundSet.contains( testFeature.
id() ) )
268 if ( predicates.count() == 1 && predicates.at( 0 ) == Disjoint && !disjointSet.contains( testFeature.
id() ) )
277 engine->prepareGeometry();
280 bool isMatch =
false;
282 for ( Predicate predicate : std::as_const( predicates ) )
295 disjointSet.remove( testFeature.
id() );
320 foundSet.insert( testFeature.
id() );
321 handleFeatureFunction( testFeature );
330 if ( predicates.contains( Disjoint ) )
332 disjointSet = disjointSet.subtract( foundSet );
334 if ( onlyRequireTargetIds )
340 handleFeatureFunction( f );
350 void QgsSelectByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
352 QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
353 << QObject::tr(
"adding to current selection" )
354 << QObject::tr(
"selecting within current selection" )
355 << QObject::tr(
"removing from current selection" );
359 addPredicateParameter();
361 QObject::tr(
"By comparing to the features from" ),
365 QObject::tr(
"Modify current selection by" ),
366 methods,
false, 0 ) );
369 QString QgsSelectByLocationAlgorithm::name()
const
371 return QStringLiteral(
"selectbylocation" );
374 QgsProcessingAlgorithm::Flags QgsSelectByLocationAlgorithm::flags()
const
379 QString QgsSelectByLocationAlgorithm::displayName()
const
381 return QObject::tr(
"Select by location" );
384 QStringList QgsSelectByLocationAlgorithm::tags()
const
386 return QObject::tr(
"select,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
389 QString QgsSelectByLocationAlgorithm::group()
const
391 return QObject::tr(
"Vector selection" );
394 QString QgsSelectByLocationAlgorithm::groupId()
const
396 return QStringLiteral(
"vectorselection" );
399 QString QgsSelectByLocationAlgorithm::shortHelpString()
const
401 return QObject::tr(
"This algorithm creates a selection in a vector layer. The criteria for selecting "
402 "features is based on the spatial relationship between each feature and the features in an additional layer." );
405 QgsSelectByLocationAlgorithm *QgsSelectByLocationAlgorithm::createInstance()
const
407 return new QgsSelectByLocationAlgorithm();
412 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT" ), context );
417 std::unique_ptr< QgsFeatureSource > intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
418 if ( !intersectSource )
421 const QList< int > selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
424 auto addToSelection = [&](
const QgsFeature & feature )
426 selectedIds.insert( feature.id() );
428 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
432 results.insert( QStringLiteral(
"OUTPUT" ), parameters.value( QStringLiteral(
"INPUT" ) ) );
441 void QgsExtractByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
444 QObject::tr(
"Extract features from" ),
446 addPredicateParameter();
448 QObject::tr(
"By comparing to the features from" ),
454 QString QgsExtractByLocationAlgorithm::name()
const
456 return QStringLiteral(
"extractbylocation" );
459 QString QgsExtractByLocationAlgorithm::displayName()
const
461 return QObject::tr(
"Extract by location" );
464 QStringList QgsExtractByLocationAlgorithm::tags()
const
466 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
469 QString QgsExtractByLocationAlgorithm::group()
const
471 return QObject::tr(
"Vector selection" );
474 QString QgsExtractByLocationAlgorithm::groupId()
const
476 return QStringLiteral(
"vectorselection" );
479 QString QgsExtractByLocationAlgorithm::shortHelpString()
const
481 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
482 "input layer. The criteria for adding features to the resulting layer is defined "
483 "based on the spatial relationship between each feature and the features in an additional layer." );
486 QgsExtractByLocationAlgorithm *QgsExtractByLocationAlgorithm::createInstance()
const
488 return new QgsExtractByLocationAlgorithm();
493 std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
496 std::unique_ptr< QgsFeatureSource > intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
497 if ( !intersectSource )
500 const QList< int > selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
502 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, input->fields(), input->wkbType(), input->sourceCrs() ) );
507 auto addToSink = [&](
const QgsFeature & feature )
512 process( context, input.get(), intersectSource.get(), selectedPredicates, addToSink,
false, feedback );
515 results.insert( QStringLiteral(
"OUTPUT" ), dest );
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
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 QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
@ SpatialIndexNotPresent
No spatial index exists for 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.
virtual SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on 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 SIP_HOLDGIL
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine representing the specified geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
@ FlagNotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
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.
@ TypeVectorAnyGeometry
Any vector layer with geometry.
A rectangle specified with double values.
Represents a vector layer which manages a vector based data sets.
SelectBehavior
Selection behavior.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using a list of feature IDs.
QSet< QgsFeatureId > QgsFeatureIds