25using namespace Qt::StringLiterals;
29void QgsDistanceWithinAlgorithm::addDistanceParameter()
31 auto distanceParam = std::make_unique<QgsProcessingParameterDistance>( u
"DISTANCE"_s, QObject::tr(
"Where the features are within" ), 100, u
"INPUT"_s,
false, 0 );
32 distanceParam->setIsDynamic(
true );
34 distanceParam->setDynamicLayerParameterName( u
"INPUT"_s );
36 addParameter( distanceParam.release() );
47 bool iterateOverTarget =
false;
57 iterateOverTarget =
true;
64 iterateOverTarget =
true;
72 iterateOverTarget =
true;
80 iterateOverTarget =
true;
85 if ( iterateOverTarget )
87 processByIteratingOverTargetSource( context, targetSource, referenceSource, distance, distanceProperty, handleFeatureFunction, onlyRequireTargetIds, feedback, expressionContext );
91 processByIteratingOverReferenceSource( context, targetSource, referenceSource, distance, handleFeatureFunction, onlyRequireTargetIds, feedback );
98 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
102 if ( onlyRequireTargetIds )
105 const bool dynamicDistance = distanceProperty.
isActive();
120 double currentDistance = distance;
121 if ( dynamicDistance )
124 currentDistance = distanceProperty.
valueAsDouble( expressionContext, currentDistance );
135 foundSet.insert( f.
id() );
136 handleFeatureFunction( f );
147 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
165 if ( onlyRequireTargetIds )
175 if ( foundSet.contains( testFeature.
id() ) )
181 foundSet.insert( testFeature.
id() );
182 handleFeatureFunction( testFeature );
195void QgsSelectWithinDistanceAlgorithm::initAlgorithm(
const QVariantMap & )
197 const QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
198 << QObject::tr(
"adding to current selection" )
199 << QObject::tr(
"selecting within current selection" )
200 << QObject::tr(
"removing from current selection" );
205 addDistanceParameter();
207 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Modify current selection by" ), methods,
false, 0 ) );
210QString QgsSelectWithinDistanceAlgorithm::name()
const
212 return u
"selectwithindistance"_s;
220QString QgsSelectWithinDistanceAlgorithm::displayName()
const
222 return QObject::tr(
"Select within distance" );
225QStringList QgsSelectWithinDistanceAlgorithm::tags()
const
227 return QObject::tr(
"select,by,maximum,buffer" ).split(
',' );
230QString QgsSelectWithinDistanceAlgorithm::group()
const
232 return QObject::tr(
"Vector selection" );
235QString QgsSelectWithinDistanceAlgorithm::groupId()
const
237 return u
"vectorselection"_s;
240QString QgsSelectWithinDistanceAlgorithm::shortHelpString()
const
242 return QObject::tr(
"This algorithm creates a selection in a vector layer. Features are selected wherever they are within "
243 "the specified maximum distance from the features in an additional reference layer." );
246QString QgsSelectWithinDistanceAlgorithm::shortDescription()
const
248 return QObject::tr(
"Selects features that are within a specified distance from features in another layer." );
251QgsSelectWithinDistanceAlgorithm *QgsSelectWithinDistanceAlgorithm::createInstance()
const
253 return new QgsSelectWithinDistanceAlgorithm();
258 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
263 const std::unique_ptr<QgsFeatureSource> referenceSource( parameterAsSource( parameters, u
"REFERENCE"_s, context ) );
264 if ( !referenceSource )
267 const double distance = parameterAsDouble( parameters, u
"DISTANCE"_s, context );
270 if ( dynamicDistance )
276 auto addToSelection = [&](
const QgsFeature &feature ) {
277 selectedIds.insert( feature.id() );
279 process( context, selectLayer, referenceSource.get(), distance, distanceProperty, addToSelection,
true, feedback, expressionContext );
283 results.insert( u
"OUTPUT"_s, parameters.value( u
"INPUT"_s ) );
292void QgsExtractWithinDistanceAlgorithm::initAlgorithm(
const QVariantMap & )
296 addDistanceParameter();
301QString QgsExtractWithinDistanceAlgorithm::name()
const
303 return u
"extractwithindistance"_s;
306QString QgsExtractWithinDistanceAlgorithm::displayName()
const
308 return QObject::tr(
"Extract within distance" );
311QStringList QgsExtractWithinDistanceAlgorithm::tags()
const
313 return QObject::tr(
"extract,by,filter,select,maximum,buffer" ).split(
',' );
316QString QgsExtractWithinDistanceAlgorithm::group()
const
318 return QObject::tr(
"Vector selection" );
321QString QgsExtractWithinDistanceAlgorithm::groupId()
const
323 return u
"vectorselection"_s;
326QString QgsExtractWithinDistanceAlgorithm::shortHelpString()
const
328 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
329 "input layer. Features are copied wherever they are within "
330 "the specified maximum distance from the features in an additional reference layer." );
333QString QgsExtractWithinDistanceAlgorithm::shortDescription()
const
335 return QObject::tr(
"Creates a new vector layer with features that are within "
336 "a specified distance from features in another layer." );
339QgsExtractWithinDistanceAlgorithm *QgsExtractWithinDistanceAlgorithm::createInstance()
const
341 return new QgsExtractWithinDistanceAlgorithm();
346 std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, u
"INPUT"_s, context ) );
349 const std::unique_ptr<QgsFeatureSource> referenceSource( parameterAsSource( parameters, u
"REFERENCE"_s, context ) );
350 if ( !referenceSource )
353 const double distance = parameterAsDouble( parameters, u
"DISTANCE"_s, context );
356 if ( dynamicDistance )
358 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, input.get() );
361 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, input->fields(), input->wkbType(), input->sourceCrs() ) );
366 auto addToSink = [&](
const QgsFeature &feature ) {
371 process( context, input.get(), referenceSource.get(), distance, distanceProperty, addToSink,
false, feedback, expressionContext );
376 results.insert( u
"OUTPUT"_s, dest );
@ VectorAnyGeometry
Any vector layer with geometry.
@ NotPresent
No spatial index exists for the source.
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.
Represents a coordinate reference system (CRS).
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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 & setLimit(long long limit)
Set the maximum number of features to request.
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 & setDistanceWithin(const QgsGeometry &geometry, double distance)
Sets a reference geometry and a maximum distance from this geometry to retrieve features within.
@ 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.
virtual Qgis::WkbType wkbType() const =0
Returns the geometry type for features returned by this source.
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.
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.
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.
static bool isDynamic(const QVariantMap ¶meters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
@ DoublePositive
Positive double value (including 0).
A store for object properties.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
bool isActive() const
Returns whether the property is currently active.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
Represents a vector layer which manages a vector based dataset.
QgsExpressionContextScope * createExpressionContextScope() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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