QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsalgorithmextenttolayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextenttolayer.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
21
22QString QgsExtentToLayerAlgorithm::name() const
23{
24 return QStringLiteral( "extenttolayer" );
25}
26
27void QgsExtentToLayerAlgorithm::initAlgorithm( const QVariantMap & )
28{
29 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "INPUT" ), QObject::tr( "Extent" ) ) );
30 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extent" ), Qgis::ProcessingSourceType::VectorPolygon ) );
31}
32
33QString QgsExtentToLayerAlgorithm::shortHelpString() const
34{
35 return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching an extent parameter.\n\n"
36 "It can be used in models to convert an extent into a layer which can be used for other algorithms which require "
37 "a layer based input." );
38}
39
40QgsExtentToLayerAlgorithm *QgsExtentToLayerAlgorithm::createInstance() const
41{
42 return new QgsExtentToLayerAlgorithm();
43}
44
45QVariantMap QgsExtentToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
46{
47 const QgsCoordinateReferenceSystem crs = parameterAsExtentCrs( parameters, QStringLiteral( "INPUT" ), context );
48 const QgsGeometry geom = parameterAsExtentGeometry( parameters, QStringLiteral( "INPUT" ), context );
49
50 QgsFields fields;
51 fields.append( QgsField( QStringLiteral( "id" ), QMetaType::Type::Int ) );
52
53 QString dest;
54 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, Qgis::WkbType::Polygon, crs ) );
55 if ( !sink )
56 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
57
58 QgsFeature f;
59 f.setAttributes( QgsAttributes() << 1 );
60 f.setGeometry( geom );
61 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
62 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
63
64 sink->finalize();
65 feedback->setProgress( 100 );
66
67 QVariantMap outputs;
68 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
69 return outputs;
70}
71
73
@ VectorPolygon
Vector polygon layers.
@ Polygon
Polygon.
A vector of attributes.
This class represents a coordinate reference system (CRS).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
A geometry is the spatial representation of a feature.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A rectangular map extent parameter for processing algorithms.
A feature sink output for processing algorithms.
const QgsCoordinateReferenceSystem & crs