Skip to content
Permalink
Browse files

New algorithm to convert an extent parameter to a layer

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 8605be0dbbd7e981b2a5dec66704156780a01cfd
Showing with 70 additions and 1 deletion.
  1. +46 −1 src/core/processing/qgsnativealgorithms.cpp
  2. +24 −0 src/core/processing/qgsnativealgorithms.h
@@ -83,6 +83,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
addAlgorithm( new QgsExtractByExtentAlgorithm() );
addAlgorithm( new QgsExtentToLayerAlgorithm() );
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
@@ -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
@@ -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

0 comments on commit 8605be0

Please sign in to comment.
You can’t perform that action at this time.