Skip to content

Commit

Permalink
New algorithm to convert an extent parameter to a layer
Browse files Browse the repository at this point in the history
Creates a new layer with a single feature with polygon geometry
covering the extent parameter value.

This is designed for use in models where some child algorithms require
a layer based input, while others require an extent based
parameter
  • Loading branch information
nyalldawson committed Sep 15, 2017
1 parent 9b11228 commit 8605be0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/core/processing/qgsnativealgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
addAlgorithm( new QgsExtractByExtentAlgorithm() );
addAlgorithm( new QgsExtentToLayerAlgorithm() );
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
Expand Down Expand Up @@ -1884,5 +1885,49 @@ QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &pa
return outputs;
}

///@endcond

void QgsExtentToLayerAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "INPUT" ), QObject::tr( "Extent" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), QgsProcessing::TypeVectorPolygon ) );
}

QString QgsExtentToLayerAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching an extent parameter.\n\n"
"It can be used in models to convert an extent into a layer which can be used for other algorithms which require "
"a layer based input." );
}

QgsExtentToLayerAlgorithm *QgsExtentToLayerAlgorithm::createInstance() const
{
return new QgsExtentToLayerAlgorithm();
}

QVariantMap QgsExtentToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsCoordinateReferenceSystem crs = parameterAsExtentCrs( parameters, QStringLiteral( "INPUT" ), context );
QgsGeometry geom = parameterAsExtentGeometry( parameters, QStringLiteral( "INPUT" ), context );

QgsFields fields;
fields.append( QgsField( QStringLiteral( "id" ), QVariant::Int ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, crs ) );
if ( !sink )
return QVariantMap();

QgsFeature f;
f.setAttributes( QgsAttributes() << 1 );
f.setGeometry( geom );
sink->addFeature( f, QgsFeatureSink::FastInsert );

feedback->setProgress( 100 );

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}


///@endcond
24 changes: 24 additions & 0 deletions src/core/processing/qgsnativealgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,30 @@ class QgsExtractByExtentAlgorithm : public QgsProcessingAlgorithm

};

/**
* Native extent to layer algorithm.
*/
class QgsExtentToLayerAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsExtentToLayerAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override { return QStringLiteral( "extenttolayer" ); }
QString displayName() const override { return QObject::tr( "Create layer from extent" ); }
virtual QStringList tags() const override { return QObject::tr( "extent,layer,polygon,create,new" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector geometry" ); }
QString shortHelpString() const override;
QgsExtentToLayerAlgorithm *createInstance() const override SIP_FACTORY;

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

};

///@endcond PRIVATE

#endif // QGSNATIVEALGORITHMS_H
Expand Down

0 comments on commit 8605be0

Please sign in to comment.