25void QgsLocationBasedAlgorithm::addPredicateParameter()
28 QObject::tr(
"Where the features (geometric predicate)" ),
29 predicateOptionsList(),
true, QVariant::fromValue( QList< int >() << 0 ) ) );
31 QVariantMap predicateMetadata;
32 QVariantMap widgetMetadata;
33 widgetMetadata.insert( QStringLiteral(
"useCheckBoxes" ),
true );
34 widgetMetadata.insert( QStringLiteral(
"columns" ), 2 );
35 predicateMetadata.insert( QStringLiteral(
"widget_wrapper" ), widgetMetadata );
36 predicateParam->setMetadata( predicateMetadata );
38 addParameter( predicateParam.release() );
41QgsLocationBasedAlgorithm::Predicate QgsLocationBasedAlgorithm::reversePredicate( QgsLocationBasedAlgorithm::Predicate predicate )
const
66QStringList QgsLocationBasedAlgorithm::predicateOptionsList()
const
68 return QStringList() << QObject::tr(
"intersect" )
69 << QObject::tr(
"contain" )
70 << QObject::tr(
"disjoint" )
71 << QObject::tr(
"equal" )
72 << QObject::tr(
"touch" )
73 << QObject::tr(
"overlap" )
74 << QObject::tr(
"are within" )
75 << QObject::tr(
"cross" );
80 const QList< int > &selectedPredicates,
81 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
82 bool onlyRequireTargetIds,
87 if ( mTargetFeatureCount == 0 )
91 if ( mIntersectFeatureCount == 0 &&
92 !selectedPredicates.contains( Disjoint ) )
95 if ( mTargetFeatureCount > 0 && mIntersectFeatureCount > 0 &&
96 mTargetFeatureCount < mIntersectFeatureCount )
99 processByIteratingOverTargetSource( context, targetSource, intersectSource,
100 selectedPredicates, handleFeatureFunction,
101 onlyRequireTargetIds, feedback, skipTargetFeatureIds );
111 processByIteratingOverIntersectSource( context, targetSource, intersectSource,
112 selectedPredicates, handleFeatureFunction,
113 onlyRequireTargetIds, feedback, skipTargetFeatureIds );
119 const QList< int > &selectedPredicates,
120 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
121 bool onlyRequireTargetIds,
126 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
130 if ( onlyRequireTargetIds )
134 double step = mTargetFeatureCount > 0 ? 100.0 /
static_cast< double >( mTargetFeatureCount ) : 1;
137 std::unique_ptr< QgsGeometryEngine > engine;
144 if ( skipTargetFeatureIds.contains( f.
id() ) )
156 bool isMatch =
false;
157 bool isDisjoint =
true;
166 engine->prepareGeometry();
169 for (
int predicate : selectedPredicates )
171 switch (
static_cast< Predicate
>( predicate ) )
208 foundSet.insert( f.
id() );
209 handleFeatureFunction( f );
213 if ( isDisjoint && selectedPredicates.contains( Disjoint ) )
215 foundSet.insert( f.
id() );
216 handleFeatureFunction( f );
226 const QList< int > &selectedPredicates,
227 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
228 bool onlyRequireTargetIds,
233 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
238 QList< Predicate > predicates;
239 predicates.reserve( selectedPredicates.count() );
240 for (
int i : selectedPredicates )
242 predicates << reversePredicate( static_cast< Predicate >( i ) );
246 if ( predicates.contains( Disjoint ) )
252 double step = mIntersectFeatureCount > 0 ? 100.0 /
static_cast< double >( mIntersectFeatureCount ) : 1;
255 std::unique_ptr< QgsGeometryEngine > engine;
268 if ( onlyRequireTargetIds )
278 if ( skipTargetFeatureIds.contains( testFeature.
id() ) )
283 if ( foundSet.contains( testFeature.
id() ) )
288 if ( predicates.count() == 1 && predicates.at( 0 ) == Disjoint && !disjointSet.contains( testFeature.
id() ) )
297 engine->prepareGeometry();
300 bool isMatch =
false;
302 for ( Predicate predicate : std::as_const( predicates ) )
315 disjointSet.remove( testFeature.
id() );
340 foundSet.insert( testFeature.
id() );
341 handleFeatureFunction( testFeature );
350 if ( predicates.contains( Disjoint ) )
352 disjointSet = disjointSet.subtract( foundSet );
354 if ( onlyRequireTargetIds )
360 handleFeatureFunction( f );
370void QgsSelectByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
372 QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
373 << QObject::tr(
"adding to current selection" )
374 << QObject::tr(
"selecting within current selection" )
375 << QObject::tr(
"removing from current selection" );
379 addPredicateParameter();
381 QObject::tr(
"By comparing to the features from" ),
385 QObject::tr(
"Modify current selection by" ),
386 methods,
false, 0 ) );
389QString QgsSelectByLocationAlgorithm::name()
const
391 return QStringLiteral(
"selectbylocation" );
399QString QgsSelectByLocationAlgorithm::displayName()
const
401 return QObject::tr(
"Select by location" );
404QStringList QgsSelectByLocationAlgorithm::tags()
const
406 return QObject::tr(
"select,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
409QString QgsSelectByLocationAlgorithm::group()
const
411 return QObject::tr(
"Vector selection" );
414QString QgsSelectByLocationAlgorithm::groupId()
const
416 return QStringLiteral(
"vectorselection" );
419QString QgsSelectByLocationAlgorithm::shortHelpString()
const
421 return QObject::tr(
"This algorithm creates a selection in a vector layer. The criteria for selecting "
422 "features is based on the spatial relationship between each feature and the features in an additional layer." );
425QgsSelectByLocationAlgorithm *QgsSelectByLocationAlgorithm::createInstance()
const
427 return new QgsSelectByLocationAlgorithm();
432 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT" ), context );
437 std::unique_ptr< QgsFeatureSource > intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
438 if ( !intersectSource )
441 const QList< int > selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
444 auto addToSelection = [&](
const QgsFeature & feature )
446 selectedIds.insert( feature.id() );
451 mIntersectFeatureCount = intersectSource->
featureCount();
460 mTargetCrs = selectLayerSelected->sourceCrs();
461 mTargetFeatureCount = selectLayerSelected->featureCount();
462 process( context, selectLayerSelected.get(), intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
467 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback, selectLayer->
selectedFeatureIds() );
470 process( context, selectLayer, intersectSource.get(), selectedPredicates, addToSelection,
true, feedback );
476 results.insert( QStringLiteral(
"OUTPUT" ), parameters.value( QStringLiteral(
"INPUT" ) ) );
485void QgsExtractByLocationAlgorithm::initAlgorithm(
const QVariantMap & )
488 QObject::tr(
"Extract features from" ),
490 addPredicateParameter();
492 QObject::tr(
"By comparing to the features from" ),
498QString QgsExtractByLocationAlgorithm::name()
const
500 return QStringLiteral(
"extractbylocation" );
503QString QgsExtractByLocationAlgorithm::displayName()
const
505 return QObject::tr(
"Extract by location" );
508QStringList QgsExtractByLocationAlgorithm::tags()
const
510 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
513QString QgsExtractByLocationAlgorithm::group()
const
515 return QObject::tr(
"Vector selection" );
518QString QgsExtractByLocationAlgorithm::groupId()
const
520 return QStringLiteral(
"vectorselection" );
523QString QgsExtractByLocationAlgorithm::shortHelpString()
const
525 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
526 "input layer. The criteria for adding features to the resulting layer is defined "
527 "based on the spatial relationship between each feature and the features in an additional layer." );
530QgsExtractByLocationAlgorithm *QgsExtractByLocationAlgorithm::createInstance()
const
532 return new QgsExtractByLocationAlgorithm();
538 std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
541 std::unique_ptr< QgsFeatureSource > intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
542 if ( !intersectSource )
545 mTargetCrs = input->sourceCrs();
546 mTargetFeatureCount = input->featureCount();
547 mIntersectFeatureCount = intersectSource->
featureCount();
554 std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
557 std::unique_ptr< QgsFeatureSource > intersectSource( parameterAsSource( parameters, QStringLiteral(
"INTERSECT" ), context ) );
558 if ( !intersectSource )
561 const QList< int > selectedPredicates = parameterAsEnums( parameters, QStringLiteral(
"PREDICATE" ), context );
563 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, input->fields(), input->wkbType(), mTargetCrs ) );
568 auto addToSink = [&](
const QgsFeature & feature )
574 process( context, input.get(), intersectSource.get(), selectedPredicates, addToSink,
false, feedback );
577 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.
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 & 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.
QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
Represents a vector layer which manages a vector based data sets.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsCoordinateReferenceSystem sourceCrs() const FINAL
Returns the coordinate reference system for features in the source.
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.
QSet< QgsFeatureId > QgsFeatureIds