25 QString QgsClipAlgorithm::name()
const
27 return QStringLiteral(
"clip" );
30 QgsProcessingAlgorithm::Flags QgsClipAlgorithm::flags()
const
37 QString QgsClipAlgorithm::displayName()
const
39 return QObject::tr(
"Clip" );
42 QStringList QgsClipAlgorithm::tags()
const
44 return QObject::tr(
"clip,intersect,intersection,mask" ).split(
',' );
47 QString QgsClipAlgorithm::group()
const
49 return QObject::tr(
"Vector overlay" );
52 QString QgsClipAlgorithm::groupId()
const
54 return QStringLiteral(
"vectoroverlay" );
57 void QgsClipAlgorithm::initAlgorithm(
const QVariantMap & )
65 QString QgsClipAlgorithm::shortHelpString()
const
67 return QObject::tr(
"This algorithm clips a vector layer using the features of an additional polygon layer. Only the parts of the features "
68 "in the Input layer that fall within the polygons of the Overlay layer will be added to the resulting layer." )
69 + QStringLiteral(
"\n\n" )
70 + QObject::tr(
"The attributes of the features are not modified, although properties such as area or length of the features will "
71 "be modified by the clipping operation. If such properties are stored as attributes, those attributes will have to "
72 "be manually updated." );
75 QgsClipAlgorithm *QgsClipAlgorithm::createInstance()
const
77 return new QgsClipAlgorithm();
80 bool QgsClipAlgorithm::supportInPlaceEdit(
const QgsMapLayer *l )
const
82 const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
91 std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
95 std::unique_ptr< QgsFeatureSource > maskSource( parameterAsSource( parameters, QStringLiteral(
"OVERLAY" ), context ) );
100 feedback->
reportError( QObject::tr(
"No spatial index exists for input layer, performance will be severely degraded" ) );
104 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, featureSource->fields(),
QgsWkbTypes::multiType( featureSource->wkbType() ), featureSource->sourceCrs() ) );
110 QVector< QgsGeometry > clipGeoms;
120 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
122 if ( clipGeoms.isEmpty() )
126 bool singleClipFeature =
false;
128 if ( clipGeoms.length() > 1 )
131 if ( combinedClipGeom.
isEmpty() )
135 singleClipFeature =
false;
139 combinedClipGeom = clipGeoms.at( 0 );
140 singleClipFeature =
true;
145 engine->prepareGeometry();
150 const auto constClipGeoms = clipGeoms;
151 for (
const QgsGeometry &clipGeom : constClipGeoms )
164 if ( inputFeatures.isEmpty() )
168 if ( singleClipFeature )
169 step = 100.0 / inputFeatures.length();
172 const auto constInputFeatures = inputFeatures;
173 for (
const QgsFeature &inputFeature : constInputFeatures )
180 if ( !inputFeature.hasGeometry() )
183 if ( testedFeatureIds.contains( inputFeature.id() ) )
188 testedFeatureIds.insert( inputFeature.id() );
190 if ( !engine->intersects( inputFeature.geometry().constGet() ) )
194 if ( !engine->contains( inputFeature.geometry().constGet() ) )
196 QgsGeometry currentGeometry = inputFeature.geometry();
197 newGeometry = combinedClipGeom.
intersection( currentGeometry );
208 newGeometry = inputFeature.geometry();
211 if ( !QgsOverlayUtils::sanitizeIntersectionResult( newGeometry, sinkType ) )
220 if ( singleClipFeature )
224 if ( !singleClipFeature )
227 feedback->
setProgress( 100.0 *
static_cast< double >( i ) / clipGeoms.length() );