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 ) );
101 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, featureSource->fields(),
QgsWkbTypes::multiType( featureSource->wkbType() ), featureSource->sourceCrs() ) );
107 QVector< QgsGeometry > clipGeoms;
117 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
119 if ( clipGeoms.isEmpty() )
123 bool singleClipFeature =
false;
125 if ( clipGeoms.length() > 1 )
128 if ( combinedClipGeom.
isEmpty() )
132 singleClipFeature =
false;
136 combinedClipGeom = clipGeoms.at( 0 );
137 singleClipFeature =
true;
142 engine->prepareGeometry();
147 const auto constClipGeoms = clipGeoms;
148 for (
const QgsGeometry &clipGeom : constClipGeoms )
161 if ( inputFeatures.isEmpty() )
165 if ( singleClipFeature )
166 step = 100.0 / inputFeatures.length();
169 const auto constInputFeatures = inputFeatures;
170 for (
const QgsFeature &inputFeature : constInputFeatures )
177 if ( !inputFeature.hasGeometry() )
180 if ( testedFeatureIds.contains( inputFeature.id() ) )
185 testedFeatureIds.insert( inputFeature.id() );
187 if ( !engine->intersects( inputFeature.geometry().constGet() ) )
191 if ( !engine->contains( inputFeature.geometry().constGet() ) )
193 QgsGeometry currentGeometry = inputFeature.geometry();
194 newGeometry = combinedClipGeom.
intersection( currentGeometry );
205 newGeometry = inputFeature.geometry();
208 if ( !QgsOverlayUtils::sanitizeIntersectionResult( newGeometry, sinkType ) )
217 if ( singleClipFeature )
221 if ( !singleClipFeature )
224 feedback->
setProgress( 100.0 *
static_cast< double >( i ) / clipGeoms.length() );