Skip to content

Commit 8605be0

Browse files
committedSep 15, 2017
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
1 parent 9b11228 commit 8605be0

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
 

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
8383
addAlgorithm( new QgsSmoothAlgorithm() );
8484
addAlgorithm( new QgsSimplifyAlgorithm() );
8585
addAlgorithm( new QgsExtractByExtentAlgorithm() );
86+
addAlgorithm( new QgsExtentToLayerAlgorithm() );
8687
}
8788

8889
void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
@@ -1884,5 +1885,49 @@ QVariantMap QgsExtractByExtentAlgorithm::processAlgorithm( const QVariantMap &pa
18841885
return outputs;
18851886
}
18861887

1887-
///@endcond
18881888

1889+
void QgsExtentToLayerAlgorithm::initAlgorithm( const QVariantMap & )
1890+
{
1891+
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "INPUT" ), QObject::tr( "Extent" ) ) );
1892+
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), QgsProcessing::TypeVectorPolygon ) );
1893+
}
1894+
1895+
QString QgsExtentToLayerAlgorithm::shortHelpString() const
1896+
{
1897+
return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching an extent parameter.\n\n"
1898+
"It can be used in models to convert an extent into a layer which can be used for other algorithms which require "
1899+
"a layer based input." );
1900+
}
1901+
1902+
QgsExtentToLayerAlgorithm *QgsExtentToLayerAlgorithm::createInstance() const
1903+
{
1904+
return new QgsExtentToLayerAlgorithm();
1905+
}
1906+
1907+
QVariantMap QgsExtentToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
1908+
{
1909+
QgsCoordinateReferenceSystem crs = parameterAsExtentCrs( parameters, QStringLiteral( "INPUT" ), context );
1910+
QgsGeometry geom = parameterAsExtentGeometry( parameters, QStringLiteral( "INPUT" ), context );
1911+
1912+
QgsFields fields;
1913+
fields.append( QgsField( QStringLiteral( "id" ), QVariant::Int ) );
1914+
1915+
QString dest;
1916+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, crs ) );
1917+
if ( !sink )
1918+
return QVariantMap();
1919+
1920+
QgsFeature f;
1921+
f.setAttributes( QgsAttributes() << 1 );
1922+
f.setGeometry( geom );
1923+
sink->addFeature( f, QgsFeatureSink::FastInsert );
1924+
1925+
feedback->setProgress( 100 );
1926+
1927+
QVariantMap outputs;
1928+
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
1929+
return outputs;
1930+
}
1931+
1932+
1933+
///@endcond

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,30 @@ class QgsExtractByExtentAlgorithm : public QgsProcessingAlgorithm
688688

689689
};
690690

691+
/**
692+
* Native extent to layer algorithm.
693+
*/
694+
class QgsExtentToLayerAlgorithm : public QgsProcessingAlgorithm
695+
{
696+
697+
public:
698+
699+
QgsExtentToLayerAlgorithm() = default;
700+
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
701+
QString name() const override { return QStringLiteral( "extenttolayer" ); }
702+
QString displayName() const override { return QObject::tr( "Create layer from extent" ); }
703+
virtual QStringList tags() const override { return QObject::tr( "extent,layer,polygon,create,new" ).split( ',' ); }
704+
QString group() const override { return QObject::tr( "Vector geometry" ); }
705+
QString shortHelpString() const override;
706+
QgsExtentToLayerAlgorithm *createInstance() const override SIP_FACTORY;
707+
708+
protected:
709+
710+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
711+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
712+
713+
};
714+
691715
///@endcond PRIVATE
692716

693717
#endif // QGSNATIVEALGORITHMS_H

0 commit comments

Comments
 (0)
Please sign in to comment.