23 QString QgsUnionAlgorithm::name()
const
25 return QStringLiteral(
"union" );
28 QString QgsUnionAlgorithm::displayName()
const
30 return QObject::tr(
"Union" );
33 QString QgsUnionAlgorithm::group()
const
35 return QObject::tr(
"Vector overlay" );
38 QString QgsUnionAlgorithm::groupId()
const
40 return QStringLiteral(
"vectoroverlay" );
43 QString QgsUnionAlgorithm::shortHelpString()
const
45 return QObject::tr(
"This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
46 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
47 "features that participate in that overlap." )
48 + QStringLiteral(
"\n\n" )
49 + QObject::tr(
"An Overlay layer can also be used, in which case features from each layer are split at their overlap with features from "
50 "the other one, creating a layer containing all the portions from both Input and Overlay layers. "
51 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
52 "for non-overlapping features, and attribute values from both layers for overlapping features." );
57 return new QgsUnionAlgorithm();
60 void QgsUnionAlgorithm::initAlgorithm(
const QVariantMap & )
65 std::unique_ptr< QgsProcessingParameterString > prefix = std::make_unique< QgsProcessingParameterString >( QStringLiteral(
"OVERLAY_FIELDS_PREFIX" ), QObject::tr(
"Overlay fields prefix" ), QString(),
false,
true );
67 addParameter( prefix.release() );
74 std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
78 std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral(
"OVERLAY" ), context ) );
79 if ( parameters.value( QStringLiteral(
"OVERLAY" ) ).isValid() && !sourceB )
84 const QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral(
"OVERLAY_FIELDS_PREFIX" ), context );
88 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
93 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
98 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
106 const long total = sourceA->featureCount() * 2 + sourceB->featureCount();
108 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB );
112 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB );
116 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA );