24 void QgsDistanceWithinAlgorithm::addDistanceParameter()
27 QObject::tr(
"Where the features are within" ), 100, QStringLiteral(
"INPUT" ),
false, 0 ) );
28 distanceParam->setIsDynamic(
true );
30 distanceParam->setDynamicLayerParameterName( QStringLiteral(
"INPUT" ) );
32 addParameter( distanceParam.release() );
37 double distance,
const QgsProperty &distanceProperty,
38 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
39 bool onlyRequireTargetIds,
48 bool iterateOverTarget =
false;
58 iterateOverTarget =
true;
65 iterateOverTarget =
true;
74 iterateOverTarget =
true;
82 iterateOverTarget =
true;
88 if ( iterateOverTarget )
90 processByIteratingOverTargetSource( context, targetSource, referenceSource,
91 distance, distanceProperty, handleFeatureFunction,
92 onlyRequireTargetIds, feedback, expressionContext );
96 processByIteratingOverReferenceSource( context, targetSource, referenceSource,
97 distance, handleFeatureFunction,
98 onlyRequireTargetIds, feedback );
104 const double distance,
const QgsProperty &distanceProperty,
105 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
106 bool onlyRequireTargetIds,
110 feedback->
pushWarning( QObject::tr(
"No spatial index exists for intersect layer, performance will be severely degraded" ) );
114 if ( onlyRequireTargetIds )
117 const bool dynamicDistance = distanceProperty.
isActive();
131 double currentDistance = distance;
132 if ( dynamicDistance )
135 currentDistance = distanceProperty.
valueAsDouble( expressionContext, currentDistance );
146 foundSet.insert( f.
id() );
147 handleFeatureFunction( f );
157 const double distance,
158 const std::function <
void(
const QgsFeature & ) > &handleFeatureFunction,
159 bool onlyRequireTargetIds,
163 feedback->
pushWarning( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
181 if ( onlyRequireTargetIds )
191 if ( foundSet.contains( testFeature.
id() ) )
197 foundSet.insert( testFeature.
id() );
198 handleFeatureFunction( testFeature );
211 void QgsSelectWithinDistanceAlgorithm::initAlgorithm(
const QVariantMap & )
213 const QStringList methods = QStringList() << QObject::tr(
"creating new selection" )
214 << QObject::tr(
"adding to current selection" )
215 << QObject::tr(
"selecting within current selection" )
216 << QObject::tr(
"removing from current selection" );
222 QObject::tr(
"By comparing to the features from" ),
224 addDistanceParameter();
227 QObject::tr(
"Modify current selection by" ),
228 methods,
false, 0 ) );
231 QString QgsSelectWithinDistanceAlgorithm::name()
const
233 return QStringLiteral(
"selectwithindistance" );
236 QgsProcessingAlgorithm::Flags QgsSelectWithinDistanceAlgorithm::flags()
const
241 QString QgsSelectWithinDistanceAlgorithm::displayName()
const
243 return QObject::tr(
"Select within distance" );
246 QStringList QgsSelectWithinDistanceAlgorithm::tags()
const
248 return QObject::tr(
"select,maximum,buffer" ).split(
',' );
251 QString QgsSelectWithinDistanceAlgorithm::group()
const
253 return QObject::tr(
"Vector selection" );
256 QString QgsSelectWithinDistanceAlgorithm::groupId()
const
258 return QStringLiteral(
"vectorselection" );
261 QString QgsSelectWithinDistanceAlgorithm::shortHelpString()
const
263 return QObject::tr(
"This algorithm creates a selection in a vector layer. Features are selected wherever they are within "
264 "the specified maximum distance from the features in an additional reference layer." );
267 QgsSelectWithinDistanceAlgorithm *QgsSelectWithinDistanceAlgorithm::createInstance()
const
269 return new QgsSelectWithinDistanceAlgorithm();
274 QgsVectorLayer *selectLayer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT" ), context );
279 const std::unique_ptr< QgsFeatureSource > referenceSource( parameterAsSource( parameters, QStringLiteral(
"REFERENCE" ), context ) );
280 if ( !referenceSource )
283 const double distance = parameterAsDouble( parameters, QStringLiteral(
"DISTANCE" ), context );
286 if ( dynamicDistance )
287 distanceProperty = parameters.
value( QStringLiteral(
"DISTANCE" ) ).value<
QgsProperty >();
292 auto addToSelection = [&](
const QgsFeature & feature )
294 selectedIds.insert( feature.id() );
296 process( context, selectLayer, referenceSource.get(), distance, distanceProperty, addToSelection,
true, feedback, expressionContext );
300 results.insert( QStringLiteral(
"OUTPUT" ), parameters.value( QStringLiteral(
"INPUT" ) ) );
309 void QgsExtractWithinDistanceAlgorithm::initAlgorithm(
const QVariantMap & )
312 QObject::tr(
"Extract features from" ),
315 QObject::tr(
"By comparing to the features from" ),
317 addDistanceParameter();
322 QString QgsExtractWithinDistanceAlgorithm::name()
const
324 return QStringLiteral(
"extractwithindistance" );
327 QString QgsExtractWithinDistanceAlgorithm::displayName()
const
329 return QObject::tr(
"Extract within distance" );
332 QStringList QgsExtractWithinDistanceAlgorithm::tags()
const
334 return QObject::tr(
"extract,filter,select,maximum,buffer" ).split(
',' );
337 QString QgsExtractWithinDistanceAlgorithm::group()
const
339 return QObject::tr(
"Vector selection" );
342 QString QgsExtractWithinDistanceAlgorithm::groupId()
const
344 return QStringLiteral(
"vectorselection" );
347 QString QgsExtractWithinDistanceAlgorithm::shortHelpString()
const
349 return QObject::tr(
"This algorithm creates a new vector layer that only contains matching features from an "
350 "input layer. Features are copied wherever they are within "
351 "the specified maximum distance from the features in an additional reference layer." );
354 QgsExtractWithinDistanceAlgorithm *QgsExtractWithinDistanceAlgorithm::createInstance()
const
356 return new QgsExtractWithinDistanceAlgorithm();
361 std::unique_ptr< QgsProcessingFeatureSource > input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
364 const std::unique_ptr< QgsFeatureSource > referenceSource( parameterAsSource( parameters, QStringLiteral(
"REFERENCE" ), context ) );
365 if ( !referenceSource )
368 const double distance = parameterAsDouble( parameters, QStringLiteral(
"DISTANCE" ), context );
371 if ( dynamicDistance )
372 distanceProperty = parameters.
value( QStringLiteral(
"DISTANCE" ) ).value<
QgsProperty >();
373 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, input.get() );
376 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, input->fields(), input->wkbType(), input->sourceCrs() ) );
381 auto addToSink = [&](
const QgsFeature & feature )
387 process( context, input.get(), referenceSource.get(), distance, distanceProperty, addToSink,
false, feedback, expressionContext );
390 results.insert( QStringLiteral(
"OUTPUT" ), dest );