forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
343ac33
commit ed0766e
Showing
11 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/*************************************************************************** | ||
qgsalgorithmcoverageunion.cpp | ||
--------------------- | ||
begin : October 2023 | ||
copyright : (C) 2023 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
|
||
#include "qgsalgorithmcoverageunion.h" | ||
#include "qgsgeometrycollection.h" | ||
#include "qgsgeos.h" | ||
|
||
|
||
///@cond PRIVATE | ||
|
||
QString QgsCoverageUnionAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "coverageunion" ); | ||
} | ||
|
||
QString QgsCoverageUnionAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Dissolve coverage" ); | ||
} | ||
|
||
QStringList QgsCoverageUnionAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "union,merge,topological,boundary" ).split( ',' ); | ||
} | ||
|
||
QString QgsCoverageUnionAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector coverage" ); | ||
} | ||
|
||
QString QgsCoverageUnionAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorcoverage" ); | ||
} | ||
|
||
void QgsCoverageUnionAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorPolygon ) ); | ||
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Dissolved" ), QgsProcessing::TypeVectorPolygon ) ); | ||
} | ||
|
||
QString QgsCoverageUnionAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "" ); | ||
} | ||
|
||
QgsCoverageUnionAlgorithm *QgsCoverageUnionAlgorithm::createInstance() const | ||
{ | ||
return new QgsCoverageUnionAlgorithm(); | ||
} | ||
|
||
QVariantMap QgsCoverageUnionAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); | ||
if ( !source ) | ||
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); | ||
|
||
QString sinkId; | ||
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, sinkId, source->fields(), Qgis::WkbType::MultiPolygon, source->sourceCrs() ) ); | ||
if ( !sink ) | ||
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); | ||
|
||
// we have to build up a list of geometries in advance | ||
QgsGeometryCollection collection; | ||
|
||
const long count = source->featureCount(); | ||
if ( count > 0 ) | ||
{ | ||
collection.reserve( count ); | ||
} | ||
|
||
const double step = count > 0 ? 100.0 / count : 1; | ||
int current = 0; | ||
|
||
feedback->pushInfo( QObject::tr( "Collecting features" ) ); | ||
|
||
QgsFeature inFeature; | ||
QgsFeatureRequest req; | ||
req.setNoAttributes(); | ||
QgsFeatureIterator features = source->getFeatures( req ); | ||
while ( features.nextFeature( inFeature ) ) | ||
{ | ||
if ( feedback->isCanceled() ) | ||
{ | ||
break; | ||
} | ||
|
||
if ( inFeature.hasGeometry() ) | ||
{ | ||
collection.addGeometry( inFeature.geometry().constGet()->clone() ); | ||
} | ||
|
||
feedback->setProgress( current * step * 0.2 ); | ||
current++; | ||
} | ||
|
||
feedback->pushInfo( QObject::tr( "Dissolving coverage" ) ); | ||
|
||
QgsGeos geos( &collection ); | ||
QString error; | ||
std::unique_ptr< QgsAbstractGeometry > dissolved = geos.unionCoverage( &error ); | ||
|
||
if ( !dissolved ) | ||
{ | ||
if ( !error.isEmpty() ) | ||
throw QgsProcessingException( error ); | ||
else | ||
throw QgsProcessingException( QObject::tr( "No geometry was returned for dissolved coverage" ) ); | ||
} | ||
|
||
feedback->setProgress( 95 ); | ||
|
||
feedback->pushInfo( QObject::tr( "Storing output" ) ); | ||
QgsFeature outFeature( source->fields() ); | ||
outFeature.setGeometry( std::move( dissolved ) ); | ||
if ( !sink->addFeature( outFeature, QgsFeatureSink::FastInsert ) ) | ||
throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) ); | ||
|
||
QVariantMap outputs; | ||
outputs.insert( QStringLiteral( "OUTPUT" ), sinkId ); | ||
return outputs; | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/*************************************************************************** | ||
qgsalgorithmcoverageunion.h | ||
--------------------- | ||
begin : October 2023 | ||
copyright : (C) 2023 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMCOVERAGEUNION_H | ||
#define QGSALGORITHMCOVERAGEUNION_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native coverage union algorithm. | ||
*/ | ||
class QgsCoverageUnionAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
public: | ||
QgsCoverageUnionAlgorithm() = default; | ||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QgsCoverageUnionAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
}; | ||
|
||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMCOVERAGEUNION_H | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters